首页 > 代码库 > linux常用命令-sed

linux常用命令-sed

sed基本用法:
sed: Stream EDitor
 行编辑器 (全屏编辑器: vi)


sed: 将文本读入内存空间,模式空间
默认不编辑原文件,仅对模式空间中的数据做处理;而后,处理结束后,将模式空间打印至屏幕;


sed [options] ‘AddressCommand‘ file ...
 -n: 静默模式,不再默认显示模式空间中的内容
 -i: 直接修改原文件
 -e SCRIPT -e SCRIPT:可以同时执行多个脚本
 -f /PATH/TO/SED_SCRIPT
  sed -f /path/to/scripts  file
 -r: 表示使用扩展正则表达式


 
Address:
1、StartLine,EndLine
 比如1,100
 $:最后一行
2、/RegExp/
 /^root/
3、/pattern1/,/pattern2/
 第一次被pattern1匹配到的行开始,至第一次被pattern2匹配到的行结束,这中间的所有行
4、LineNumber
 指定的行
5、StartLine, +N
 从startLine开始,向后的N行;
 
Command:
 d: 删除符合条件的行;
 p: 显示符合条件的行;
 a \string: 在指定的行后面追加新行,内容为string
  \n:可以用于换行
 i \string: 在指定的行前面添加新行,内容为string
 r FILE: 将指定的文件的内容添加至符合条件的行处
 w FILE: 将地址指定的范围内的行另存至指定的文件中;
 s/pattern/string/修饰符: 查找并替换,默认只替换每行中第一次被模式匹配到的字符串
      加修饰符
      g: 全局替换
      i: 忽略字符大小写
 s///: s###, s@@@ 
  \(\), \1, \2
  
 l..e: like-->liker
    love-->lover
   
    like-->Like
    love-->Love
 
 &: 引用模式匹配整个串




练习:
#
# /etc/fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# 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=57d85756-7680-4c7c-9125-6ad67dae2c45 /                       ext4    defaults        1 1
UUID=2622a4b4-ddc9-47a3-aa2b-f06bc9bec085 /boot                   ext4    defaults        1 2
UUID=33d94759-fa01-4c4f-b4ac-bf3a1fe5e84f swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@localhost ~]# sed 1,2d /etc/fstab
# /etc/fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# 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=57d85756-7680-4c7c-9125-6ad67dae2c45 /                       ext4    defaults        1 1
UUID=2622a4b4-ddc9-47a3-aa2b-f06bc9bec085 /boot                   ext4    defaults        1 2
UUID=33d94759-fa01-4c4f-b4ac-bf3a1fe5e84f swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@localhost ~]#


练习:

[root@localhost ~]# sed ‘3,$d‘ /etc/fstab


#
[root@localhost ~]#


练习:

[rhel@localhost ~]$ sed ‘$r /etc/issue‘ /etc/fstab


#
# /etc/fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# 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=57d85756-7680-4c7c-9125-6ad67dae2c45 /                       ext4    defaults        1 1
UUID=2622a4b4-ddc9-47a3-aa2b-f06bc9bec085 /boot                   ext4    defaults        1 2
UUID=33d94759-fa01-4c4f-b4ac-bf3a1fe5e84f swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
Red Hat Enterprise  Server release 6.6 (Santiago)
\m


[rhel@localhost ~]$


练习:

[root@localhost ~]# sed ‘/oot/d‘ /etc/fstab


#
# /etc/fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# 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=57d85756-7680-4c7c-9125-6ad67dae2c45 /                       ext4    defaults        1 1
UUID=33d94759-fa01-4c4f-b4ac-bf3a1fe5e84f swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@localhost ~]#


练习:

[root@localhost ~]# sed -n ‘/^#/p‘ /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# 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 ~]#


练习:

[rhel@localhost ~]$
[rhel@localhost ~]$ sed ‘/oot/w /tmp/oot.txt‘ /etc/fstab


