Linux学习----sed命令

sed命令

sed基本用法:

sed  :流编辑器  Stream EDitor。  操作纯ASCII 文本。
             行编辑器 (另一种是全屏编辑器)
             

模式空间:

逐行读取到内存空间中,在内存中处理。 处理完成后,将模式空间的内容显示在屏幕上。默认并不对原文件进行任何处理,仅对模式空间中的数据进行处理。

sed   [options]  'AddressCommand'   file ......
          -n: 静默模式,不再默认显示模式 
          -i : 直接修改原文件。
          -e  script   -e  script: 同时执行多个脚本。
          -f   script-file :  指定要执行的脚本文件
          -r :  扩展正则表达式

Address:
1、 起始行 ,结束行    例如: 1,100  第一行到第100行
        $  :文件的最后一行,  $-1,$-2存疑
2、 /RegExp/   使用正则表达式指定模式。  例如:  /^root/  以root 开始的行
3、 /pattern1/,/pattern2/  两个模式,从第一次匹配到模式1的行到 第一次匹配到模式2的行。
4、 某个行号 linenumber。  指定的行
5、起始行, +n   从指定行开始,向后的n行,包括指定行。


Command:
       d:删除符合条件的行

       
[root@localhost ~]# sed "1,2d" /etc/fstab 
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@localhost ~]# sed "1,10d" /etc/fstab 
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@localhost ~]# sed "/swap/d" /etc/fstab 

    p:显示符合条件的行


[root@localhost ~]# sed "/^\//p" /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

[root@localhost ~]# sed -n "/^\//p" /etc/fstab 
/dev/mapper/centos-root /                       xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

     a:在匹配到的行后面新增一行。   a \string


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

#
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
# hello world
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
# hello world

[root@localhost ~]# sed  "/^\//a \# hello world\n#hello linux" /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
# hello world
#hello linux
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
# hello world
#hello linux

         i :在匹配到的行前面新增一行。    i \string

[root@localhost ~]# sed  "/^\//i \# hello world\n#hello linux" /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
# hello world
#hello linux
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot                   xfs     defaults        0 0
# hello world
#hello linux
/dev/mapper/centos-swap swap                    swap    defaults        0 0

     r  FILE : 将指定文件的内容添加至指定行后,可用于合并文件。

[root@localhost ~]# sed "3r /etc/issue" /etc/fstab 

#
# /etc/fstab
\S
Kernel \r on an \m

# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@localhost ~]# cat /etc/issue
\S
Kernel \r on an \m

 

      w  FILE: 将指定范围的行内容另存至指定的文件夹。

[root@localhost ~]# sed "/swap/w /tmp/swap.txt"  /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@localhost ~]# cat /tmp/swap.txt 
/dev/mapper/centos-swap swap                    swap    defaults        0 0
    

         s : 查找并替换。  s/pattern/string/  将每一行第一次被匹配到pattern的字符串替换成string。  s###,s@@@  分隔符保持一致即可。
                 加修饰符
                 g:全局替换  s/pattern/string/g
                 i :查找时,忽略大小写

[root@localhost ~]# sed  "s/oot/OOT/" /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
/dev/mapper/centos-rOOT /                       xfs     defaults        0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /bOOT                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

[root@localhost ~]# sed  "s/swap/SWAP/" /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-SWAP swap                    swap    defaults        0 0

[root@localhost ~]# sed  "s/swap/SWAP/g" /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 /boot                   xfs     defaults        0 0
/dev/mapper/centos-SWAP SWAP                    SWAP    defaults        0 0

[root@localhost ~]# sed "s@/@#@g" /etc/fstab 

#
# #etc#fstab
# Created by anaconda on Sat Apr 16 18:39:54 2016
#
# 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
#
#dev#mapper#centos-root #                       xfs     defaults        0 0
UUID=0c8c0c2c-d3b8-4658-b899-0fed6a2e94d2 #boot                   xfs     defaults        0 0
#dev#mapper#centos-swap swap                    swap    defaults        0 0

         &:引用模式匹配到的整个内容。

[root@localhost ~]# sed "[email protected]@l..er@g" /root/sed.txt 
my l..er,
I l..er you.
[root@localhost ~]# sed "[email protected]@&r@g" /root/sed.txt 
my lover,
I liker you.
[root@localhost ~]# sed "s@\(l..e\)@\1r@g" /root/sed.txt    \\后向引用
my lover,
I liker you.

[root@localhost ~]# sed "s@l\(..e\)@L\1@g" /root/sed.txt  \\将l  --> L
my Love,
I Like you.

练习:

1、删除/etc/grub2.cfg 文件的行首的空白符


2、替换/etc/inittab 中multi-user.target: analogous to runlevel 3  的3 为 5
3、删除/etc/inittab 中的开头的#号

[root@localhost etc]# sed "s@^#@@g" /etc/inittab 
3、删除/etc/inittab 中的开头的#号以及空白字符,#后有空白字符。

[root@localhost tmp]# sed "s@^#[[:space:]]\{1,\}@@g" /etc/inittab
      删除某文件中空白字符后面跟#的的行中的空白字符以及#

[root@localhost tmp]# cat /tmp/sed.txt 
#asdf
# hello world
    # lady gaga
[root@localhost tmp]# sed "s@[[:space:]]\{1,\}#@@g" /tmp/sed.txt 
#asdf
# hello world
 lady gaga
4、删除/etc/inittab中的空白行

[root@localhost tmp]# sed "/^$/d" /tmp/blandline.txt 

5、取出一个文件路径的父目录名称。
[root@localhost tmp]# echo "/etc/rc.d" | sed -r "s@^(/.*/)[^/]+/?@\1@g"
/etc/

[root@localhost tmp]# echo "/etc/lsm/lsmd.conf" | sed -r "s@^/.*/([^/]+/?)@\1@g"  \\取出文件名
lsmd.conf





    















你可能感兴趣的:(Linux学习----sed命令)