如何使用'sed'命令行工具在两个字符串之间打印行?

时间:2012-08-20 09:59:41

标签: sed

假设我有一个以下文本文件,其中包含:

Non-Leaf Node:  level=1  count=27  address=52
branch 0 : rect id 1
rect:   -117.035332 -114.738449
    32.269550   32.711048
branch 1 : rect id 2
rect:   -117.037956 -114.758972
    32.245251   32.684460
branch 2 : rect id 3
rect:   -117.010262 -114.773041
    32.249630   32.674690
branch 3 : rect id 4
rect:   -117.054260 -114.791054
    32.272289   32.668911
branch 4 : rect id 5
rect:   -117.119293 -113.539886
    32.048962   32.542961
branch 5 : rect id 6
rect:   -116.345711 -114.758499
    31.519569   32.546711
branch 6 : rect id 7
rect:   -116.901352 -113.503807
    32.045238   32.549450
branch 7 : rect id 8
rect:   -117.066643 -114.961197
    31.356871   32.616192
branch 8 : rect id 9
rect:   -116.901352 -114.488243
    31.528259   32.572491
branch 9 : rect id 10
rect:   -116.915199 -113.516113
    32.046570   32.574520
branch 10 : rect id 11
rect:   -117.060387 -114.771698
    31.465639   32.532761
branch 11 : rect id 12
rect:   -117.047707 -113.531639
    32.047310   32.524540
branch 12 : rect id 13
rect:   -116.859573 -113.473984
    32.002258   32.415321
branch 13 : rect id 14
rect:   -116.876701 -113.415466
    32.000000   32.369591
branch 14 : rect id 15
rect:   -116.867813 -112.714043
    31.373249   32.358768
branch 15 : rect id 16
rect:   -116.668228 -113.651627
    31.995630   32.462391
branch 16 : rect id 17
rect:   -116.904770 -112.831932
    31.324810   32.335918
branch 17 : rect id 18
rect:   -116.682022 -113.454338
    32.000000   32.532761
branch 18 : rect id 19
rect:   -116.862717 -113.483856
    32.000000   32.473270
branch 19 : rect id 20
rect:   -116.358490 -113.359070
    32.000000   32.515930
branch 20 : rect id 21
rect:   -116.430298 -113.358810
    32.000000   32.464859
branch 21 : rect id 22
rect:   -116.663437 -113.639427
    32.000671   32.442909
branch 22 : rect id 23
rect:   -116.749977 -111.565224
    31.380131   32.152309
branch 23 : rect id 25
rect:   -116.668083 -115.258987
    31.253040   32.000671
branch 24 : rect id 66
rect:   -117.060562 -115.442619
    31.334410   32.573971
branch 25 : rect id 67
rect:   -113.639427 -110.942093
    31.297899   32.062290
branch 26 : rect id 69
rect:   -116.669258 -114.795250
    31.304890   32.131001
Leaf Node:  level=0  count=23  address=1
Non-Leaf Node:  level=1  count=31  address=53
branch 0 : rect id 42
rect:   -115.768539 -106.251556
    31.425039   31.717550
branch 1 : rect id 50
rect:   -109.559479 -106.009361
    31.296721   31.775299
branch 2 : rect id 51
rect:   -110.937401 -106.226143
    31.285870   31.771971
branch 3 : rect id 54
rect:   -109.584412 -106.069092
    31.285240   31.775230
branch 4 : rect id 56
rect:   -109.570961 -106.000954
    31.296721   31.780769
branch 5 : rect id 58
rect:   -115.806213 -106.366188
    31.400450   31.687519
branch 6 : rect id 59
rect:   -113.173859 -106.244057
    31.297440   31.627750
branch 7 : rect id 60
rect:   -115.811478 -106.278252
    31.400450   31.679470
branch 8 : rect id 61
rect:   -109.953888 -106.020111
    31.325319   31.775270
branch 9 : rect id 64
rect:   -113.070969 -106.015968
    31.331841   31.704750
branch 10 : rect id 68
rect:   -113.065689 -107.034576
    31.326300   31.770809
branch 11 : rect id 71
rect:   -112.333344 -106.059860
    31.284081   31.662920
branch 12 : rect id 73
rect:   -115.071083 -106.309677
    31.267879   31.466850
branch 13 : rect id 74
rect:   -116.094414 -106.286308
    31.236290   31.424770
branch 14 : rect id 75
rect:   -115.423264 -106.286308
    31.229691   31.415510
branch 15 : rect id 76
rect:   -116.111656 -106.313110
    31.259390   31.478300
branch 16 : rect id 77
rect:   -116.247467 -106.309677
    31.240231   31.451799
branch 17 : rect id 78
rect:   -116.170792 -106.094543
    31.156429   31.391781
