Linux shell编程之sed命令之一

sed编辑器称为流编辑器(stream editor),在交互式编辑器中,可以交互地使用键盘命令插入、删除、替换文本。而sed将根据处理数据之前事先提供的规则对数据流进行编辑。sed编辑器每次从输入读取一行数据,将该数据与所提供的规则进行匹配,根据规则对数据进行处理,然后将处理后数据输出到stdout,在一行数据匹配完之后,它读取下一行数据,并重复上述过程,直到所有数据行处理完毕后,sed编辑器停止,sed编辑器比交互式编辑器快得多。需要注意的一点是:

sed并不修改文本文件中的数据,它只是将文本中的数据处理后发送到stdout,文本文件中的数据并不改变。

一、基本用法
sed命令的基本格式如下:

sed options script file

可以看出sed命令本身很简单,但是这个命令麻烦之处在于:数据处理规则命令的编写。

$ echo "This is a test" | sed 's/test/big test/'
This is a big test
上面这个例子中's/test/big test/'即为数据处理规则,在sed编程器中使用了s命令,s命令用第二个文本字符串替换第一个文本字符串,注意上述命令中'/'的使用,在命令与字符串、字符串之间 以及结尾都有'/'。
$ cat test.txt 
I have an apple.
I have an apple.
I have an apple.
$ cat test.txt |sed 's/apple/banana/'
I have an banana.
I have an banana.
I have an banana.
使用-e选项可以添加多个数据处理规则:

$ cat test.txt 
I have an apple.
I have an apple.
I have an apple.
$ sed -e 's/apple/oranges/;s/an/two/' test.txt 
I have two oranges.
I have two oranges.
I have two oranges.
如果有很多sed命令需要处理,那么将它们放一个文件中即可,便于复用。可以使用 -f选项指定数据处理规则文件。
$ cat  test.txt 
I have an apple.
I have an apple.
I have an apple.
$ cat sed.txt 
s/apple/pear/
s/an/a/
$ sed -f sed.txt  test.txt 
I have a pear.
I have a pear.
I have a pear.
二、替换命令
下面重点介绍一下sed处理规则命令,上面几个例子中用到的s命令是替换命令。替换命令在替换多个文本行的文本时,默认情况下仅替换每行首次出现的文本。
$ cat test.txt 
apple apple apple 
apple apple apple 
apple apple apple 
$ sed 's/apple/banana/' test.txt 
banana apple apple 
banana apple apple 
banana apple apple 
要想使替换命令把所有文本全部替换,必须使用替换标记(substitution flag)。替换标记放在替换字符串最后。
s/pattern/replacement/flags
可用的替换标记有4种
1、数字:表示新文本替换的模式。
2、g:表示用新文本替换现在文本中的全部实例
3、p:表示除了打印替换后的文本外,额外打印包含替换命令中匹配模式的那一行
4、w file:将替换的结果写入文件。
1、数字标记可以指定替换文本行的第几个字符串。
$ cat test.txt 
apple apple apple 
apple apple apple 
apple apple apple
$ sed 's/apple/pen/1' test.txt 
pen apple apple 
pen apple apple 
pen apple apple 
$ sed 's/apple/pen/2' test.txt 
apple pen apple 
apple pen apple 
apple pen apple 
$ sed 's/apple/pen/3' test.txt 
apple apple pen 
apple apple pen 
apple apple pen 
2、g标记用新文本替换现在文本中的全部实例

$ cat test.txt 
apple apple apple 
apple apple apple 
apple apple apple 
$ sed 's/apple/pen/g' test.txt 
pen pen pen 
pen pen pen 
pen pen pen
3、p标记表示除了打印替换后的文本外,额外打印包含替换命令中匹配模式的那一行
$ cat test.txt 
there is a king in the east
there is a doctor in the west
$ sed 's/king/farmer/p' test.txt 
there is a farmer in the east
there is a farmer in the east
there is a doctor in the west
-n选项用于禁止sed编辑器的输出,与p标记一起用可以仅打印替换命令更改的那些行。
$ cat test.txt 
there is a king in the east
there is a doctor in the west
$ sed -n 's/king/farmer/p' test.txt 
there is a farmer in the east
4、w标记 将替换的结果写入文件
$ cat test.txt 
there is a king in the east
there is a doctor in the west
$ cat test
this is test
$ sed 's/king/doctor/w test' test.txt 
there is a doctor in the east
there is a doctor in the west
$ cat test.txt 
there is a king in the east
there is a doctor in the west
$ cat test
there is a doctor in the east
注意这个写是覆盖写,并且只有那些包含匹配模式的行才会写入文件。除了'/'字符外,还可以使用其它字符作为定界符。如果我们要处理的文本中含有'/'时,使用其它字符就会非常方便,免除了转义字符的麻烦。
$ cat test.txt 
/home/jix/code
/etc/passwd
$ sed 's!/home/jix!/home/jie!' test.txt 
/home/jie/code
/etc/passwd
不使用'!'作为分隔符的话就要使用转义的方法:
$ sed 's/\/home\/jix/\/home\/jie/' test.txt 
/home/jie/code
/etc/passwd
没有对比就木有伤害啊,麻烦了很多。使用'?'也可以,这个分隔符很灵活。

$ sed 's?/home/jix?/home/jie?' test.txt 
/home/jie/code
/etc/passwd
下篇博客中继续学习sed命令的用法。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值