[英]How to extract files from a merged file
I want to separate a merged file into two files. 我想将合并的文件分成两个文件。 The file:
文件:
file.dat
i =100
1 2 3
i =1
-1 -2 -3
i =101
1 2 3
i =102
1 2 3
i =103
1 2 3
i =2
-1 -2 -3
....
The mixed indices are 混合指数是
1,2,3,4, ...,99
and 和
100, 101, 102, 103,...,200.
The indices appear alternately, but there is no rule. 索引交替显示,但是没有规则。 The data
数据
1 2 3
and 和
-1 -2 -3
just denote the data block in each step. 仅表示每个步骤中的数据块。
Could you give an idea to separate the merged file into two files with respect to the indices? 您能否给出一个关于索引将合并文件分为两个文件的想法?
If you just want the data blocks appended to two different files, depending on which group of indexes it belongs to, this should work: 如果只希望将数据块附加到两个不同的文件中,这取决于它属于哪个索引组,则应该可以:
# separate.awk
{
if ($1 == "i")
{
split($2,a,"=");
i = a[2];
}
if (i < 100)
print > "1-99.dat";
else
print > "100-200.dat"
}
$ awk -f separate.awk file.dat
$ cat 1-99.dat
i =1
-1 -2 -3
i =2
-1 -2 -3
$ cat 100-200.dat
i =100
1 2 3
i =101
1 2 3
i =102
1 2 3
i =103
1 2 3
This awk
should do it for you: 这个
awk
应该为您做:
awk -F= '/=/{f="a.txt";if($2>99)f="b.txt";next} {print >f}' file.dat
First, it sets the field separator to =
. 首先,将字段分隔符设置为
=
。 Then it checks if the line contains an equals sign, and if so, it is time to set the name of the output file to either "a.txt" or "b.txt" depending on the number after the equals sign. 然后,它检查该行是否包含等号,如果是,则应根据等号后的数字将输出文件的名称设置为“ a.txt”或“ b.txt”。 Then on subsequent records we just write to the file we last selected.
然后在随后的记录中,我们只写到最后选择的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.