branch 18 : rect id 79
rect:   -116.225723 -106.292709
    31.239960   31.442850
branch 19 : rect id 80
rect:   -116.268013 -105.769913
    31.157240   31.378111
branch 20 : rect id 82
rect:   -116.215424 -105.827202
    31.198441   31.383421
branch 21 : rect id 83
rect:   -116.095734 -105.826439
    31.197460   31.373819
branch 22 : rect id 84
rect:   -115.423264 -105.815018
    31.182640   31.368891
branch 23 : rect id 85
rect:   -116.221527 -105.776512
    31.160931   31.389830
branch 24 : rect id 86
rect:   -116.203369 -106.473831
    31.168350   31.367611
branch 25 : rect id 87
rect:   -115.727631 -106.501587
    31.189100   31.395941
branch 26 : rect id 88
rect:   -116.237289 -105.790756
    31.164780   31.358959
branch 27 : rect id 89
rect:   -115.791344 -105.990044
    31.072620   31.349529
branch 28 : rect id 90
rect:   -115.736847 -106.495079
    31.187969   31.376900
branch 29 : rect id 91
rect:   -115.721710 -106.000130
    31.160351   31.354601
branch 30 : rect id 92
rect:   -115.792236 -106.000793
    31.166620   31.378811
Leaf Node:  level=0  count=21  address=42
Non-Leaf Node:  level=1  count=31  address=81
branch 0 : rect id 24
rect:   -116.844414 -112.145180
    31.498260   32.060459
branch 1 : rect id 26
rect:   -115.951759 -106.352959
    31.681940   32.000000
branch 2 : rect id 27
rect:   -116.743240 -112.668854
    31.446930   32.000000
branch 3 : rect id 28
rect:   -116.700821 -111.622360
    31.411551   31.976200
branch 4 : rect id 29
rect:   -116.592079 -112.608749
    31.395941   32.000000
branch 5 : rect id 30
rect:   -116.657410 -105.197319
    31.693340   32.000000
branch 6 : rect id 31
rect:   -116.717651 -112.397774
    31.297131   31.934071
branch 7 : rect id 32
rect:   -116.577538 -112.635239
    31.310921   31.984739
branch 8 : rect id 33
rect:   -115.213539 -105.196251
    31.672661   32.000000
branch 9 : rect id 34
rect:   -116.605301 -111.706429
    31.455721   31.930630
branch 10 : rect id 35
rect:   -116.598053 -112.377373
    31.438101   31.998800
branch 11 : rect id 36
rect:   -116.483093 -112.499519
    31.382000   31.980379
branch 12 : rect id 37
rect:   -116.611397 -106.342651
    31.676640   31.975651
branch 13 : rect id 38
rect:   -116.729889 -111.579712
    31.384859   31.918150
branch 14 : rect id 39
rect:   -116.563179 -111.683273
    31.431530   31.937929
branch 15 : rect id 40
rect:   -116.607269 -112.358627
    31.441401   31.998569
branch 16 : rect id 41
rect:   -116.408211 -106.430351
    31.519779   31.775270
branch 17 : rect id 43
rect:   -116.382568 -111.548553
    31.449320   31.918150
branch 18 : rect id 44
rect:   -116.453720 -106.419258
    31.519550   31.775330
branch 19 : rect id 45
rect:   -116.606407 -112.181351
    31.428320   31.961201
branch 20 : rect id 46
rect:   -116.453720 -107.823608
    31.458740   31.742060
branch 21 : rect id 47
rect:   -116.643982 -106.447441
    31.483940   31.745359
branch 22 : rect id 48
rect:   -116.603752 -111.648804
    31.421570   31.862720
branch 23 : rect id 49
rect:   -116.514053 -111.551590
    31.353201   31.810820
branch 24 : rect id 55
rect:   -116.650948 -111.595901
    31.303341   31.809580
branch 25 : rect id 57
rect:   -116.700432 -111.744400
    31.416981   31.865299
branch 26 : rect id 62
rect:   -116.323624 -109.556908
    31.297440   31.638611
branch 27 : rect id 63
rect:   -116.591187 -109.210472
    31.320740   31.628071
branch 28 : rect id 65
rect:   -116.231857 -106.018204
    31.298290   31.481331
branch 29 : rect id 70
rect:   -116.246323 -107.546043
    31.279659   31.461029
branch 30 : rect id 72
rect:   -116.276016 -108.424301
    31.308680   31.519550
Leaf Node:  level=0  count=26  address=24
Non-Leaf Node:  level=1  count=31  address=113
branch 0 : rect id 125
rect:   -116.062019 -105.923248
    30.836700   31.122910
branch 1 : rect id 126
rect:   -116.057999 -105.870377
    30.824100   31.116961
