如何一起grep多个模式

时间:2013-09-11 20:38:52

标签: awk grep

我有一个文件,其中包含一些信息,并且具有与以下内容类似的结构。 注意:请假设------作为不需要的文字。这是我不关心的其他信息。

==== 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
-------
-------

我有兴趣通过grep或其他方式获取以下信息:

==== 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

我该怎么做?

2 个答案:

答案 0 :(得分:3)

我假设每个块中的文本都是动态的。但你有固定的行号,那么这适合你:

awk '/^====/{print;for(i=1;i<=3;i++)getline;x=0;next}{++x}x<=5' 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

答案 1 :(得分:2)

尝试这样做:

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