Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 逐块合并两个文本文件_Bash_Shell_Awk_Sed - Fatal编程技术网

Bash 逐块合并两个文本文件

Bash 逐块合并两个文本文件,bash,shell,awk,sed,Bash,Shell,Awk,Sed,我有两个文本文件,每个文件包含一个由空行分隔的文本块。这些积木大小不一 # ::id 10 # ::snt Yes ! ...multiple lines of unstructured data from file 1... # ::id 11 # ::snt said Lion . ...multiple lines of unstructured data from file 1... # ::id 12 # ::snt Yes yes ! ...multiple lines o

我有两个文本文件,每个文件包含一个由空行分隔的文本块。这些积木大小不一

# ::id 10
# ::snt Yes !
 ...multiple lines of unstructured data from file 1...

# ::id 11
# ::snt said Lion .
 ...multiple lines of unstructured data from file 1...

# ::id 12
# ::snt Yes yes !
 ...multiple lines of unstructured data from file 1...

# ::id 13
# ::snt said Tiger .
 ...multiple lines of unstructured data from file 1...
同样地,另一个

# ::id 10
# ::snt No !
 ...multiple lines of unstructured data from file 2...

# ::id 11
# ::snt said Monkey .
 ...multiple lines of unstructured data from file 2...

# ::id 12
# ::snt No no !
 ...multiple lines of unstructured data from file 2...

# ::id 13
# ::snt said Donkey .
 ...multiple lines of unstructured data from file 2...
我想合并这两个块,但按它们的
::id
对它们进行排序。另外,我需要在file2数据块之前维护file1数据块的顺序。所以最终输出应该是这样的:

# ::id 10
# ::snt Yes !
 ...multiple lines of unstructured data from file 1...

# ::id 10
# ::snt No !
 ...multiple lines of unstructured data from file 2...

# ::id 11
# ::snt said Lion .
 ...multiple lines of unstructured data from file 1...

# ::id 11
# ::snt said Monkey .
 ...multiple lines of unstructured data from file 2...

# ::id 12
# ::snt Yes yes !
 ...multiple lines of unstructured data from file 1...

# ::id 12
# ::snt No no !
 ...multiple lines of unstructured data from file 2...

# ::id 13
# ::snt said Tiger .
 ...multiple lines of unstructured data from file 1...

# ::id 13
# ::snt said Donkey .
 ...multiple lines of unstructured data from file 2...

我该怎么做?任何东西都可以工作,
bash
sed
awk
说:
awk-f merge.awk file1 file2

