- 论坛徽章:
- 0
|
请教sed的一个问题,急救呀!!!!!!
-e 是順序的.
我把datafile加了數字,方便分析.
原數據內容
# cat datafile
1 southeast SE Patricia Hemenway 4.0 .7 4 17
2 eastern EA TB Savage 4.4 .84 5 20
3 northeast NE AM Main Jr. 5.1 .94 3 13
4 north NO Margot Weber 4.5 .89 5 9
5 central CT Ann Stephens 5.7 .94 5 13
# sed -e '/^1/h' -e '/^4/x' -e '/^3/x' datafile
1 southeast SE Patricia Hemenway 4.0 .7 4 17
2 eastern EA TB Savage 4.4 .84 5 20
1 southeast SE Patricia Hemenway 4.0 .7 4 17
4 north NO Margot Weber 4.5 .89 5 9
5 central CT Ann Stephens 5.7 .94 5 13
分析
line 1:
before: current=line1
'^1/h' true. put line 1 to buffer,so buffer=line1
'^4/x' , '/^3/x' false
result: output line1 and buffer=line1
line 2:
before: current=line2
current=line2
'^1/h' ..... false, so do nothing.
result: output line2 and buffer=line1
line 3:
before: current=line3
'^1/h' false
'^4/x' false
'^3/x' true, exchange current and buffer , so current=line1 and buffer=line3
result: output line1 and buffer=line3
line 4:
before:current=line4
'^1/h' false
'^4/x' true , exchange current and buffer,so current=line3, buffer=line4
'^3/x' true, 因為上一步已經把current=line3. after exchange current and buffer, current=line4 and buffer=line3
result utput line4 and buffer=line3
line 5;
same as line 2
result utput line 5
# sed -e '/^1/h' -e '/^3/x' -e '/^4/x' datafile
1 southeast SE Patricia Hemenway 4.0 .7 4 17
2 eastern EA TB Savage 4.4 .84 5 20
1 southeast SE Patricia Hemenway 4.0 .7 4 17
3 northeast NE AM Main Jr. 5.1 .94 3 13
5 central CT Ann Stephens 5.7 .94 5 13
分析
line 1:
before: current=line1
'^1/h' true. put line 1 to buffer,so buffer=line1
'^3/x' , '/^4/x' false
result: output line1 and buffer=line1
line 2:
before: current=line2
current=line2
'^1/h' ..... &&³&false, so do nothing.
result: output line2 and buffer=line1
line 3:
before: current=line3
'^1/h' false
'^3/x' true, exchange current and buffer, so current=line1 and buffer=line3
'^3/x' false
result: output line1 and buffer=line3
line 4:
before:current=line4
'^1/h' false
'^3/x' false
'^4/x' true, exchange current and buffer, current=line3 and buffer=line4
result utput line3 and buffer=line4
line 5;
same as line 2
result utput line 5
# sed -e '/^1/h' -e '/^4/x' -e '/^3/g' datafile
1 southeast SE Patricia Hemenway 4.0 .7 4 17
2 eastern EA TB Savage 4.4 .84 5 20
1 southeast SE Patricia Hemenway 4.0 .7 4 17
1 southeast SE Patricia Hemenway 4.0 .7 4 17
5 central CT Ann Stephens 5.7 .94 5 13
Do you explain ? |
|