hopeless-dream

导航

sed命令

命令作用

  stream editor 非交互式文本流编辑器。

语法:

  sed [OPTION]... {script-only-if-no-other-script} [input-file]...

工作流程:

  sed不是在源文件上直接进行处理的(非就地修改),而是先将读入的行放到缓冲区中,对缓冲区里的内容进行处理,处理完毕后也不会写回原文件(除非用shell的输出重定向来保存结果),而是直接输出到屏幕上。sed运行过程中维护着两个缓冲区,一个是活动的“模式空间(pattern space)”,另一个是起辅助作用的“保持空间(holding space)”。一般情况下,每当运行sed,sed首先把第一行装入模式空间,进行处理后输出到屏幕,然后将第二行装入模式空间替换掉模式空间里原来的内容,然后进行处理,以此类推。

 

常用选项:

  -n, --quiet, --silent     # 不输出模式空间中的内容至标准输出

  -e           # 多点编辑

  -f script-file                         # 从指定文件中读取编辑脚本

  -r, --regexp-extended         # 使用扩展正则表达式

  -i                                        # 就地修改源文件(慎用,生产操作是需要先不加此参数,确保修改没有问题后,使用-i.bak做备份)

地址定界

  (1)不给定地址:表示对全文进行处理

  (2)单地址:

    n:指定的行

    /pattern/:指定pattern能够匹配的每一行

  (3)地址范围:

    m,n:从第m行到第n行

    m,+n:从第m行到第m+n行

    /pattern1/,/pattern2/:从匹配pattern1的行到匹配pattern2的行

    m,/pattern1/ 从第m行到匹配pattern1的行

编辑命令

1、d:删除

