简体   繁体   English

如何从合并的文件中提取文件

[英]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.

相关问题 如何从不同文件中提取特定列并在一个文件中输出? - How to extract specific columns from different files and output in one file? 如何组合目录中的所有文件,将它们各自的文件名添加为最终合并文件中的新列 - How to combine all files in a directory, adding their individual file names as a new column in final merged file 如何使用Shell脚本从2个不同的文件中提取字段并将其存储在输出文件中? - How to extract fields from 2 different files and store in an output file using shell script? 如何将文本文件拆分为多个文件并从行前缀提取文件名? - How to split text file into multiple files and extract filename from line prefix? 如何从文件列表中提取文件名? - How to extract file name from file list? 使用AWK连接两个文件的某些行,然后对合并的文件进行排序 - using AWK to join certain lines of two files and then sort the merged file 从多个文件中提取列 &#39;x&#39;,并用 &#39;x&#39; 转置文件名 - Extract column 'x' from multiple files, and transpose file name with 'x' 从多个文件(结构输出)中提取数据并打印到一个文件 - Extract data from multiple files (Structure outputs) and printing to one file 从文本文件中提取行并将其保存在单独的文件中 - extract rows from text file and save it in separate files 将目录文件中的特定列提取到新文件中 - Extract specific column from files of a directory into a new file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM