Rajiv Rai Rajiv Rai - 17 days ago 5
Linux Question

Shell script to check if a paragraph exist in a file

I have a file,

Published 3EO's
Save completed
Trade saving save successful for 123

Published 3EO's
Save completed
Trade saving save successful for 234



Published 4EO's
Save completed
Trade saving save successful for trade
5666688|000|b


My Question is I want to write a script which checks that following paragraph exist or not also I want to store the paragraph in a variable for comparision.

Published 3EO's
Save completed
Trade saving save successful for 123


I have written like this

cat $file | grep -A 2 "Published 3EO's"


where $file contains the original paragraphs.

The problem is, It Returns more than two value, 1st and 2nd Paragraph.
But I want only one paragraph for which i am looking a match.

Any help will be appreciated.

Answer

With sed:

$ output=$(sed -n "/Published 3EO's/N;{/Save completed/N;{/Trade saving save successful for 123/p}}" file)
$ echo "$output"
Published 3EO's
Save completed
Trade saving save successful for 123
Comments