branch 2 : rect id 127
rect:   -115.716652 -105.494858
    30.750620   31.033270
branch 3 : rect id 128
rect:   -114.730713 -105.870377
    30.724689   31.056841
branch 4 : rect id 129
rect:   -114.825920 -105.978859
    30.708509   31.079599
branch 5 : rect id 130
rect:   -110.970833 -105.418823
    30.683910   31.201090
branch 6 : rect id 131
rect:   -116.011887 -105.505829
    30.681511   30.994631
branch 7 : rect id 132
rect:   -116.142776 -105.322853
    30.676550   30.944139
branch 8 : rect id 133
rect:   -115.983566 -105.340584
    30.734440   31.018629
branch 9 : rect id 134
rect:   -116.005539 -105.337570
    30.742010   30.994040
branch 10 : rect id 135
rect:   -115.228737 -105.564430
    30.742500   31.047960
branch 11 : rect id 136
rect:   -116.224113 -105.292313
    30.773220   31.018539
branch 12 : rect id 137
rect:   -116.236526 -105.333473
    30.754471   31.030479
branch 13 : rect id 138
rect:   -116.211700 -105.333473
    30.683491   30.981649
branch 14 : rect id 139
rect:   -116.160454 -105.405853
    30.685789   30.986340
branch 15 : rect id 140
rect:   -114.825920 -105.275978
    30.746550   31.004930
branch 16 : rect id 141
rect:   -114.825394 -105.859383
    30.686991   31.010651
branch 17 : rect id 142
rect:   -114.726151 -105.304848
    30.689159   30.988810
branch 18 : rect id 143
rect:   -115.987747 -105.330299
    30.701000   30.969620
branch 19 : rect id 144
rect:   -114.813454 -109.000000
    30.565340   31.207520
branch 20 : rect id 146
rect:   -114.781357 -109.057899
    30.725670   31.217340
branch 21 : rect id 147
rect:   -114.798599 -108.642197
    30.586399   31.168140
branch 22 : rect id 148
rect:   -115.963821 -105.353859
    30.722370   30.998461
branch 23 : rect id 149
rect:   -116.019737 -107.013718
    30.692369   30.998461
branch 24 : rect id 150
rect:   -115.987778 -106.194839
    30.680531   30.956421
branch 25 : rect id 151
rect:   -110.962387 -105.032944
    30.512711   30.911329
branch 26 : rect id 152
rect:   -114.776154 -106.039482
    30.533449   30.943800
branch 27 : rect id 153
rect:   -114.756538 -105.767838
    30.608200   30.921181
branch 28 : rect id 154
rect:   -114.768997 -106.066727
    30.622959   30.940580
branch 29 : rect id 155
rect:   -114.727547 -106.233871
    30.437620   30.903721
branch 30 : rect id 157
rect:   -114.702766 -107.237587
    30.416451   30.915380
Leaf Node:  level=0  count=33  address=125
Non-Leaf Node:  level=1  count=31  address=145
branch 0 : rect id 93
rect:   -116.349747 -108.026497
    31.030939   31.331841
branch 1 : rect id 94
rect:   -116.304611 -106.708038
    31.131109   31.379641
branch 2 : rect id 95
rect:   -112.449677 -106.038094
    31.019670   31.370770
branch 3 : rect id 96
rect:   -115.844307 -107.371758
    30.972601   31.343229
branch 4 : rect id 97
rect:   -116.292664 -106.085373
    31.124689   31.350660
branch 5 : rect id 98
rect:   -116.367180 -106.967934
    31.047510   31.290630
branch 6 : rect id 99
rect:   -116.275307 -108.491081
    30.974831   31.327230
branch 7 : rect id 100
rect:   -116.367180 -106.957008
    31.008089   31.270849
branch 8 : rect id 101
rect:   -116.364403 -107.918480
    30.972820   31.256411
branch 9 : rect id 102
rect:   -116.272133 -107.988899
    30.976681   31.334410
branch 10 : rect id 103
rect:   -116.365402 -107.893059
    30.972601   31.277880
branch 11 : rect id 104
rect:   -116.200783 -106.824043
    30.964640   31.236290
branch 12 : rect id 105
rect:   -115.325272 -106.829910
    31.047960   31.320009
branch 13 : rect id 106
rect:   -116.192703 -106.894081
    30.961340   31.216749
branch 14 : rect id 107
rect:   -116.176559 -106.824043
    30.961340   31.202200
branch 15 : rect id 108
rect:   -116.295670 -106.038094
    30.947639   31.214199
branch 16 : rect id 109
rect:   -115.937958 -105.756721
    30.963070   31.208099
branch 17 : rect id 110
rect:   -115.595337 -105.736366
    30.998461   31.255560
