标签:head pat ast 停止 star txt sleep 删除命令 div
1 # cat data1.txt 2 this is the header line 3 4 this is the data line 5 6 this is the last line
经n命令处理后可跳过第一行:
1 # sed ‘/header/{n;d}‘ data1.txt 2 this is the header line 3 this is the data line 4 5 this is the last line
1 # cat data2.txt 2 This is the header line. 3 This is the first data line. 4 This is the second data line. 5 This is the last line.
1 # sed ‘/first/{N;s/\n/ /}‘ data2.txt 2 This is the header line. 3 This is the first data line. This is the second data line. 4 This is the last line.
Tips:使用N命令可查找分散在两行的文本。注意最后一行进入模式空间,执行N命令由于没有后续的可读入行,会导致后面的命令无法执行。
1 # cat data5.txt 2 3 This is the header line. 4 This is a data line. 5 6 This is the last line.
1 # sed ‘/^$/{N ; /header/D}‘ data5.txt 2 This is the header line. 3 This is a data line. 4 5 This is the last line.
Tips:这种办法可实现删除特殊行,比如需要同时满足两种条件的行。
1 # sed -n ‘/header/{N;P}‘ data5.txt 2 This is the header line.
命令
|
描述
|
h
H
g
G
x
|
将模式空间复制到保持空间
将模式空间附加到保持空间
将保持空间复制到模式空间
将保持空间附加到模式空间
交换模式空间和保持空间
|
1 # sed -n ‘/first/ {h ; p ; n ; p ; g ; p }‘ data2.txt 2 This is the first data line. 3 This is the second data line. 4 This is the first data line.
1 # sed -n ‘/header/p‘ data2.txt 2 This is the header line.
普通p命令打印匹配文本行,使用!后,效果相反,不打印该行而打印其它行:
1 # sed -n ‘/header/!p‘ data2.txt 2 This is the first data line. 3 This is the second data line. 4 This is the last line.
结合保持空间反转文本行的顺序:
1 # sed -n ‘{1!G ; h ; $p }‘ data2.txt 2 This is the last line. 3 This is the second data line. 4 This is the first data line. 5 This is the header line.
Tips:sed编辑器默认会将命令循环应用在每一行文本上,因此合理使用命令,可写出简洁高效的sed脚本
1 # sed ‘{2,3b ; s/This is/Is this/ ; s/line./test?/}‘ data2.txt 2 Is this the header test? 3 This is the first data line. 4 This is the second data line. 5 Is this the last test?
加address,加label,分支匹配处的行跳到标签处,执行标签后的命令;不匹配的行执行所有命令:
1 # sed ‘/first/b jump1 ; s/This is the/No jump on/; :jump1 ; s/This is the/Jump here on/‘ data2.txt 2 No jump on header line. 3 Jump here on first data line. 4 No jump on second data line. 5 No jump on last line.
不加address,b命令无条件跳转到标签上,可造成循环的效果:
1 # echo "This, is, a, test, to, remove, commas." | sed -n ‘{:start;s/,//1p;b start}‘ 2 This is, a, test, to, remove, commas. 3 This is a, test, to, remove, commas. 4 This is a test, to, remove, commas. 5 This is a test to, remove, commas. 6 This is a test to remove, commas. 7 This is a test to remove commas. 8 ^C
该脚本不会停止循环,可使用/,/b start代替b start,在没有逗号时自动结束循环。
1 # sed ‘s/first/matched/; t ; s/This/Here/‘ data2.txt 2 Here is the header line. 3 This is the matched data line. 4 Here is the second data line. 5 Here is the last line.
加label,跳转到label处:
1 # echo "This,is,a,test,to,remove,commas." | sed -n ‘{:start ; s/,/ /p ; t start}‘ 2 This is,a,test,to,remove,commas. 3 This is a,test,to,remove,commas. 4 This is a test,to,remove,commas. 5 This is a test to,remove,commas. 6 This is a test to remove,commas. 7 This is a test to remove commas.
1 # sed ‘s/first/matched/; t ;s/This/Here/;s/line/LINE/‘ data2.txt 2 Here is the header LINE. 3 This is the matched data line. 4 Here is the second data LINE. 5 Here is the last LINE.
1 # sed ‘s/first/matched/; t jump ;s/This/Here/; :jump ;s/line/LINE/‘ data2.txt 2 Here is the header LINE. 3 This is the matched data LINE. 4 Here is the second data LINE. 5 Here is the last LINE.
Tips:使用标签和t命令可在脚本命令中形成循环
1 # echo "The cat sleeps in his hat." | sed ‘s/.at/"&"/g‘ 2 The "cat" sleeps in his "hat".
【替代单独的单词】
1 # echo 1234567 | sed ‘:start;s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t start‘ 2 1,234,567
Tips:圆括号必须转义
标签:head pat ast 停止 star txt sleep 删除命令 div
原文地址:https://www.cnblogs.com/MrReboot/p/10988347.html