根据模式将文件分割成多个文件并在Unix中用搜索模式命名新文件? [英] Split a file into multiple files based on a pattern and name the new files by the search pattern in Unix?
问题描述
如何根据搜索模式分割文件,并且将生成的新文件的文件名与用于分割文件的搜索模式相同。
目前,我正在使用以下命令来分割文件。
awk'/ ^ GROUP * / {x =F++ i;} {print> x;}'cdw_all_jobs_reduced3.txt
它以F1,F2等名称生成文件。但我希望文件的名称与搜索模式行相同。
即如果在我的原始文件中有以下内容:
GROUP1
xyz
pqr
GROUP2
abc
GROUP3
sdlsa
然后新文件应该是按搜索模式划分GROUP *&换句话说,我如何自定义生成(重命名)将基于文件名拆分的文件搜索模式?
code> awk'/ ^ GROUP [0-9] + $ / {x = $ 0; next} {print> x;}'cdw_all_jobs_reduced3.txt
如果您希望文件名移除下一条语句:
awk'/ ^ GROUP [0-9] + $ / {x = $ 0} {print> x;}'cdw_all_jobs_reduced3.txt
How can I split a file based on a search pattern and the new files that will be generated will have the file names as equal to the search pattern which was used to split the file.
Currently, I'm using the following command to split files.
awk '/^GROUP*/{x="F"++i;}{print > x;}' cdw_all_jobs_reduced3.txt
It generated files by the names as F1, F2 and so on. But I want the names of files to be equal to the search pattern line.
i.e. If in my original file I have the following:
GROUP1
xyz
pqr
GROUP2
abc
GROUP3
sdlsa
Then the new files should be split by the search pattern GROUP* & renamed as GROUP1, GROUP2 and GROUP3 containing their respective content.
In other words, how do I custom generate(rename) the names of the files that will be split based on the search pattern?
Try:
awk '/^GROUP[0-9]+$/{x=$0;next}{print > x;}' cdw_all_jobs_reduced3.txt
If you want the "filenames" remove next statement:
awk '/^GROUP[0-9]+$/{x=$0}{print > x;}' cdw_all_jobs_reduced3.txt
这篇关于根据模式将文件分割成多个文件并在Unix中用搜索模式命名新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!