grep or sed to match a block between start and end using same pattern

Home / Uncategorized / grep or sed to match a block between start and end using same pattern

Question:
I have a file with the following info:start pattern1
line1
line2

end pattern1

If I know what "pattern1" is , I can do
sed ‘/start pattern1/,/end pattern1/p’
but here, I want to match pattern1 and use the same in the end. How can I do that?


Answer:
Using awk to print between pattern1s (inclusive):$ awk ‘/pattern1/{p=!p;print;next} p’ file
pattern1
line1
line2

pattern1
The regex could be defined better, like /^pattern1$/ or $0=="pattern1".
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *