linux sed命令使用疑惑总结

s 替换命令

[zhuhc@test111 ~]$ sed 's/ma/mass' test.txt
sed: -e expression #1, char 9: unterminated `s' command

原因:替换命令s末尾的斜杠丢失了。正确命令:sed 's/ma/mass/' test.txt

 

[zhuhc@test111 ~]$  sed s/ma/, mass test.txt                                
sed: -e expression #1, char 6: unterminated `s' command

原因:替换命令s,替换的内容包含特殊字符 空格 ,则命令要用 单引号 括起来。正确的命令:sed 's/ma/, mass/' test.txt

 

p 打印命令

[zhuhc@test111 ~]$ cat test1.txt
jhon daggett,341 king road,plymouth MA
[zhuhc@test111 ~]$ sed 's/MA/, MASS/p' test1.txt
jhon daggett,341 king road,plymouth , MASS
jhon daggett,341 king road,plymouth , MASS

原因:观察结果,使用p命令,将匹配的行,打印了两遍,是因为sed会默认的打印一遍,然后p又打印一遍,所以一共就两遍。p命令会和 -n命令一起使用,-n命令是禁止sed默认打印。正确的命令:

[zhuhc@test111 ~]$ sed -n 's/MA/, MASS/p' test1.txt
jhon daggett,341 king road,plymouth , MASS

 

posted on 2018-02-05 16:51  婵婵  阅读(973)  评论(0编辑  收藏  举报

导航