AWK:显示匹配正则表达式(多) [英] awk: Display matched regex (multiline)
问题描述
我有一个看起来像这样的文件:
I have a file that looks like this:
SF:/home/developer/project/test/resources/somefile.js
DA:1,2
DA:3,2
end_of_record
SF:/home/developer/project/src/resources/otherfile.js
DA:9,2
DA:15,2
DA:22,2
end_of_record
...some more SF:/home/xxx and end_of_record lines...
该文件包括与开始块SF的:...
和 end_of_record
结束。请注意,行的计数其间( DA,则x
)可以是不同的。我想打印在第一行字符串测试的所有块(如SF:/家庭/开发/ 测试 /资源/ ...在这里)。在这个例子中,我想要的输出将是:
The file consists of blocks beginning with SF: ...
and ending with end_of_record
. Note that the count of the lines inbetween (DA:x,x
) can be different. I want to print all blocks that have the String "test" in its first line (like "SF:/home/developer/test/resources/..." here). For this example, my wanted output would be:
SF:/home/developer/project/test/resources/somefile.js
DA:1,2
DA:3,2
end_of_record
我想这样做在的Linux 环境。
我的第一次尝试是这样使用sed的命令的事,但一些研究似乎像AWK后做多行操作的更合适的工具。
My first try was to do this using the "sed" command, but after some research it seemed like "awk" is a more suitable tool for doing multiline operations.
使用AWK和正则表达式,这是我到目前为止的命令:
Using awk and Regex, this is the command that I have so far:
awk '/SF[:\/a-zA-Z0-9]*test[\/A-Za-z0-9.,:\n]*end_of_record/ {print}' FS="\n" RS="" examplefile
但它输出完整的 examplefile
,不仅包含测试在第一线的块。我不知道如果我的正则表达式是错误的,或者如果我错过在我的 AWK
电话什么的。
But it outputs the full examplefile
, not only the blocks containing "test" in the first line. I am not sure if my Regex is wrong or if I am missing something in my awk
call.
我怎样才能得到只有阻滞在第一行测试?
How can I get only the block with "test" in the first line?
推荐答案
您需要一个标志:
awk '/^SF.*test.*/{f=1}f;/end_of_record/{f=0}' yourFile
这篇关于AWK:显示匹配正则表达式(多)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!