Ankit Kumar
Ankit Kumar

Reputation: 1463

awk giving different behavior on different systems

I am using awk to grep a paragraph containing a specific keyword until the record seperator which in my case is the space.

awk -vRS= /\<keyword2\>/ file.txt

and file.txt contains this

this is the first keyword1 occurance
and line in the first paragraph

this is the second keyword2 occurance
and line in the second paragraph

now the output of the awk command should be

this is the second keyword2 occurance
and line in the second paragraph

but it works only on one of my system. doesn't work on the others. please help

Error found
Installed nawk on Ubuntu 14.04 and does not work.
Works fine with gawk

Upvotes: 0

Views: 135

Answers (3)

Tom Fenech
Tom Fenech

Reputation: 74596

It sounds like you are trying to use GNU awk extensions on an incompatible implementation of awk.

Unfortunately there is no POSIX-standard word boundary marker. Depending on exactly what the text is, you might be able to use:

awk -vRS= '/[[:space:]]keyword2[[:space:]]/' file.txt

Which will match keyword2 with spaces either side. This should work on any implementation of awk.

Upvotes: 0

Jotne
Jotne

Reputation: 41446

You are very close with your 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

You can also try to skip to word boundary:

awk -v RS= '/keyword2/' file

A perl version:

perl -ne 'BEGIN { $/="\n\n" }; print if $_ =~ /keyword2/;' file

sed version

sed -e '/./{H;$!d;}' -e 'x;/\<keyword2\>/!d;' file

Another data
this is the second keyword2 occurance
and line in the second paragraph

or

sed -e '/./{H;$!d;}' -e 'x;/keyword2/!d;' file

Upvotes: 1

perreal
perreal

Reputation: 97918

You can try sed:

sed -n '/\<keyword2\>/,/^$/p' file.txt

Upvotes: 0

Related Questions