根据模式将文件拆分为多个文件并通过 Unix 中的搜索模式命名新文件? [英] Split a file into multiple files based on a pattern and name the new files by the search pattern in Unix?

查看:24
本文介绍了根据模式将文件拆分为多个文件并通过 Unix 中的搜索模式命名新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据搜索模式拆分文件,生成的新文件的文件名将与用于拆分文件的搜索模式相同.

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

它生成的文件名称为 F1、F2 等.但我希望文件名等于搜索模式行.

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

然后新文件应该按照搜索模式 GROUP* & 进行拆分.重命名为 GROUP1、GROUP2 和 GROUP3,包含各自的内容.

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?

推荐答案

尝试:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