一.SEd文本转换和替代(-n和p一起用只打印变动行)
$ sed -n ‘2,4s/B/567/p’ $ sed -n ‘/AA/s/237/567/’ $ sed -n ‘/AA/,/DD/s/B/567/p’ |
$ sed -n ‘s/La//p $ sed 's/:.*//' /etc/passwd 删除第一个冒号之后的所有东西 $ sed -n ‘s/^...//p’ 删除行首的3个字符 $ sed -n ‘/...$//p’ 删除行尾的3个字符 $ sed '/baz/s/foo/bar/g' 只在行中出现baz的情况下替换 $ sed '/baz/!s/foo/bar/g' 只在行中未出现baz的情况下替换 |
$ sed 's/ruby/red/g;s/puce/red/g' ruby|puce一律换成red $ gsed 'ruby\|puce/red/g' ruby|puce一律换成red |
$ sed 's/^[ \t]*//' 删除行前的“空白字符”,左对齐 $ sed 's/[ \t]*$//' 删除拖尾的“空白字符” $ sed 's/^[ \t]*//;s/[ \t]*$//' 删除前导和拖尾的空白字符 |
$ sed -n 's/^/zcs/p' file 在行首添加zcs $ sed 's/^/ /' 行首插入5个空格(全文右移5字符) |
$ sed 's#10#100#g' 紧跟s命令的被认为是新的分隔符(代替/) |
$ sed -e "s/$var1/$var2/g" 使用变量替换(使用双引号) |
$ sed -n 's/^root/&user/p' ^root改为^rootuser $ sed -n -r 's/(love) able/\1rs/p' love被标记为1 |
$ sed 's/foo/bar/g' 每行中的所有foo都换成bar |
二.SEd选择性地显示特定行
sed -n '/Iowa/,/Montana/p' 区分大小写方式 sed -n '/regexp/,$p' 从包含正则的行到最后一行 |
sed -n '8,12p'或sed '8,12!d' 显第8至第12行 |
sed '$!d' 或sed -n '$p' 模拟“tail -1” |
sed -n '/reg/p'或sed '/reg/!d' 模拟grep sed -n '/reg/!p'或sed '/reg/d' 模拟grep -v |
sed -n '/^.\{65\}/p' 显示包含65个或以上字符的行 sed -n '/^.\{65\}/!p' 显示包含65个以下字符的行 sed '/^.\{65\}/d' 显示包含65个以下字符的行 |
sed -n '52p'或sed '52!d' 显示第52行 |
sed '/AAA/!d; /BBB/!d; /CCC/!d' 显示含AAA.BBB或CCC的行,任意次序 sed '/AAA.*BBB.*CCC/!d' 显示含AAA.BBB或CCC的行,固定次序 sed '/AAA\|BBB\|CCC/!d' 显示含AAA.BBB或CCC的行,类egrep |
三.SEd选择性地删除特定行
sed '2,$d' file sed ‘/^d/d’ file sed ‘/[0-9]/\{3\}/d’ file |
sed '/test/d' file 删除含test的行 sed '/test/'d file 删除含test的行 |
sed 's/^[ ]*//g' file 删除行首空格 |
sed 's/^ *//g' file 删除行首空格 |
sed 's/^[[:space:]]*//g' file 删除行首空格 |
sed '/In/,/Out/d' 除了两个正则表达式之间的内容都显示 |
sed '/^$/d' 或sed '/./!d'或 grep '.' 显示非空行 |
sed '/./,$!d' 删除文件顶部的所有空行 |
grep -v ^# filename | sed /^[[:space:]]*$/d | sed /^$/d删空行和空格及注释行 |
四.sed案例
$cat data northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 southwest SW Levis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13 |
$ sed ’s/[0-9][0-9]$/&.5/’ data northwest NW Charles Main 3.0 .98 3 34.5 western WE Sharon Gray 5.3 .97 5 23.5 |
$ sed -n ’s/\(Mar\)got/\1ianne/p’ data north NO Marianne Weber 4.5 .89 5 9 $sed ’s#3#88#g’ data northwest NW Charles Main 88.0 .98 88 884 western WE Sharon Gray 5.88 .97 5 288 |
$ sed ’/west/,/east/s/$/**VACA**/’ data northwest NW Charles Main 3.0 .98 3 34**VACA** western WE Sharon Gray 5.3 .97 5 23**VACA** |
$ sed -e ’1,3d’ -e ’s/Hemenway/Jones/’ data |
$ cat notice | *** SUAN HAS LEFT THE COMPANY *** | $ sed ’/Suan/r notice’ data southern SO Suan Chin 5.1 .95 4 15 | *** SUAN HAS LEFT THE COMPANY *** | |
$ sed -n ’/north/w newfile’ data |
$ sed ’/^north /a -->THE NORTH SALES DISTRICT HAS MOVED<--’ data north NO Margot Weber 4.5 .89 5 9 -->THE NORTH SALES DISTRICT HAS MOVED<-- |
$ sed ’/eastern/{n;s/AM/Archie/;}’ data eastern EA TB Savage 4.4 .84 5 20 northeast NE Archie Main Jr. 5.1 .94 3 13 |
$ sed ’1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/’ data |
$ sed -e ’/northeast/h’ -e ’$G’ data northwest NW Charles Main 3.0 .98 3 34 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 northeast NE AM Main Jr. 5.1 .94 3 13 |