标签:sed
一: sed 是什么?
sed 行编辑工具. sed , awk , grep 被称作文本三剑客.
二:sed 的命令语法
sed [option]... ‘地址 编辑命令‘ FILE...
optin 的选项有:
-n: 静默模式,不显示模式空间中的内容 -r: 支持使用扩展正则表达式 -i: 修改原文件; -e: sed -e "" -e "" -e "", sed "{COM1;COM2;COM3}"
填写地址方式:
行范围: start_line[, end_line] # sed -n ‘1,5p‘ test.txt #从第一行到第五行打印出来 # sed -n ‘1,$p‘ test.txt #从第一行到最后一行打印出来 /patten1/,/patten2/: 第一次被pattern1匹配到的行开始,到第一次被pattern2匹配到的行结束之间的所有行; # sed -n ‘/\<and\>/,/\<or\>/p‘ test.txt 特定行: /pattern/ : 符合条件的特定行. # sed -n ‘/all/p‘ test.pp 无地址:全文
编辑命令
编辑命令:命令可在之前加!取反 p:打印 d: 删除 i \text: 行上方,text即为插入的内容 a \text: 行下方,text即为插入的内容 r /path/from/some_file: 读入文件 w /path/to/some_file: 把符合条件的行保存至指定的文件中 =: 显示符合条件行的行号 s///: s@@@ g:为全局替换 s///g
一些常见的几个命令
‘s / \ . $ / / g’ 删除以句点结尾的. ‘-e /abcd/d’ 删除包含a b c d的行. ‘s / [ ][ ]* / [ ] / g’ 删除一个以上空格,用一个空格代替 此处的[] 指空格代替 ‘s / ^ [ ] [ ] * / / g’ 删除行首空格 ‘s / \ . [ ] [ ] * / [ ] / g’ 删除句点后跟一个或更多空格,代之以一个空格 ‘/ ^ $ / d’ 删除空行 ‘s / ^ . / / g’ 删除第一个字符 ‘s / ^ \ / / / g’ 从路径中删除第一个/ ‘S / ^ [ ] / / g’ 删除行首所有t a b键 ‘s / [ ] * / / g’ 删除所有t a b键
三:增删换显几个练习
例:cat 1.txt
cat 1.txt 12332###DISO###45.12^M 00332###LPSO###23.14^M 01299###USPD###34.46^M 把所有是#的内容替换掉 sed ‘s/##*//‘ 1.txt 12332DISO45.12^M 00332LPSO23.14^M 01299USPD34.46^M 显示第2行内容 sed -n ‘2p‘ 1.txt 00332###LPSO###23.14^M 删除所有行首是0 的行内容 sed /^00*/d 1.txt 12332###DISO###45.12^M 替换行首是0的内容为1 sed ‘s/^0/1/g‘ 1.txt 12332###DISO###45.12^M 10332###LPSO###23.14^M 11299###USPD###34.46^M 替换行尾是特殊字符^M sed ‘s/\^M$//g‘ 1.txt 12332###DISO###45.12 00332###LPSO###23.14 01299###USPD###34.46 操作集合 cat 1.txt |sed ‘s/##*//g‘ | sed ‘s/\^M//g‘ | sed ‘2d‘ | sed ‘/^0/i hello‘ | sed ‘/^0/a word‘ | sed ‘1p‘ 12332DISO45.12 12332DISO45.12 hello 01299USPD34.46 word 看到前两行是同样的内容输出,如果我们想要输出匹配的一行. 用-n 参数; 如果想要让上面的内容生效用-i 参数
四:打印行号
例 :cat 2.txt
cat 2.txt The honeysuckle band played all night long for only $90. It was an evening of splendid art and company. The office Dibble art played well. The local nurse music P.Neave was in attendance. sed -e ‘/was/=‘ 2.txt The honeysuckle band played all night long for only $90. 2 It was an evening of splendid art and company. The office Dibble art played well. 4 The local nurse music P.Neave was in attendance. 如果只关心行号 把-e换成-n 只显示匹配到的行和行号 sed -n -e ‘/was/p‘ -e ‘/was/=‘ 2.txt It was an evening of splendid art and company. 2 The local nurse music P.Neave was in attendance. 4
五:读入文件内容,写入文件内容
cat 1.txt 12332###DISO###45.12^M 00332###LPSO###23.14^M 01299###USPD###34.46^M cat 2.txt The honeysuckle band played all night long for only $90. It was an evening of splendid art and company. The office Dibble art played well. The local nurse music P.Neave was in attendance. 把2.txt中的内容读入1.txt第一行下面 sed ‘1r 2.txt‘ 1.txt 12332###DISO###45.12^M The honeysuckle band played all night long for only $90. It was an evening of splendid art and company. The office Dibble art played well. The local nurse music P.Neave was in attendance. 00332###LPSO###23.14^M 01299###USPD###34.46^M 把1.txt第1行写入test.txt中 sed ‘1w test.txt‘ 1.txt cat test.txt 12332###DISO###45.12^M
六:在内容后面插入内容
例 :cat 3.txt
cat 3.txt AC456 AC492169 AC9967 AC88345 在数字最后插入 passwd sed ‘s/[0-9][0-9]*/& passwd/g‘ 3.txt sed ‘s/[0-9][0-9]*/& passwd/g‘ 3.txt AC456 passwd AC492169 passwd AC9967 passwd AC88345 passwd
本文出自 “slayer” 博客,请务必保留此出处http://slayer.blog.51cto.com/4845839/1664248
标签:sed
原文地址:http://slayer.blog.51cto.com/4845839/1664248