sed 作为流编辑神器,在写脚本的时候往往会发挥奇效,所以这里我将sed的一些关键用法介绍一下,目的在于实用,而不是全面。
<code>-i</code> 参数
常常会看到会有sed -i这样的命令,-i的意思就是直接在源文件上进行操作。
动作
sed的动作是我们需要掌握的最为重要的内容 :
– a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行);
– c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行;
– d :删除, d 后面通常不接任何字符;
– i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
– p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行;
– s :替换,可以直接进行取代的工作通常这个 s 的动作可以搭配正则语法,语法很像vim的替换。
样例
测试文件内容:
1
2
3
4
5
6
7
8
# cat song.txt
my name is zhangshan
his name is lisi
my dog name is doglas
today is good day
we sang a song:
lalalalala
hahahahaha
<code>s</code> 替换
sed "s/zhangshan/wangnima/g" song.txt
替换的方式和vim很像
输出
1
2
3
4
5
6
7
8
# sed "s/zhangshan/wangnima/g" song.txt
my name is wangnima
his name is lisi
my dog name is doglas
today is good day
we sang a song:
lalalalala
hahahahaha
# sed "1s/name/nume/g" song.txt
my nume is wangnima
his name is lisi
my dog name is doglas
today is good day
we sang a song:
lalalalala
hahahahaha
指定行号范围
sed "1,2s/name/nume/g" song.txt
输出
1
2
3
4
5
6
7
8
# sed "1,2s/name/nume/g" song.txt
my nume is wangnima
his nume is lisi
my dog name is doglas
today is good day
we sang a song:
lalalalala
hahahahaha
只替换每行第一个s
sed "s/s/S/1" song.txt
输出
1
2
3
4
5
6
7
8
# sed "s/s/S/1" song.txt
my name iS wangnima
hiS name is lisi
my dog name iS doglas
today iS good day
we Sang a song:
lalalalala
hahahahaha
只替换每行第2个s
sed "s/s/S/2" song.txt
输出
1
2
3
4
5
6
7
8
# sed "s/s/S/2" song.txt
my name is wangnima
his name iS lisi
my dog name is doglaS
today is good day
we sang a Song:
lalalalala
hahahahaha
只替换每行第2个之后的s
sed "s/s/S/2g" song.txt
输出
1
2
3
4
5
6
7
8
# sed "s/s/S/2g" song.txt
my name is wangnima
his name iS liSi
my dog name is doglaS
today is good day
we sang a Song:
lalalalala
hahahahaha
多次匹配
将第一行的”wangnima”改成”zhangshan”然后把第二行的lisi改成”wangwu”
1
sed "1s/wangnima/zhangshan/g; 2s/lisi/wangwu/" song.txt
输出
1
2
3
4
5
6
7
8
# sed "1s/wangnima/zhangshan/g; 2s/lisi/wangwu/" song.txt
my name is zhangshan
his name is wangwu
my dog name is doglas
today is good day
we sang a song:
lalalalala
hahahahaha
等价命令
1
sed -e "1s/wangnima/zhangshan/g" -e "2s/lisi/wangwu/" song.txt
`&` 的妙用
可以用&表示被匹配到的字符,我们可以用这个trick 做一些事情:
1
sed "s/name/@&@/g" song.txt
输出:
1
2
3
4
5
6
7
my @name@ is wangnima
his @name@ is lisi
my dog @name@ is doglas
today is good day
we sang a song:
lalalalala
hahahahaha