将文本文件中的块写入多个新文件 [英] Write blocks in a text file to multiple new files

查看:48
本文介绍了将文本文件中的块写入多个新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取文本文件中的块并将其放入新的单个文件中.例如,考虑以下文件:

I am trying to extract blocks in a text file and put them to new individual files. For example, consider the following file:

some junk lines

ABC: this is abc text
abc block text1
abc block text2
abc block text3

I dont care about this line

Text at start of block. I dont want this line also. 
ABC: this is another abc text
abc block text5
abc block text2
abc block text3
abc block text1

some other dont care line

我对"ABC"区块感兴趣.每个块的开头都有"ABC:",结尾有换行.因此,我想生成包含以下内容的abc1.txt:

I am interested in 'ABC' blocks. Every block has "ABC:" at beginning and new line at the end. So, I want to generate abc1.txt that contains:

ABC: this is abc text
abc block text1
abc block text2
abc block text3

和包含以下内容的abc2.txt:

and abc2.txt that contains:

ABC: this is another abc text
abc block text5
abc block text2
abc block text3
abc block text1

我尝试使用awk来获取块,但是在匹配结尾的新行时遇到了困难.

I tried using awk to get the blocks but having hard time in matching ending new line.

一个选项是编写一个脚本,该脚本循环遍历文件中的每一行.我相信有更好的解决方案.有人可以帮忙吗?预先感谢!

One option is to write a script that loops through each and every line in the file. I believe there is a better solution. Can someone please help? Thanks in advance!

推荐答案

此一线工作即可:

awk '/^ABC/{p=1;close(fn);fn="abc"++i}!NF{p=0}p{print > fn}' file

以您的示例作为输入:

kent$  awk '/^ABC/{p=1;close(fn);fn="abc"++i}!NF{p=0}p{print > fn}' f

kent$  head abc*
==> abc1 <==
ABC: this is abc text
abc block text1
abc block text2
abc block text3

==> abc2 <==
ABC: this is another abc text
abc block text5
abc block text2
abc block text3
abc block text1

注意:

  • 如果您有许多"ABC"块,则必须使用 close(fn),否则会收到诸如打开文件过多"之类的错误消息
  • Note:

    • the close(fn) is necessary, if you have many "ABC" blocks, otherwise you got error msgs like "too many opened files"
    • 这篇关于将文本文件中的块写入多个新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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