我有一个有点大的输出文本文件,我需要删除两个模式之间的所有行,但保持模式匹配.
这些文件看起来像以下输出一样模糊.
TEST #1
coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
indicator |
0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
year |
2 | -.4324005 2.231387 -0.19 0.847 -4.836829 3.972028
3 | -.362762 1.97184 -0.18 0.854 -4.254882 3.529358
|
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
TEST #2
coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
year |
4 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
5 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
idnumber |
6 | -.4324005 2.231387 -0.19 0.847 -4.836829 3.972028
7 | -.362762 1.97184 -0.18 0.854 -4.254882 3.529358
|
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
Run Code Online (Sandbox Code Playgroud)
我需要采取以下输出并删除"year"和"_cons"之间的所有行,但我需要保留以"_cons"开头的行.所需的输出如下:
TEST #1
coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
indicator |
0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
TEST #2
coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
Run Code Online (Sandbox Code Playgroud)
我编写了以下脚本(在OS X下):
sed '/^ +year/,/^ +_cons/{/^ +year/!{/^ +_cons/!d}}' input.txt >output.txt
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
sed: 1: "/^ +year/,/^ +_cons/{/^ ...": extra characters at the end of d command
Run Code Online (Sandbox Code Playgroud)
我不确定这种方法是否正确,因为我似乎无法执行.在这里甚至是合适的还是我应该使用awk?
最后一点,我需要这个脚本来处理相对通用的Unix安装.我必须将此发送给必须在非常基本的AIX(我认为)安装下执行它的人.没有perl,没有python,我无法通过电子邮件对其安装进行太多故障排除.
这应该工作 -
awk '/year/{print; getline; while($0!~/_cons/) {getline}}1' INPUT_FILE
Run Code Online (Sandbox Code Playgroud)
要么
awk '/_cons/{print;f=0;next}/year/{f=1;print;next}f{next}1' INPUT_FILE
Run Code Online (Sandbox Code Playgroud)
[jaypal:~/Temp] awk '/year/{print; getline; while($0!~/_cons/) {getline}}1' file
TEST #1
coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
indicator |
0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
TEST #2
coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
Run Code Online (Sandbox Code Playgroud)
[jaypal:~/Temp] awk '/_cons/{print;f=0;next}/year/{f=1;print;next}f{next}1' file
TEST #1
coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
indicator |
0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
TEST #2
coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
Run Code Online (Sandbox Code Playgroud)
尝试在后面添加分号d以指示命令已结束。(GNU——我sed唯一sed可以方便测试的——不需要这个,但也许另一个sed会?)
另外,如果您需要支持 的多个实现sed,那么您不能使用+表示“一个或多个”:它不是标准的,并且并非所有实现都支持它。你可以使用\{1,\},但是那很丑陋。。。我只是使用*并附加一份额外的副本。
所以:
sed '/^ * year/,/^ * _cons/{/^ * year/!{/^ * _cons/!d;}}' input.txt >output.txt
Run Code Online (Sandbox Code Playgroud)
(已测试,但仅使用 GNU sed,而不是 OS X,当然也不是 AIX,抱歉。)