0

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.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Rajiv Rai
  • 235
  • 4
  • 16
  • 5
    Why do you have an exact same question (unaccepted with an answer available) http://stackoverflow.com/questions/40586948/shell-script-to-check-if-a-paragraph-stream-of-lines-exist-in-a-file? again? – Inian Nov 16 '16 at 07:06
  • http://stackoverflow.com/questions/3717772/regex-grep-for-multi-line-search-needed ; http://stackoverflow.com/questions/2686147/how-to-find-patterns-across-multiple-lines-using-grep – Ipor Sircer Nov 16 '16 at 07:08

2 Answers2

1

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
SLePort
  • 15,211
  • 3
  • 34
  • 44
0

You can use awk:

awk "/Published 3EO's/ && /Save completed/ && /and so on.../" RS='' file.txt

Setting RS to an empty string is a special feature. It will split the text into paragraphs.

From man awk:

If RS is set to the null string, then records are separated by blank lines. When RS is set to the null string, the newline character always acts as a field separator, in addition to whatever value FS may have.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266