SeattleOrBayArea
SeattleOrBayArea

Reputation: 3118

How to grep multiple patterns together

I have a file which has text with some information and has similar structure to the following. Note: Please assume ------ as unwanted text. This is other information that I don't care for.

==== Start of Info for Employee 1 ====
--------
--------
--------
Start of Address information for employee 1
Street No
City
Zip
End of Address information
-------
-------
==== Start of Info for Employee 2 ====
--------
--------
--------
Start of Address information for employee 2
Street No
City
Zip
End of Address information
-------
-------
==== Start of Info for Employee 3 ====
--------
--------
--------
Start of Address information for employee 3
Street No
City
Zip
End of Address information
-------
-------

I am interested in getting the following information by doing a grep or some other way:

==== Start of Info for Employee 1 ====
Start of Address information for employee 1
Street No
City
Zip
End of Address information
==== Start of Info for Employee 2 ====
Start of Address information for employee 2
Street No
City
Zip
End of Address information
==== Start of Info for Employee 3 ====
Start of Address information for employee 3
Street No
City
Zip
End of Address information

How can I do this?

Upvotes: 0

Views: 262

Answers (2)

Kent
Kent

Reputation: 195039

I assume your text in each block could be dynamic. but you have fixed line numbers, then this works for you:

awk '/^====/{print;for(i=1;i<=3;i++)getline;x=0;next}{++x}x<=5' file

test with your file:

kent$  awk '/^====/{print;for(i=1;i<=3;i++)getline;x=0;next}{++x}x<=5' f
==== Start of Info for Employee 1 ====
Start of Address information for employee 1
Street No
City
Zip
End of Address information
==== Start of Info for Employee 2 ====
Start of Address information for employee 2
Street No
City
Zip
End of Address information
==== Start of Info for Employee 3 ====
Start of Address information for employee 3
Street No
City
Zip
End of Address information

Upvotes: 3

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185025

Try doing this :

grep -E '^(==== Start of Info for Employ|Start of Address information|Street No|City Zip|End)' file

Upvotes: 2

Related Questions