我有一个看起来像这样的文件:
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,x)可以不同.我想在第一行打印所有具有字符串“test”的块(例如“SF:/ home / developer / test / resources / …”).对于此示例,我想要的输出将是:
SF:/home/developer/project/test/resources/somefile.js
DA:1,2
DA:3,2
end_of_record
我想在Linux环境中这样做.
我的第一次尝试是使用“sed”命令执行此操作,但经过一些研究后,似乎“awk”是一个更适合进行多行操作的工具.
使用awk和Regex,这是我到目前为止的命令:
awk '/SF[:\/a-zA-Z0-9]*test[\/A-Za-z0-9.,:
]*end_of_record/ {print}' FS="
" RS="" examplefile
但它输出完整的examplefile,而不仅是第一行中包含“test”的块.我不确定我的正则表达式是否错误,或者我的awk通话中是否遗漏了某些内容.
如何在第一行中只获得带有“test”的块?