码迷,mamicode.com
首页 > 其他好文 > 详细

sed

时间:2019-09-05 14:08:45      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:技术   lan   后向引用   run   取消   glob   范围   oba   显示   

1.1用法与原理
?sed [option]… ‘script’ file

技术图片

1.2常用选项

?-n:取消默认输出
?-e:多点编辑
?-f :/PATH/TO/SCRIPT_FILE从指定文件中读取编辑脚本
?-r:支持使用扩展正则表达式

1.3地址定界


?(1)不给地址:对全文进行处理

?(2)单地址:
#:指定的行
    /pattern/:被此处模式所能够匹配到的每一行

?(3)地址范围:
   #,#
   #,+#
   /pat1/,/pat2/
   #,/pat1/
   #;#;#分隔的多行

1.3.1实践操作

环境准备
[root@oldboyedu-02 oldboy]# cat person.txt 
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
?-n取消默认输出,只打印想要的内容
[root@oldboyedu-02 oldboy]# sed ‘1p‘ person.txt 
101,oldboy,CEO
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
[root@oldboyedu-02 oldboy]# sed -n ‘1p‘ person.txt 
101,oldboy,CEO

?连续多行
[root@oldboyedu-02 oldboy]# sed -n ‘2,4p‘ person.txt 
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO

?找出包含oldboy的行 到 包含104的行
[root@oldboyedu-02 oldboy]# sed -n ‘/oldboy/,/^104/p‘ person.txt 
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO

[root@oldboyedu-01 ~]# awk ‘/root/,/operator/{print $0}‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

?包含oldboy的行 或 yy的行
[root@oldboyedu-02 oldboy]# sed -rn ‘/oldboy|yy/p‘ person.txt
101,oldboy,CEO
104,yy,CFO
[root@oldboyedu-02 oldboy]# egrep ‘oldboy|yy‘ person.txt
101,oldboy,CEO
104,yy,CFO

?打印分隔的多行
[root@oldboyedu-02 oldboy]# sed -n ‘1p;2p;4p‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
104,yy,CFO

?打印第二行和后面三行
[root@oldboyedu-01 ~]# sed -n ‘2,+3p‘ /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

?打印第二行到包含root的行
[root@oldboyedu-01 ~]# sed -n ‘2,/root/p‘ /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@oldboyedu-01 ~]# awk ‘NR==2,/root/‘ /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

1.4编辑命令

?d:删除
?p:显示模式空间中的内容
?a \text:在行后面追加文本,支持使用\n实现多行追加
?i \text:在行前面插入文本,支持使用\n实现多行插入
?c \text:替换行尾单行或多行文本
?w /path/to/somefile:保存模式空间匹配到的行到指定文件中
?r /path/from/somefile:读取指定文件的文本流到模式空间中匹配到的行后
?=:为模式空间中的行打印行号
?!:取反条件
?s///g:支持使用其他分隔符,s###g,s@@@g
?s:替换每行的第一处匹配的内容
?g:行内全局替换
?p:显示替换成功的行
?w:/path/to/somefile:将替换成功的结果保存到指定文件中
?&表示被替换的内容

1.4.1实践操作

?a 追加append,在指定行后添加一行或多行文本
 [root@oldboyedu-02 oldboy]# sed  ‘3a 103.5,xiaoyu,CXO‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
103.5,xiaoyu,CXO
104,yy,CFO
105,feixue,CIO

?i 插入insert,在指定行前添加一行或多行文本
[root@oldboyedu-02 oldboy]# sed  ‘3i 103.5,xiaoyu,CXO‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103.5,xiaoyu,CXO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
单行追加
echo ‘test‘ >>person.txt

?增加多行
[root@oldboyedu-02 oldboy]# sed ‘$a 106\n107\n108‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106
107
108
下面比较常用
[root@oldboyedu-02 oldboy]# cat >>person.txt<<EOF
> 106 106
> 107 107
> EOF

?删除
[root@oldboyedu-02 oldboy]# cat -A person.txt
101,oldboy,CEO$
$
102,zhangyao,CTO$
103,Alex,COO$
$
104,yy,CFO$
105,feixue,CIO$
106 106$
107 107$

[root@oldboyedu-02 oldboy]# sed ‘/^$/d‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106 106
107 107

[root@oldboyedu-02 oldboy]# sed -n ‘/^$/p‘ person.txt

[root@oldboyedu-02 oldboy]# sed -n ‘/^$/!p‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106 106
107 107

[root@oldboyedu-02 oldboy]# sed ‘$d‘ person.txt
101,oldboy,CEO

