sed流编辑器

sed 是一种在线编辑器,它一次处理一行内容。
处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。
sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

sed命令的格式

  • sed 选项 '动作' 文件名
  • cat 文件名 | sed 选项 '动作'

选项:
-n :只显示被修改的行的内容
-e :直接在命令列模式上进行 sed 的动作编辑;
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
-r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改读取的文件内容,而不是输出到终端。

动作:
a :在指定行后 新增一行或多行内容
c :替换指定行的内容
d :删除指定行的内容
i :在指定行之前 插入一行或多行内容
s :替换指定内容

 

删除行内容

1、删除行内容删除第2行的内容

  • cat file.txt | sed '2d'
  • sed '2d' file.txt

2、删除第2-5行的内容

  • cat file.txt | sed '2,5d'
  • sed '2,5d' file.txt

3、删除第2行到最后一行的内容

  • cat file.txt | sed '2,$d'
  • sed '2,$d' file.txt

 

新增、插入 行内容

1、在第2行后新增内容

  • cat file.txt | sed '2a hello world'

2、在第2行前插入内容

  • cat file.txt | sed '2i hello world

3、在第2行后新增2行内容

  • cat file.txt | sed '2a hello\nworld'

 

替换行内容

1、把第2行的内容替换为 hello world

  • cat file.txt | sed '2c hello world'

2、把第2-5行的内容替换为 hello world

  • cat file.txt | sed '2,5c hello world'

3、只显示被修改的行的内容

  • cat file.txt | sed -n '2c hello world'

 

搜索指定内容

1、搜索含有abc的行

  • cat file.txt | sed -n '/abc/p'

2、搜索并删除指定内容

  • cat file.txt | sed '/abc/d'

 

替换指定内容

1、默认只替换每行中模式首次匹配的内容

  • cat file.txt | sed 's/abc/'替换后的内容'/'

2、g标记可以使sed执行全局替换

  • cat file.txt | sed 's/abc/'替换后的内容'/g'

3、g标记可以使sed替换第N次出现的匹配

  • sed 's/abc/xxx/2g' file.txt

 

直接操作文件内容(危险动作)

1、直接删除文件内容

  • sed -i '2,5d' file

2、直接替换文件内容

  • sed -i 's/abc/xxx/g' file

 

在sed中使用正则表达式

1、搜索并删除空行

  • sed '/^$/d' file.txt

2、替换3位数的数字

[root@linuxprobe test_shell]# cat b.txt
a 1 b 22 c 333 d 4444
[root@linuxprobe test_shell]# sed 's/\b[0-9]\{3\}\b/666/g' b.txt
a 1 b 22 c 666 d 4444

[root@linuxprobe test_shell]# cat file
a1
b22
c333
d4444
ab12abc123abcd1234
[root@linuxprobe test_shell]# sed 's/[0-9]\{3\}/666/g' file
a1
b22
c666
d6664
ab12abc666abcd6664

3、操作文件内容时,备份原文件

[root@linuxprobe test_shell]# cat file
xxx 123 xxx123 123xxx
[root@linuxprobe test_shell]# sed -i.bak 's/abc/xxx/g' file
[root@linuxprobe test_shell]# cat file
xxx 123 xxx123 123xxx
[root@linuxprobe test_shell]# ls
b.txt  file  file.bak  test.sh
[root@linuxprobe test_shell]# cat file.bak
xxx 123 xxx123 123xxx

4、已匹配字符串标记( & )

在 sed 中 & 指代模式所匹配到的字符串

[root@linuxprobe test_shell]# cat file
abc 123 abc123 123abc
[root@linuxprobe test_shell]# sed 's/abc/[&]/g' file
[abc] 123 [abc]123 123[abc]

5、子串匹配标记( \1  \2 \3 ......)

在sed中 \1  \2 \3 指代出现在括号中的部分正则表达式所匹配到的内容

[root@linuxprobe test_shell]# echo abc 123 | sed 's/abc \([0-9]\)/\1/'
123

对于匹配到的第一个子串,其对应的标记是 \1 ,匹配到的第二个子串是 \2 ,往后以此类推。

[root@linuxprobe test_shell]# echo HELLO world | sed 's/\([A-Z]\+\) \([a-z]\+\)/\2 \1/'
world HELLO

6、组合多个表达式

[root@linuxprobe test_shell]# echo abc 123 | sed 's/[a-z]\+/xxx/' | sed 's/[0-9]\+/666/'
xxx 666
[root@linuxprobe test_shell]# echo abc 123 | sed 's/[a-z]\+/xxx/;s/[0-9]\+/666/'
xxx 666
[root@linuxprobe test_shell]# echo abc 123 | sed 's/[a-z]\+/xxx/;s/[0-9]\+/666/'
xxx 666

7、引用变量

[root@linuxprobe test_shell]# echo user | sed "s/user/$USER/"
root

备注:在双引号中,可以引用变量。



 

转载于:https://www.cnblogs.com/lfjn/p/11455970.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值