branch 18 : rect id 111
rect:   -116.203499 -105.582573
    30.997290   31.216681
branch 19 : rect id 112
rect:   -115.604919 -105.745247
    31.000710   31.265800
branch 20 : rect id 114
rect:   -116.274750 -106.139961
    30.972040   31.221251
branch 21 : rect id 115
rect:   -116.293137 -105.573257
    30.998100   31.231001
branch 22 : rect id 116
rect:   -116.273071 -106.156433
    30.979561   31.224291
branch 23 : rect id 117
rect:   -116.220413 -106.046547
    30.947639   31.183411
branch 24 : rect id 118
rect:   -116.199783 -106.080406
    30.951260   31.249399
branch 25 : rect id 119
rect:   -116.236526 -106.184769
    30.939819   31.181259
branch 26 : rect id 120
rect:   -116.295197 -106.066818
    30.916910   31.142250
branch 27 : rect id 121
rect:   -116.295670 -106.065643
    30.919300   31.153290
branch 28 : rect id 122
rect:   -116.289436 -105.877930
    30.908859   31.099859
branch 29 : rect id 123
rect:   -116.276169 -106.044052
    30.897539   31.097830
branch 30 : rect id 124
rect:   -114.783638 -106.032387
    30.561710   31.156429
Leaf Node:  level=0  count=40  address=93
Non-Leaf Node:  level=1  count=31  address=177

我想使用sed打印每行的每个坐标值。例如,它应该是:

-117.035332 
-114.738449  
32.269550   
32.711048

如果考虑输入的第3行和第4行 请注意,“Non-Leaf”和“Node:”之间只有一个空格,“Node:”和“level = 1”之间有2个空格,“rect:”和坐标值之间的标签位于坐标值。如果它不可能由sed,那么请告诉我如何打印2个字符串之间的线条并将整个事件管理到与此相同的操作?

3 个答案:

答案 0 :(得分:2)

您也可以使用awk。使用适当的记录分隔符,所需元素变得微不足道:

awk -v RS='rect:' 'NR > 1 { print $1, $2, $3, $4 }' infile

NR > 1跳过标题。

输出:

-117.035332 -114.738449 32.269550 32.711048
-117.037956 -114.758972 32.245251 32.684460
-117.010262 -114.773041 32.249630 32.674690
-117.054260 -114.791054 32.272289 32.668911
...

如果您希望输出与birei的答案相同,请分别设置OFSORS,分别为\n\n\n

awk -v RS='rect:' -v OFS='\n' -v ORS='\n\n' 'NR > 1 { print $1, $2, $3, $4 }' infile

输出:

-117.035332
-114.738449
32.269550
32.711048

-117.037956
-114.758972
32.245251
32.684460

-117.010262
-114.773041
32.249630
32.674690

-117.054260
-114.791054
32.272289
32.668911

...

答案 1 :(得分:1)

一种方式。避免使用-n开关自动打印,然后当一行与行首开始匹配时,删除它,追加以下行,删除多余空格,在坐标之间添加换行符并打印:

rect:

假设sed -ne '/^rect:[\t]*/ { s/// ; N ; s/\n[ ]*/ / ; s/[ \t]\+/\n/g ; p }' infile 已在您的问题中粘贴了内容,则输出将为:

infile

UPDATE 在坐标后添加字符串-117.035332 -114.738449 32.269550 32.711048 -117.037956 -114.758972 32.245251 32.684460 -117.010262 -114.773041 32.249630 32.674690 -117.054260 -114.791054 32.272289 32.668911 ...

rect id命令:

sed

输出:

sed -ne '
    ## In a line that matches "branch" at the beginning of the line:
    ## 1. Remove all characters until a colon and all spaces just after it.
    ##    The result will be the "rect id" string.
    ## 2. Save it in the "hold space".
    /^branch/ { 
        s/^[^:]*:[ \t]*//; 
        h
    }; 

    ## In a line that matches "rect" at the beginning of the line:
    ## 1. Remove the content matched.
    ## 2. Append next line (it will be the second pair of coordinates).
    ## 3. Remove the newline character between coordinates.
    ## 4. Now all coordinates will be in the same line, so sustitute the
    ##    spaces between coordinates with a newline character.
    ## 5. Print coordinates (one per line).
    ## 6. Get content of "hold space" (remember, the "rect id" saved before) 
    ##    and print it just after coordinaes.
    /^rect:[\t]*/ { 
        s///; 
        N; 
        s/\n[ ]*/ /; 
        s/[ \t]\+/\n/g; 
        p; 
        g; 
        p 
    }
' infile

答案 2 :(得分:0)

sed -ne '/^rect:\t*/ { s///; N; s/\n\s*/ /; s/\s\+/\n/g; p }' inputfile