首先查看一下文件内容
[root@SERVER-Test testperl]# cat asd.txt This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
把其中的my字符串替换成zxcv,下面的语句应该很好理解(s表示替换命令,/my/表示匹配my,/Hao Chen’s/表示把匹配替换成Hao Chen’s,/g 表示一行上的替换所有的匹配)。
我们再来看看指定需要替换第3行的内容:
[root@SERVER-Test testperl]# sed "s/my/zxcv/g" asd.txt This is zxcv cat zxcv cat's name is betty This is zxcv dog zxcv dog's name is frank This is zxcv fish zxcv fish's name is george This is zxcv goat zxcv goat's name is adam
注意:上面的sed并没有对文件的内容改变,只是把处理过后的内容输出,如果你要写回文件,你可以使用重定向,如:
[root@SERVER-Test testperl]# sed "s/my/zxcv/g" asd.txt > /tmp/zxcv.txt
或使用 -i 参数直接修改文件内容:
[root@SERVER-Test testperl]# sed -i "s/my/zxcv/g" asd.txt
在每一行最前面加点东西:
[root@SERVER-Test testperl]# sed "s/^/......./g" asd.txt .......This is my cat ....... my cat's name is betty .......This is my dog ....... my dog's name is frank .......This is my fish ....... my fish's name is george .......This is my goat ....... my goat's name is adam
在每一行最后面加点东西:
[root@SERVER-Test testperl]# sed "s/$/##########/g" asd.txt This is my cat########## my cat's name is betty########## This is my dog ########## my dog's name is frank########## This is my fish ########## my fish's name is george########## This is my goat ########## my goat's name is adam##########
正则表达式的一些最基本的东西:
^ 表示一行的开头。如:/^#/ 以#开头的匹配。
$ 表示一行的结尾。如:/}$/ 以}结尾的匹配。
\< 表示词首。 如 \<abc 表示以 abc 为首的�~。
\> 表示词尾。 如 abc\> 表示以 abc �Y尾的�~。
. 表示任何单个字符。
* 表示某个字符出现了0次或多次。
[ ] 字符集合。 如:[abc]表示匹配a或b或c,还有[a-zA-Z]表示匹配所有的26个字符。如果其中有^表示反,如[^a]表示非a的字符
我们再来看看指定需要替换的第3行内容:
[root@SERVER-Test testperl]# sed "3s/my/your/g" asd.txt This is my cat my cat's name is betty This is your dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
下面的命令只替换第3到第6行的文本。
[root@SERVER-Test testperl]# cat asd.txt This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam [root@SERVER-Test testperl]# sed '3,6s/my/your/g' asd.txt This is my cat my cat's name is betty This is your dog your dog's name is frank This is your fish your fish's name is george This is my goat my goat's name is adam
只替换每一行的第一个s:
[root@SERVER-Test testperl]# sed 's/s/S/1' asd.txt ThiS is my cat , my cat's name is betty ThiS is my dog , my dog's name is frank ThiS is my fish , my fish's name is george ThiS is my goat , my goat's name is adam
只替换每一行的第二个s:
[root@SERVER-Test testperl]# sed 's/s/S/2' asd.txt This iS my cat , my cat's name is betty This iS my dog , my dog's name is frank This iS my fish , my fish's name is george This iS my goat , my goat's name is adam
只替换第一行的第3个以后的s:
[root@SERVER-Test testperl]# cat asd.txt This is my cat , my cat's name is betty This is my dog , my dog's name is frank This is my fish , my fish's name is george This is my goat , my goat's name is adam [root@SERVER-Test testperl]# sed 's/s/S/3g' asd.txt This is my cat , my cat'S name iS betty This is my dog , my dog'S name iS frank This is my fiSh , my fiSh'S name iS george This is my goat , my goat'S name iS adam
如果我们需要一次替换多个模式,可参看下面的示例:(第一个模式把第一行到第三行的my替换成your,第二个则把第3行以后的This替换成了That)
[root@SERVER-Test testperl]# sed '1,3s/my/your/g; 3,$s/This/that/g' asd.txt This is your cat , your cat's name is betty This is your dog , your dog's name is frank that is your fish, your fish's name is george that is my goat, my goat's name is adam [root@SERVER-Test testperl]# cat asd.txt This is my cat , my cat's name is betty This is my dog , my dog's name is frank This is my fish, my fish's name is george This is my goat, my goat's name is adam
上面的命令等价于:(注:下面使用的是sed的-e命令行参数)
[root@SERVER-Test testperl]# sed -e '1,3s/my/your/g' -e '3,$s/This/that/g' asd.txt This is your cat , your cat's name is betty This is your dog , your dog's name is frank that is your fish, your fish's name is george that is my goat, my goat's name is adam
我们可以使用&来当做被匹配的变量,然后可以在基本左右加点东西。如下所示:
[root@SERVER-Test testperl]# sed 's/my/[&]/g' asd.txt This is [my] cat , [my] cat's name is betty This is [my] dog , [my] dog's name is frank This is [my] fish, [my] fish's name is george This is [my] goat, [my] goat's name is adam
我们可以使用&在我们想要的地方添加一些内容,例如在my后面添加内容123,如下
[root@SERVER-Test testperl]# sed 's/my/& 123/g' asd.txt This is my 123 cat , my 123 cat's name is betty This is my 123 dog , my 123 dog's name is frank This is my 123 fish, my 123 fish's name is george This is my 123 goat, my 123 goat's name is adam [root@SERVER-Test testperl]# cat asd.txt This is my cat , my cat's name is betty This is my dog , my dog's name is frank This is my fish, my fish's name is george This is my goat, my goat's name is adam
[root@SERVER-Test testperl]# cat qwe.txt This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam [root@SERVER-Test testperl]# sed 'N;s/my/your/' qwe.txt This is your cat my cat's name is betty This is your dog my dog's name is frank This is your fish my fish's name is george This is your goat my goat's name is adam
小n命令--只匹配偶数行内容
[root@SERVER-Test testperl]# sed 'n;s/my/your/' asd.txt This is my cat, my cat's name is betty This is your dog,my dog's name is frank This is my fish,my fish's name is george This is your goat,my goat's name is adam [root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam
[root@SERVER-Test testperl]# cat qwe.txt This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
上面例子其实就是下面的这样
This is my cat\n my cat's name is betty This is my dog\n my dog's name is frank This is my fish\n my fish's name is george This is my goat\n my goat's name is adam
可以将\n替换成,
[root@SERVER-Test testperl]# sed 'N;s/\n/,/' qwe.txt This is my cat, my cat's name is betty This is my dog, my dog's name is frank This is my fish, my fish's name is george This is my goat, my goat's name is adam
a命令就是append, i命令就是insert,它们是用来添加行的。如:
例如: 其中的1i表明,其要在第1行前插入一行(insert)
[root@SERVER-Test testperl]# cat qwe.txt This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam [root@SERVER-Test testperl]# sed "1 i this is my hourse, my hourse's name is wuhang" qwe.txt this is my hourse, my hourse's name is wuhang This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
# 其中的1a表明,其要在第一行后追加一行(append)
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed "1 a this 3333333333333333333333" asd.txt This is my cat, my cat's name is betty this 3333333333333333333333 This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam#其中4a
###其中$ a表示将要在最后一行追加一行内容
[root@SERVER-Test testperl]# sed "$ a this 999999999999" asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam this 999999999999
我们可以运用匹配来添加文本:
例如:注意其中的/fish/a,这意思是匹配到/fish/后就追加一行
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed "/fish/a this is 4444444444444" asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george this is 4444444444444 This is my goat,my goat's name is adam
下面这个例子是对每一行都进行插入:
[root@SERVER-Test testperl]# sed "/my/a -----------" asd.txt This is my cat, my cat's name is betty ----------- This is my dog,my dog's name is frank ----------- This is my fish,my fish's name is george ----------- This is my goat,my goat's name is adam -----------
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed "2 c this 88888888888333333333" asd.txt This is my cat, my cat's name is betty this 88888888888333333333 This is my fish,my fish's name is george This is my goat,my goat's name is adam
下面就是将fish行内容替换成555555555内容
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed "/fish/c 55555555555555555555" asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank 55555555555555555555 This is my goat,my goat's name is adam
例如:删除匹配fish行内容
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed "/fish/d" asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my goat,my goat's name is adam
例如:删除第2行内容
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed "2d" asd.txt This is my cat, my cat's name is betty This is my fish,my fish's name is george This is my goat,my goat's name is adam
例如:删除从第2行到结尾(最后一行)的内容
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed '2,$d' asd.txt This is my cat, my cat's name is betty
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed '/fish/p' asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my fish,my fish's name is george This is my goat,my goat's name is adam
上面例子其实使用-n参数就好了
[root@SERVER-Test testperl]# sed -n '/fish/p' asd.txt This is my fish,my fish's name is george
例如:打印匹配的二行内容
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed -n "/dog/,/fish/p" asd.txt This is my dog,my dog's name is frank This is my fish,my fish's name is george
例如:打印从第一行到匹配fish成功的那一行
[root@SERVER-Test testperl]# cat asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george This is my goat,my goat's name is adam [root@SERVER-Test testperl]# sed -n "1,/fish/p" asd.txt This is my cat, my cat's name is betty This is my dog,my dog's name is frank This is my fish,my fish's name is george
1.命令打包,命令可以是多个,它们可以用分号分开,可以用大括号括起来作为嵌套命令。
例如:删除第3到6行的中匹配This的内容, 对3行到第6行,执行命令/This/d
[root@SERVER-Test testperl]# cat asd.txt This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam [root@SERVER-Test testperl]# sed '3,6 {/This/d}' asd.txt This is my cat my cat's name is betty my dog's name is frank my fish's name is george This is my goat my goat's name is adam
# 对3行到第6行,匹配/This/成功后,再匹配/fish/,成功后执行d命令,将fish行删除。
[root@SERVER-Test testperl]# cat asd.txt This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam [root@SERVER-Test testperl]# sed '3,6 {/This/{/fish/d}}' asd.txt This is my cat my cat's name is betty This is my dog my dog's name is frank my fish's name is george This is my goat my goat's name is adam
# 从第一行到最后一行,如果匹配到This,则删除之;如果前面有空格,则去除空格
[root@SERVER-Test testperl]# cat asd.txt This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam [root@SERVER-Test testperl]# sed '1,${/This/d; s/^ *//g}' asd.txt my cat's name is betty my dog's name is frank my fish's name is george my goat's name is adam
把pets.txt文件中有dog的行及从dog行开始的往下的3行开头加#注释掉
[root@SERVER-Test testperl]# cat asd.txt This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam [root@SERVER-Test testperl]# sed '/dog/,+3s/^/# /g' asd.txt This is my cat my cat's name is betty # This is my dog # my dog's name is frank # This is my fish # my fish's name is george This is my goat my goat's name is adam
可以参考:
http://blog.linuxeye.com/104.html
http://www.361way.com/sed-jichu/344.html
http://blog.itechol.com/space-33-do-blog-id-5590.html
http://www.linux360.cn/thread-1125-1-1.html
http://fdgui.blog.51cto.com/3484207/1557352 正则表达式、sed、awk相关资料笔记资料整合