sed 命令基本操作举例

1、替换

$ cat data1

this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog

The quick green elephant jumps over the fox dog


替换遇到的第一个匹配字符(默认)

$ sed 's/test/hello/' data1
this hello inside test
this hello inside test
this si inside hello
this si inside hello
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog


-n 禁止打印,命令p是打印修改后的行

$ sed -n 's/test/hello/p' data1
this hello inside test
this hello inside test
this si inside hello
this si inside hello


替换行内指定的匹配字符 2

$ sed -n 's/test/hello/2' data1
$ sed -n 's/test/hello/2p' data1
this test inside hello
this test inside hello


替换行内所有的匹配字符 g

$ sed -n 's/test/hello/gp' data1
this hello inside hello
this hello inside hello
this si inside hello
this si inside hello


$ cat /etc/passwd

zhongxuan:x:1001:1001:,,,:/home/zhongxuan:/bin/bash

用其他符号指定字符分节符 用 !作为分解符

$ sed -n 's!/home/zhongxuan!/yangzhongxuan!p' /etc/passwd
zhongxuan:x:1001:1001:,,,:/yangzhongxuan:/bin/bash
$ cat data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog

The quick green elephant jumps over the fox dog

文本模式匹配替换

$ sed -n '/green/s/fox/red/p' data1
The quick green elephant jumps over the red dog

$ sed -n '/fox/s/green/red/p' data1
The quick red elephant jumps over the fox dog

$ cat data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog


命令行组合

$ sed '2{
s/test/test_ch/
s/inside/outside/
}' data1
this test inside test
this test_ch outside test

$ cat data6
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.


2、插入

$ sed 'i\
quote> hello this is insert line 1' data6
hello this is insert line 1
This is line number 1.
hello this is insert line 1
This is line number 2.
hello this is insert line 1
This is line number 3.
hello this is insert line 1
This is line number 4.
hello this is insert line 1
This is line number 5.

$ sed '3i\
quote> you are ok insert the line 2.' data6
This is line number 1.
This is line number 2.
you are ok insert the line 2.
This is line number 3.
This is line number 4.

This is line number 5.


追加
$ sed '3a\
you are ok insert the line 2.' data6
This is line number 1.
This is line number 2.
This is line number 3.
you are ok insert the line 2.
This is line number 4.
This is line number 5.

$ sed '3i\
quote> you can insert more line.\
quote> you insert secnd line.\
quote> you insert third line.' data6
This is line number 1.
This is line number 2.
you can insert more line.
you insert secnd line.
you insert third line.
This is line number 3.
This is line number 4.
This is line number 5.


3、修改

$ sed '3c\
quote> you can change line with the sed command c' data6
This is line number 1.
This is line number 2.
you can change line with the sed command c
This is line number 4.
This is line number 5.

$ sed '/number 3/c\
quote> change line with txt mode' data6
This is line number 1.
This is line number 2.
change line with txt mode
This is line number 4.
This is line number 5.

$ sed '/number/c\
quote> chang lines that match the parrten' data6
chang lines that match the parrten
chang lines that match the parrten
chang lines that match the parrten
chang lines that match the parrten
chang lines that match the parrten

$ sed '2,3c\
quote> this line chang the 2 to 3 line of text.' data6
This is line number 1.
this line chang the 2 to 3 line of text.
This is line number 4.
This is line number 5.


4、写入文件

$ sed '2,3w tsetw' data6
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.

$ cat tsetw
This is line number 2.
This is line number 3.


5、读入文件

$ cat data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog

The quick green elephant jumps over the fox dog

指定行地址读入

$ sed '3r tsetw' data1
this test inside test
this test inside test
this si inside test
This is line number 2.
This is line number 3.
this si inside test
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog


指定匹配模式的行地址读入

$ sed '/dog/r tsetw' data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog
This is line number 2.
This is line number 3.
The quick green elephant jumps over the fox dog
This is line number 2.
This is line number 3.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值