theuniverseisflat
theuniverseisflat

Reputation: 881

Awk Sed Grep parsing text file to match a pattern

I need to run this from command prompt to display only those lines that match the pattern after the words beside the sign# Example #IP.
So if I'm looking for IP, I want to print the lines under the section #IP. In this case I will print only 3 lines. This is a large file and has lots of sections starting with #.

Can you kindly help with this request?

#IP Source 
1  is for 1
2  is for 2
3  3 for 3
#BASE
4 is for 4
5 is for 5

Upvotes: 0

Views: 358

Answers (2)

Cyrus
Cyrus

Reputation: 88583

Try this with GNU sed:

sed -n '/IP/,/#/{/#/d;p;}' file

Output:

1  is for 1
2  is for 2
3  3 for 3

Upvotes: 2

geirha
geirha

Reputation: 6181

Here's an awk solution:

awk '/^#IP/{f=1;next} /^#/{f=0} f{print}' file

Some explanations of each condition { action } pair:

/^#IP/{f=1;next} - if the line starts with #IP, set variable f to 1 (true), then (next) skip further processing of this line.

/^#/{f=0} - if the line starts with a #, set variable f to 0 (false)

f{print} - if the variable f is true (not empty and not 0), print the current line.

Upvotes: 0

Related Questions