打印与awk脚本匹配的文件名 [英] Printing name of files that match awk script
问题描述
根据我之前的问题
我有多个文本文件,可能有也可能没有用虚线包围的重复文本组.所有lorem ipsum文本都不应包含在输出中.
I have multiple text files that may or may not have repeating groups of text surrounded by dashed lines. All the lorem ipsum text should not be included in the output.
$ cat /tmp/testAwk/file1.txt
--------------
important text one
important text two
--------------
Lorem ipsum dolor sit amet
consectetur adipiscing elit
--------------
important text three
important text four
--------------
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua
Ut enim ad minim veniam
quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat
$ cat /tmp/testAwk/file2.txt
Duis aute irure dolor in reprehenderit
--------------
important text one
important text two
--------------
in voluptate velit esse cillum dolore
eu fugiat nulla pariatur
non proident, sunt
--------------
important text three
important text four
--------------
Excepteur sint occaecat cupidatat
$ cat /tmp/testAwk/file3.txt
consequuntur magni dolores
sed quia non numquam
Quis autem vel eum iure reprehenderit
我正在尝试使用awk
捕获--------------
的两行之间的文本,并打印出与该模式匹配的文件名.
I am trying to use awk
to capture the text between the two lines of --------------
and print out the names of files that match the pattern.
我从@Ed Morton收到了我以前的问题的精彩答复: https://stackoverflow.com/a/55507707/257233
I took the fantastic reply from @Ed Morton to my previous question: https://stackoverflow.com/a/55507707/257233
awk '{x=sub(/^-+$/,"")} f; x{f=!f}' *.txt
我试图对其进行调整,以打印出与模式匹配并缩进结果的那些文件的文件名.我无法弄清楚如何在awk
中完成全部工作,因此最终在其中也添加了grep
和sed
.
I tried to adapt it to print out the file names of those files that match the pattern and indent the results. I couldn't work out how to do the whole job in awk
, so I ended up with some grep
and sed
in there as well.
$ awk 'FNR==1{print FILENAME} {x=sub(/^-+$/,"---")} f; x{f=!f}' $(grep -E '^-+$' /tmp/testAwk/*.txt -l) | sed -re 's/^([^\/])/ \1/'
/tmp/testAwk/file1.txt
important text one
important text two
---
important text three
important text four
---
/tmp/testAwk/file2.txt
important text one
important text two
---
important text three
important text four
---
我可以仅使用awk来执行上述操作吗?
Can I do the above just with awk?
推荐答案
这就是我要这样做的方式,尤其是因为您的用例似乎正在发展,需要更多功能,因此将其塞入简短的单行代码中并不是最佳方法:
Here's how I'd do it, especially since your use case seems to be evolving to require more functionality so cramming it into a brief one-liner isn't the best approach:
$ cat tst.awk
FNR==1 { delimCnt=inBlock=block="" }
/^-+$/ {
inBlock = (++delimCnt % 2)
if ( !inBlock ) {
if (delimCnt > 1) {
if (delimCnt == 2) {
print FILENAME
}
print block " ---"
}
block = ""
}
next
}
inBlock { block = block " " $0 ORS }
.
$ awk -f tst.awk file1.txt file2.txt file3.txt
file1.txt
important text one
important text two
---
important text three
important text four
---
file2.txt
important text one
important text two
---
important text three
important text four
---
这篇关于打印与awk脚本匹配的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!