102,zhangyao,CTO
103,Alex,COO

104,yy,CFO
105,feixue,CIO
106 106

[root@oldboyedu-02 oldboy]# sed ‘$!d‘ person.txt
107 107
 [root@oldboyedu-02 oldboy]#

[root@oldboyedu-02 oldboy]# awk ‘!/^$/‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106 106
107 107

[root@oldboyedu-02 oldboy]# egrep -v ‘^$‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106 106
107 107

?替换
-i修改文件内容
-i.bak 先把源文件备份,然后替换内容
s单独使用,将每一行中的第一处匹配的字符串进行替换
g global 每一行进行全部替换

[root@oldboyedu-02 oldboy]# sed -i.bak ‘s#o#AAAAA#g‘ person.txt
[root@oldboyedu-02 oldboy]# cat person.txt.bak 
101,oldboy,CEO

102,zhangyao,CTO
103,Alex,COO

104,yy,CFO
105,feixue,CIO
106 106
107 107

[root@oldboyedu-02 oldboy]# cat person.txt
101,AAAAAldbAAAAAy,CEO

102,zhangyaAAAAA,CTO
103,Alex,COO

104,yy,CFO
105,feixue,CIO
106 106
107 107

含有变量的替换时,要使用""
[root@oldboyedu-02 oldboy]# x=oldboy
[root@oldboyedu-02 oldboy]# y=oldgirl
[root@oldboyedu-02 oldboy]# cat person.txt
101,oldboy,CEO

102,zhangyao,CTO
103,Alex,COO

104,yy,CFO
105,feixue,CIO
106 106
107 107

[root@oldboyedu-02 oldboy]# sed "s#$x#$y#g" person.txt
101,oldgirl,CEO

102,zhangyao,CTO
103,Alex,COO

104,yy,CFO
105,feixue,CIO
106 106
107 107

?删除/boot/grub/grub.conf文件中所有以空白开头的行的行首的空表字符
[root@oldboyedu-01 oldboy]# cat /boot/grub/grub.conf 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda3
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-696.el6.x86_64.img

[:blank:]                  空白字符(空格和制表符)
[:space:]       \s         所有空格符(比[:blank:]包含的范围广)
使用[:space:]
[root@oldboyedu-01 oldboy]# sed -r ‘#^[[:space:]]##g‘ /boot/grub/grub.conf 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda3
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-696.el6.x86_64.img
[root@oldboyedu-01 oldboy]# 
使用[ ]代表空格不好使,通过vim编辑器查看,也不是空格
[root@oldboyedu-01 oldboy]# sed -r ‘s#^[ ]+##g‘ /boot/grub/grub.conf 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda3
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-696.el6.x86_64.img
[root@oldboyedu-01 oldboy]# 

?删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
[root@oldboyedu-01 oldboy]# cat /etc/fstab |sed -r ‘s#^[#][ ]+##g‘

#
/etc/fstab
Created by anaconda on Mon Feb 18 12:19:50 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=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 /                       ext4    defaults        1 1
UUID=8848e519-989b-4a1f-9fdb-803f0b56db57 /boot                   ext4    defaults        1 2
UUID=b6a11888-75c1-4a5f-98fa-32c323404e4e 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@oldboyedu-01 oldboy]# cat /etc/fstab |sed -r ‘s#^[#][[:space:]]+##g‘

#
/etc/fstab
Created by anaconda on Mon Feb 18 12:19:50 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=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 /                       ext4    defaults        1 1
UUID=8848e519-989b-4a1f-9fdb-803f0b56db57 /boot                   ext4    defaults        1 2
UUID=b6a11888-75c1-4a5f-98fa-32c323404e4e 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@oldboyedu-01 oldboy]# 
要使用[[:space:]],如下[:space:]是不管用的
[root@oldboyedu-01 oldboy]# cat /etc/fstab |sed -r ‘s#^[#][:space:]+##g‘

#
# /etc/fstab
# Created by anaconda on Mon Feb 18 12:19:50 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=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 /                       ext4    defaults        1 1
UUID=8848e519-989b-4a1f-9fdb-803f0b56db57 /boot                   ext4    defaults        1 2
UUID=b6a11888-75c1-4a5f-98fa-32c323404e4e 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@oldboyedu-01 oldboy]# 

?echo一个绝对路径给sed命令,取出其基名,取出其目录名
[root@m01 oldboy]# echo "/opt/test/wew" |sed -r ‘s#.*/##g‘
wew

