Yukinon
Yukinon

Reputation: 1

print a range of lines if they match a pattern

My input

1
abc

1cde
efg
xxx

1
abc
pattern1
pattern2

efg
xxx

1
abc
cde
efg
xxx

my expected output (print from 1 it contains pattern1 and 2):

1
abc
pattern1
pattern2

efg
xxx

I have so for:

sed -n '/^1/ {x;/pattern1/ {N;/\n.*pattern2/p};d} $/^1/ {h;/pattern1/ {N;/\n.*pattern2/p};d}}H' My file

BTW my file is a very big file, please show me a method that can do it quickly. Thanks so much.

Upvotes: 0

Views: 101

Answers (1)

Ed Morton
Ed Morton

Reputation: 204015

sed is for s/old/new/ - that is all. For anything else you should be using awk.

It looks like your expected output can't actually be produced from your sample input so it's a guess and untested since we don't have anything concrete to test against but it sounds like you might want:

awk -v RS= -v ORS='\n\n' '/pattern1/ && /pattern2/' file

Upvotes: 3

Related Questions