BEGIN { RS="" }
{ ARR[NR] = $0 }
END {
    n = asort(ARR);
    for (i = 1; i <= n; i++)
        print ARR[i];
}
开始{RS=”“}
{ARR[NR]=$0}
结束{
n=asort(ARR);

对于(i=1;i说:
awk-f merge.awk file1 file2

BEGIN { RS="" }
{ ARR[NR] = $0 }
END {
    n = asort(ARR);
    for (i = 1; i <= n; i++)
        print ARR[i];
}
开始{RS=”“}
{ARR[NR]=$0}
结束{
n=asort(ARR);

对于(i=1;i您可以使用
sed
sort
实现这一点:

 sed '/# ::id/N;s/\n/ /;/^$/d' file1 file2 | sort -s -n -k3,3 | sed 's/\(# ::snt.*\)/\n\1\n/'
第一个
sed
部分将下一行与包含
\:id的部分连接在一起,并删除空行

然后根据表达式的id编号对结果进行排序::id xx
(第三个参数)


最后,在找到
::snt
的地方,将该行切割成两段

您可以使用
sed
排序来实现这一点:

 sed '/# ::id/N;s/\n/ /;/^$/d' file1 file2 | sort -s -n -k3,3 | sed 's/\(# ::snt.*\)/\n\1\n/'
$ awk -v RS= -v ORS='\n\n' 'NR==FNR{a[NR]=$0;next} {print a[FNR] ORS $0}' file1 file2
# ::id 10
# ::snt Yes !

# ::id 10
# ::snt No !

# ::id 11
# ::snt said Lion .

# ::id 11
# ::snt said Monkey .

# ::id 12
# ::snt Yes yes !

# ::id 12
# ::snt No no !

# ::id 13
# ::snt said Tiger .

# ::id 13
# ::snt said Donkey .
第一个
sed
部分将下一行与包含
\:id的部分连接在一起,并删除空行

然后根据表达式的id编号对结果进行排序::id xx
(第三个参数)

最后,在找到
#::snt
的地方,将线切割成两段

$ awk -v RS= -v ORS='\n\n' 'NR==FNR{a[NR]=$0;next} {print a[FNR] ORS $0}' file1 file2
# ::id 10
# ::snt Yes !

# ::id 10
# ::snt No !

# ::id 11
# ::snt said Lion .

# ::id 11
# ::snt said Monkey .

# ::id 12
# ::snt Yes yes !

# ::id 12
# ::snt No no !

# ::id 13
# ::snt said Tiger .

# ::id 13
# ::snt said Donkey .
上述方法将文件内容一次读取一个段落到数组
a[]
中,其中段落是由一连串空行分隔的文本块(通过将
RS
设置为null)。读取第一个文件时,它只将其存储在数组
a[1..段落数]
然后在将所有文件1读入
a[]
之后,当它读取第二个文件时,它首先打印文件1中的相应段落(
a[段落编号]
),然后打印文件2中的当前段落


上述方法将文件内容一次读取一个段落到数组
a[]
中,其中段落是由一连串空行分隔的文本块(通过将
RS
设置为null)。读取第一个文件时,它只将其存储在数组
a[1..段落数]
然后在将所有文件1读入
a[]
之后,当它读取第二个文件时,它首先打印文件1中的相应段落(
a[段落编号]
),然后打印文件2中的当前段落。

如果两个文件中的记录没有对齐,那么它将根据ID编号匹配记录

$ awk -F'\n' -v RS= 'NR==FNR{a[$1]=$0; next}
                            {printf "%s\n\n%s\n\n",a[$1],$0}' file1 file2

# ::id 10
# ::snt Yes !
 ...multiple lines of unstructured data from file 1...

# ::id 10
# ::snt No !
 ...multiple lines of unstructured data from file 2...

# ::id 11
# ::snt said Lion .
 ...multiple lines of unstructured data from file 1...

# ::id 11
# ::snt said Monkey .
 ...multiple lines of unstructured data from file 2...

# ::id 12
# ::snt Yes yes !
 ...multiple lines of unstructured data from file 1...

# ::id 12
# ::snt No no !
 ...multiple lines of unstructured data from file 2...

# ::id 13
# ::snt said Tiger .
 ...multiple lines of unstructured data from file 1...

# ::id 13
# ::snt said Donkey .
 ...multiple lines of unstructured data from file 2...

还可以增强以捕获文件2中丢失的记录。

如果两个文件中的记录未对齐,则会通过ID号匹配记录

$ awk -F'\n' -v RS= 'NR==FNR{a[$1]=$0; next}
                            {printf "%s\n\n%s\n\n",a[$1],$0}' file1 file2

# ::id 10
# ::snt Yes !
 ...multiple lines of unstructured data from file 1...

# ::id 10
# ::snt No !
 ...multiple lines of unstructured data from file 2...

# ::id 11
# ::snt said Lion .
 ...multiple lines of unstructured data from file 1...

# ::id 11
# ::snt said Monkey .
 ...multiple lines of unstructured data from file 2...

# ::id 12
# ::snt Yes yes !
 ...multiple lines of unstructured data from file 1...

# ::id 12
# ::snt No no !
 ...multiple lines of unstructured data from file 2...

# ::id 13
# ::snt said Tiger .
 ...multiple lines of unstructured data from file 1...

# ::id 13
# ::snt said Donkey .
 ...multiple lines of unstructured data from file 2...


还可以增强以捕获文件2中丢失的记录。

awk
允许您使用regexp作为记录分隔符(RS)。例如,您可以将文件读入一个数组,然后使用
asort
对其进行排序。您可以提供语法吗?我的
awk
有点生疏。问题是我想将两个文本文件合并为一个。我该如何做?基本思想是将两个文件读入一个数组并对其进行排序。但是,如果文件非常大,则uld不是一个有效的策略。请提供一些指针作为答案?例如更改RS并将匹配的文本块存储到数组中?
awk
允许您使用regexp作为记录分隔符(RS)。例如,您可以将文件读入一个数组,然后使用
asort
对其进行排序。您可以提供语法吗?我的
awk
有点生疏。问题是我想将两个文本文件合并为一个。我该如何做?基本思想是将两个文件读入一个数组并对其进行排序。但是,如果文件非常大,则uld不是一个有效的策略。请您提供一些指针来回答问题?例如更改RS并将匹配的文本块存储到数组中?这可能会超出预期的排序范围,因为排序时将使用段落的全部内容。您可以按id存储在
ARR
(并附加到值中)为了避免这种情况。谢谢!非常有用。但是,asort函数会打乱文件的顺序。我希望第一个块来自文件1,第二个块来自文件2。但它返回的顺序相反。我尝试对文件重新排序,但没有成功。它看起来像是
asort
函数将数组排序为字符串。因此,如果
file2
中的块是字符串越小,它将首先出现。有关于如何解决此问题的指针吗?呃,有关于如何存储块和id的指针吗?我可以处理一条记录两次吗?这可能会比预期的排序更多,因为它将在排序时使用段落的全部内容。您可以在
ARR
中按id存储(并附加到值)为了避免这种情况。谢谢!非常有用。但是,asort函数会打乱文件的顺序。我希望第一个块来自文件1,第二个块来自文件2。但它返回的顺序相反。我尝试对文件重新排序,但没有成功。它看起来像是
asort
函数将数组排序为字符串。因此,如果
file2
中的块是stringwise小于它将首先显示。有关于如何修复此问题的指针吗?呃,有关于如何存储块和id的指针吗?我可以处理一条记录两次吗?谢谢!但是,
::snt
行之后没有多少行(非结构化)。我如何捕获所有这些?谢谢!但是,没有多少行(非结构化)在
#::snt
行之后。我如何捕获所有这些?谢谢!这起作用了。我用更多信息更新了我的问题。但我想你了解了合并方面。如果你有时间,我将非常感谢解释它的工作原理。总是很高兴了解:-)我在最后添加了一个解释。我