0

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

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

我怎样才能做到这一点?

4

2 回答 2

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
于 2013-09-11T21:12:34.513 回答
2

尝试这样做:

grep -E '^(==== Start of Info for Employ|Start of Address information|Street No|City Zip|End)' file
于 2013-09-11T20:43:48.547 回答