将两个图案之间的线条打印到新文件 [英] Print lines between two patterns to new file
本文介绍了将两个图案之间的线条打印到新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮我解决这个问题.我有以下模式的文件
please help me with this. I have file of following pattern
ABC x
bla bla bla
bla bla
bla
XYZ
ABC y
bla bla bla
bla bla
bla bla bla
XYZ
ABC z
bla bla bla
XYZ
我需要在文件 x.txt 中输出
I need output in file x.txt
ABC x
bla bla bla
bla bla
bla
XYZ
和
ABC y
bla bla bla
bla bla
bla bla bla
XYZ
在 y.txt 中等等其他模式
in y.txt and so on for rest of patterns
推荐答案
有些像这样:
awk '/ABC/ {close(f".txt");f=$2} {print >f".txt"}' file
此测试是否以ABC
开头,然后将输出文件名设置为$2
This test if line starts with ABC
and then set output file name to value in $2
埃克斯
cat y.txt
ABC y
bla bla bla
bla bla
bla bla bla
XYZ
将 close()
添加到 awk
以关闭打开的文件,因此如果创建许多文件,它不会耗尽空间
added close()
to awk
to close open file so it does not run out of space if it creates many files
这篇关于将两个图案之间的线条打印到新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文