[root@localhost ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

指定匹配模式删除
[root@localhost ~]# sed '/^UUID/d' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#

指定地址删除
[root@localhost ~]# sed '1,4d' /etc/fstab 
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 / xfs defaults 0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot xfs defaults 0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap swap defaults 0 0

指定某一行到匹配模式删除
[root@localhost ~]# sed '5,/^UUID/d' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0


[root@localhost ~]# sed '/^#/,/^UUID/d' /etc/fstab 

UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

2、p 打印模式空间中的内容

sed命令默认打印“模式空间“中的内容,所以加p指令后会将“模式空间”内容打印两次,使用-n选项可以禁止打印“模式空间”的内容
[root@localhost ~]# sed '/^UUID/p' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

[root@localhost ~]# sed -n '/^UUID/p' /etc/fstab 
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

3、a \ 'text' (append)在地址定界的行后面追加文本

[root@localhost ~]# sed '/^UUID/a \"hello world."' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
"hello world."
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
"hello world."
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0
"hello world."

[root@localhost ~]# sed '1,3a \"hello world."' /etc/fstab 

"hello world."
#
"hello world."
# /etc/fstab
"hello world."
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

4、i \ 'text' (insert)在地址定界的行前面插入文本

[root@localhost ~]# sed '1,3i \"hello world."' /etc/fstab 
"hello world."

"hello world."
#
"hello world."
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

5、c \'text' (change)将匹配内容的行,替换成指定文本    

[root@localhost ~]# sed '/^#/c \"hello world."' /etc/fstab 

"hello world."
"hello world."
"hello world."
"hello world."
"hello world."
"hello world."
"hello world."
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

6、w filename 将“模式空间”的内容写到指定文件,会替换目标文件内容

[root@localhost ~]# sed '/^UUID/w /tmp/fstab.txt' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0
[root@localhost ~]# cat /tmp/fstab.txt 
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0
[root@localhost ~]# cat >> 1.txt <<EOF
> 123
> dsfa
> tom
> tomcat
> json
> EOF

[root@localhost ~]# sed '3,5 w /tmp/fstab.txt' 1.txt 
1
123
dsfa
tom
tomcat
json
[root@localhost ~]# cat /tmp/fstab.txt 
dsfa
tom
tomcat

7、r filename 读取指定文件中的内容至“模式空间”匹配行的行后

[root@localhost ~]# sed '/^UUID/r /etc/issue' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
\S
Kernel \r on an \m

UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
\S
Kernel \r on an \m

UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0
\S
Kernel \r on an \m

8、= “模式空间”中的行打印行号  

[root@localhost ~]# sed '/^UUID/=' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
9
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
10
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
11
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

9、!编辑命令   表示对地址定界取反

[root@localhost ~]# sed '/^UUID/d' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
[root@localhost ~]# sed '/^UUID/!d' /etc/fstab 
UUID=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
UUID=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
UUID=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

10、s/pattern/text/  将匹配到的内容pattern,替换为指定文本text 可以使用其他分隔符,如:s@@@,s###

    替换标记:

      g  行内全部替换

      p    显示成功替换的行

      w filename   将替换成功的行写入其他文件   

[root@localhost ~]# sed 's@^UUID@uuid@' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 29 08:50:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
uuid=0bbd5e50-606c-4c47-8cd7-1ae67f812437 /                       xfs     defaults        0 0
uuid=bba2c917-8540-41c8-97e6-f1d73d9143ba /boot                   xfs     defaults        0 0
uuid=1c0f8351-49f0-4dd8-9a8b-1aff1d4a77b0 swap                    swap    defaults        0 0

[root@localhost ~]# cat >>num.txt<<EOF
> 0 0 0 0 0
> 0 0 0 0 0
> 0 0 0 0 0
> 0 0 0 0 0
> EOF

[root@localhost ~]# sed '2,4s@0@1@' num.txt 
0 0 0 0 0
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0

[root@localhost ~]# sed '3s@0@1@2' num.txt 
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0

[root@localhost ~]# sed '3s@0@1@2g' num.txt 
0 0 0 0 0
0 0 0 0 0
0 1 1 1 1
0 0 0 0 0

 [root@localhost ~]# sed -n '3s@. .@&1@gpw a.txt' num.txt
 0 01 0 01 0

 [root@localhost ~]# cat a.txt
 0 01 0 01 0

11、& 后项引用

[root@localhost ~]# sed '3s@. .@&1@' num.txt 
0 0 0 0 0
0 0 0 0 0
0 01 0 0 0
0 0 0 0 0

[root@localhost ~]# sed '3s@. .@&1@2g' num.txt 
0 0 0 0 0
0 0 0 0 0
0 0 0 01 0
0 0 0 0 0

    [root@localhost ~]# sed -n '3s@. .@&1@gp' num.txt
    0 01 0 01 0

[root@localhost ~]# sed '3s@. .@&1@g' num.txt 
0 0 0 0 0
0 0 0 0 0
0 01 0 01 0
0 0 0 0 0  

删除文件行首的空白字符

[root@localhost ~]# cat num.txt 
 0 0 0 0 0
 0 0 0 0 0
 0 0 0 0 0
 0 0 0 0 0
[root@localhost ~]# sed  's/ //' num.txt 
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 

高级用法

  sed的编辑操作都在“模式空间(pattern space)”中进行,脚本中的命令依次对这一行进行处理,直到脚本处理结束,处理后的内容打印至屏幕,清除“模式空间”,然后重复此流程处理下一行,直到文件处理完。但有时候需要保留“模式空间”中的内容,以便于后续处理,就需要用到“保持空间(holding space)”高级命令 

高级编辑命令:

  h:把模式空间中的内容覆盖到保持空间

  H:把模式空间中的内容追加到保持空间

  g:从保持空间取出数据覆盖到模式空间

       G:从保持空间取出数据追加到模式空间

  x:把模式空间的内容和保持空间内容互换

  n:读取匹配行的下一行到模式空间

  N:追加匹配行的下一行至模式空间

  d:删除模式空间中的行

  D:清空模式空间

n选项:打印偶数行

[root@localhost ~]# echo -e "1\n2\n3\n4\n5\n6" > num.txt

[root@localhost ~]# sed -n 'n;p' num.txt 
2
4
6  

 逆向显示文件内容

[root@localhost ~]# sed '1!G;h;$!d' num.txt 
6
5
4
3
2
1

处理过程描述:
    先把第一行读到模式空间,判断是否是第一行,如果是,不做G操作,然后把这一行覆盖至    保持空间,由于不是最后一行,所以也不做d操作,依次处理文件的每一行

其他命令:

  [root@localhost ~]# tac num.txt
  6
  5
  4
  3
  2
  1

打印文件最后两行

[root@localhost ~]# sed '$!N;$!D' num.txt 
5
6  

将多个空白行替换成一个空白行

[root@localhost ~]# sed '/^$/d;G' num.txt 
1

2

3

4

5

6  

label标签用法

b a  ; 表示无条件到:a 这里继续执行
例子 ':a;$!N;$!ba;$s/\n/g'  所有内容 一行输出 循环作用
[root@localhost ~]# cat num.txt 
1
1
3
4
5
6

[root@localhost ~]# sed ':a;$!N;$!b a;$s/\n//g' num.txt 
113456


t a  ;   表示前面的s/   /   成功比配到 了 变 跳转到:a继续执行 

例子':a;N;s/aa\nbb/woyn/;ta;P;D'
[root@localhost ~]# vim num.txt 
1
1
3
4
5
6

[root@localhost ~]# sed -n ':a;N;s/1/2/;t a;p' num.txt 
2
2
3
4
5
6

  

 

 

  

  

  

   

  

  

 

  

 

posted on 2020-01-30 15:10  hopeless-dream  阅读(343)  评论(0编辑  收藏  举报