?删除UUID开头的行
[root@m01 oldboy]# sed ‘/^UUID/d‘ /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Feb 18 12:19:50 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
#
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@m01 oldboy]# sed ‘/^$/d‘ /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Mon Feb 18 12:19:50 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=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 /                       ext4    defaults        1 1
UUID=8848e519-989b-4a1f-9fdb-803f0b56db57 /boot                   ext4    defaults        1 2
UUID=b6a11888-75c1-4a5f-98fa-32c323404e4e 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@m01 oldboy]# 

?符合条件的行后面追加内容
[root@m01 oldboy]# sed ‘/^UUID/a wewewew‘ /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Feb 18 12:19:50 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=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 /                       ext4    defaults        1 1
wewewew
UUID=8848e519-989b-4a1f-9fdb-803f0b56db57 /boot                   ext4    defaults        1 2
wewewew
UUID=b6a11888-75c1-4a5f-98fa-32c323404e4e swap                    swap    defaults        0 0
wewewew
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@m01 oldboy]# 

?打印符合条件行的行号
[root@m01 oldboy]# sed ‘/UUID/=‘ /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Feb 18 12:19:50 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=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 /                       ext4    defaults        1 1
10
UUID=8848e519-989b-4a1f-9fdb-803f0b56db57 /boot                   ext4    defaults        1 2
11
UUID=b6a11888-75c1-4a5f-98fa-32c323404e4e 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@m01 oldboy]# 

?!取反
[root@m01 oldboy]# sed -n ‘/UUID/p‘ /etc/fstab 
UUID=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 /                       ext4    defaults        1 1
UUID=8848e519-989b-4a1f-9fdb-803f0b56db57 /boot                   ext4    defaults        1 2
UUID=b6a11888-75c1-4a5f-98fa-32c323404e4e swap                    swap    defaults        0 0
[root@m01 oldboy]# sed -n ‘/UUID/!p‘ /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Feb 18 12:19:50 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
#
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@m01 oldboy]# 

?!替换追加,&代表前面匹配的内容
[root@m01 oldboy]# sed ‘s#UUID#&NEW#g‘ /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Feb 18 12:19:50 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
#
UUIDNEW=b3a88d2b-b491-46ff-96a1-a9eff1a5c3a5 /                       ext4    defaults        1 1
UUIDNEW=8848e519-989b-4a1f-9fdb-803f0b56db57 /boot                   ext4    defaults        1 2
UUIDNEW=b6a11888-75c1-4a5f-98fa-32c323404e4e 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@m01 oldboy]# 

1.5sed后向引用

sed -n ‘s#()()#\1\2#gp‘ file
当在前面匹配部分用小括号的时候,第一个括号内容,可以在后面部分用\1输出
第二个括号内容,可以在后面部分用\2输出
[root@m01 oldboy]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:BE:11:5D  
          inet addr:10.0.0.61  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:febe:115d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10361 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7372 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:970097 (947.3 KiB)  TX bytes:905329 (884.1 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:BE:11:67  
          inet addr:172.16.1.61  Bcast:172.16.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:febe:1167/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1041 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 b)  TX bytes:62568 (61.1 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:686 errors:0 dropped:0 overruns:0 frame:0
          TX packets:686 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:60368 (58.9 KiB)  TX bytes:60368 (58.9 KiB)

[root@m01 oldboy]# ifconfig|sed -nr ‘s/.*dr:(.*) Bc/\1/g‘
[root@m01 oldboy]# ifconfig|sed -nr ‘s/.*dr:(.*) Bc/\1/gp‘
10.0.0.61 ast:10.0.0.255  Mask:255.255.255.0
172.16.1.61 ast:172.16.1.255  Mask:255.255.255.0
[root@m01 oldboy]# ifconfig|sed -nr ‘s/.*dr:(.*) Bc.*/\1/gp‘
10.0.0.61 
172.16.1.61 
[root@m01 oldboy]# 
切换第一列和最后一列
[root@m01 oldboy]# sed -nr ‘s#(.*)(:.*:)(/.*$)#\3\2\1#gp‘ /etc/passwd
/bin/bash:/root:root:x:0:0:root
/sbin/nologin:/bin:bin:x:1:1:bin
/sbin/nologin:/sbin:daemon:x:2:2:daemon
/sbin/nologin:/var/adm:adm:x:3:4:adm
/sbin/nologin:/var/spool/lpd:lp:x:4:7:lp
/bin/sync:/sbin:sync:x:5:0:sync
/sbin/shutdown:/sbin:shutdown:x:6:0:shutdown

sed

标签:技术   lan   后向引用   run   取消   glob   范围   oba   显示   

原文地址:https://blog.51cto.com/10983441/2435739

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!