#
# /etc/fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# 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=57d85756-7680-4c7c-9125-6ad67dae2c45 /                       ext4    defaults        1 1
UUID=2622a4b4-ddc9-47a3-aa2b-f06bc9bec085 /boot                   ext4    defaults        1 2
UUID=33d94759-fa01-4c4f-b4ac-bf3a1fe5e84f swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[rhel@localhost ~]$ cat /tmp/oot.txt
UUID=2622a4b4-ddc9-47a3-aa2b-f06bc9bec085 /boot                   ext4    defaults        1 2
[rhel@localhost ~]$


练习:

[rhel@localhost ~]$ sed ‘s/oot/OOT/‘ /etc/fstab


#
# /etc/fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# 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=57d85756-7680-4c7c-9125-6ad67dae2c45 /                       ext4    defaults        1 1
UUID=2622a4b4-ddc9-47a3-aa2b-f06bc9bec085 /bOOT                   ext4    defaults        1 2
UUID=33d94759-fa01-4c4f-b4ac-bf3a1fe5e84f swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[rhel@localhost ~]$


练习:

[rhel@localhost ~]$ sed ‘s/\//#/g‘ /etc/fstab


#
# #etc#fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# 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=57d85756-7680-4c7c-9125-6ad67dae2c45 #                       ext4    defaults        1 1
UUID=2622a4b4-ddc9-47a3-aa2b-f06bc9bec085 #boot                   ext4    defaults        1 2
UUID=33d94759-fa01-4c4f-b4ac-bf3a1fe5e84f swap                    swap    defaults        0 0
tmpfs                   #dev#shm                tmpfs   defaults        0 0
devpts                  #dev#pts                devpts  gid=5,mode=620  0 0
sysfs                   #sys                    sysfs   defaults        0 0
proc                    #proc                   proc    defaults        0 0


练习:

[rhel@localhost ~]$ nano sed.txt
[rhel@localhost ~]$ sed ‘s/l..e/&r/g‘ ./sed.txt
liker
lover
[rhel@localhost ~]$ cat ./sed.txt
like
love
[rhel@localhost ~]$


练习:

[rhel@localhost ~]$ sed ‘s/l\(..e\)/L\1/g‘ ./sed.txt
Like
Love
[rhel@localhost ~]$


练习:

[root@localhost ~]# history | sed ‘s#^[[:space:]]*##g‘
1  history | sed ‘s#[[:space:]]*##g’
2   sed -r ‘s@^[[:space:]]+@@g‘ /etc/grub.conf
3  history | sed ‘s#[[:space:]]+##g’
4  history | sed ‘s#[[:space:]]+##g‘
5  history | sed ‘s#[[:space:]]*##g‘
6  history | sed ‘s#^[[:space:]]*##g‘



sed练习:
1、删除/etc/grub.conf文件中行首的空白符;
sed -r ‘s@^[[:spapce:]]+@@g‘ /etc/grub.conf
2、替换/etc/inittab文件中"id:3:initdefault:"一行中的数字为5;
sed ‘s@\(id:\)[0-9]\(:initdefault:\)@\15\2@g‘ /etc/inittab
3、删除/etc/inittab文件中的空白行;
sed ‘/^$/d‘ /etc/inittab
4、删除/etc/inittab文件中开头的#号;
sed ‘s@^#@@g‘ /etc/inittab
5、删除某文件中开头的#号及后面的空白字符,但要求#号后面必须有空白字符;
sed -r ‘s@^#[[:space:]]+@@g‘ /etc/inittab
6、删除某文件中以空白字符后面跟#类的行中的开头的空白字符及#
sed -r ‘s@^[[:space:]]+#@@g‘ /etc/inittab
7、取出一个文件路径的目录名称;
echo "/etc/rc.d/" | sed -r ‘s@^(/.*/)[^/]+/?@\1@g‘ 
基名:
echo "/etc/rc.d/" | sed -r ‘s@^/.*/([^/]+)/?@\1@g‘ 

linux常用命令-sed