我使用awk grep包含特定关键字的段落,直到记录分隔符,在我的情况下是空格。
awk -vRS= /\<keyword2\>/ file.txt
和file.txt包含此
this is the first keyword1 occurance
and line in the first paragraph
this is the second keyword2 occurance
and line in the second paragraph
现在awk
命令的输出应为
this is the second keyword2 occurance
and line in the second paragraph
但它仅适用于我的一个系统。不适用于其他人。请帮忙
发现错误
在Ubuntu 14.04上安装nawk
并且无法正常工作
适用于gawk
答案 0 :(得分:1)
您与awk
cat file
More data
this is the first keyword1 occurance
and line in the first paragraph
Another data
this is the second keyword2 occurance
and line in the second paragraph
awk -v RS= '/\<keyword2\>/' file
Another data
this is the second keyword2 occurance
and line in the second paragraph
您也可以尝试跳到单词边界:
awk -v RS= '/keyword2/' file
perl
版本:
perl -ne 'BEGIN { $/="\n\n" }; print if $_ =~ /keyword2/;' file
sed
版
sed -e '/./{H;$!d;}' -e 'x;/\<keyword2\>/!d;' file
Another data
this is the second keyword2 occurance
and line in the second paragraph
或
sed -e '/./{H;$!d;}' -e 'x;/keyword2/!d;' file
答案 1 :(得分:0)
您可以尝试sed
:
sed -n '/\<keyword2\>/,/^$/p' file.txt
答案 2 :(得分:0)
听起来你正试图在不兼容的awk实现上使用GNU awk extensions。
不幸的是,没有POSIX标准的单词边界标记。根据文本的确切内容,您可以使用:
awk -vRS= '/[[:space:]]keyword2[[:space:]]/' file.txt
哪个匹配关键字2和空格。这应该适用于awk的任何实现。