文本处理三剑客之sed用法三例

利用sed 取出ifconfig命令中本机的IPv4地址

#回顾:无sed的取法
#1. 传统用法cut
[root@centos7 ~]# ifconfig ens33|grep netmask
        inet 192.168.44.10  netmask 255.255.255.0  broadcast 192.168.44.255
[root@centos7 ~]# ifconfig ens33|grep netmask|tr -s " "
 inet 192.168.44.10 netmask 255.255.255.0 broadcast 192.168.44.255
[root@centos7 ~]# ifconfig ens33|grep netmask|tr -s " "|cut -d " " -f3
192.168.44.10
-------------------------------------------------------------------------------------------------------------------------------
#2. grep的取法

#法1 思想:((0-9|10-99|100-199|200-255).){3}(0-9|10-99|100-199|200-255)
ifconfig ens33|grep -Eo "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})"

[root@centos7 ~]# ifconfig ens33|grep -Eo "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})"
192.168.44.10
255.255.255.0
192.168.44.255
-------------------------------------------------------------------------------------------------------------------------------
#法2 思想:((0-9|10-99|100-199|200-249|250-255).){3}(0-9|10-99|100-199|200-249|250-255)

[root@centos7 ~]# ifconfig ens33|grep -Eo "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})"
192.168.44.10
255.255.255.0
192.168.44.255
-------------------------------------------------------------------------------------------------------------------------------
#sed的用法
#法1 centos7 搜索替代
[root@centos7 ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.44.10  netmask 255.255.255.0  broadcast 192.168.44.255
        inet6 fe80::87ae:4ddc:8426:c8f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:33:b1:78  txqueuelen 1000  (Ethernet)
        RX packets 4083  bytes 3516633 (3.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1370  bytes 220951 (215.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@centos7 ~]# ifconfig ens33|sed -n '2p'
        inet 192.168.44.10  netma1sk 255.255.255.0  broadcast 192.168.44.255
[root@centos7 ~]# 
[root@centos7 ~]# ifconfig ens33|sed -n '2p'
        inet 192.168.44.10  netmask 255.255.255.0  broadcast 192.168.44.255
[root@centos7 ~]# ifconfig ens33|sed -n '2p'|sed 's/^.*inet//'
 192.168.44.10  netmask 255.255.255.0  broadcast 192.168.44.255
[root@centos7 ~]# ifconfig ens33|sed -n '2p'|sed 's/^.*inet//'|sed 's/netmask.*$//'
 192.168.44.10  
 
 #合成一句
 [root@centos7 ~]# ifconfig ens33|sed -n '2p'|sed 's/^.*inet//;s/netmask.*$//'  
 192.168.44.10 
 [root@centos7 ~]# ifconfig ens33|sed -n '2s/^.*inet//;s/netmask.*$//p'
 192.168.44.10 
 
 # -e 多点编辑 相当于;
 [root@centos7 ~]# ifconfig ens33|sed -n '2p'|sed -e 's/^.*inet//' -e 's/netmask.*$//'
 192.168.44.10 
------------------------------------------------------------------------------------------------------------------------------- 
 #法2 匹配IP的正则表达式
 #IP地址的形式,三个点隔成的四个数,数字最小1位,最大3位255
 #也就是最小7个,最大15个
  0.0.0.0
  255.255.255.255
  
 
[root@centos7 ~]# ifconfig ens33|grep -o '[0-9.]\{7,15\}'  
192.168.44.10
255.255.255.0
192.168.44.255
3703759
[root@centos7 ~]# ifconfig ens33|grep -o '[0-9.]\{7,15\}'|head -1
#不准确
192.168.44.10

-------------------------------------------------------------------------------------------------------------------------------
#法3 后项引用 可以做到通用centos6eth0也能取出来
[root@centos7 ~]# ifconfig ens33|sed -n '2p'|sed -r 's/(^[^0-9]+)([0-9.]+)(.*$)/\2/'
192.168.44.10
[root@centos7 ~]# ifconfig ens33|sed -nr '2s/(^[^0-9]+)([0-9.]+)(.*$)/\2/p'          
192.168.44.10
[root@centos7 ~]# ifconfig ens33|sed -nr '2s/^[^0-9]+([0-9.]+).*$/\1/p'          
192.168.44.10

删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

[root@centos7 test]# cp /etc/fstab ./
[root@centos7 test]# ls
2020-03-09.tar.gz  fstab
#将其拷贝当前目录下,不对源文件进行修改
[root@centos7 test]# sed -rn 's@^#[[:blank:]]+(.*)@\1@p' fstab 
#直接搜索替换
/etc/fstab
Created by anaconda on Tue Mar  3 08:08:26 2020
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

处理/etc/fstab路径,使用sed命令取出其目录名和基名

#常规用法 /1为目录名 /2为基名
[root@centos7 test]# echo /etc/fstab |sed -rn 's#(^/.*/)(.*)#\1\2#p'
/etc/fstab
[root@centos7 test]# echo /etc/fstab |sed -rn 's#(^/.*/)(.*)#\1 \2#p'
/etc/ fstab
[root@centos7 test]# echo /etc/fstab/baba/aa |sed -rn 's#(^/.*/)(.*)#\1 \2#p'
/etc/fstab/baba/ aa
[root@centos7 test]# echo /ssss/ssde/ |sed -rn 's#(^/.*/)(.*)#\1 \2#p'  
#测试带斜线的目录就失败了
/ssss/ssde/ 
-------------------------------------------------------------------------------------------------------------------------------
#改进 方法2
#以/开头后跟任意字符任意次并后加斜线匹配目录  以非斜线开头可以出现任意其尾部的斜线可以出现0次或一次就能匹配出带斜线的基名或不带斜线的基名
[root@centos6 ~]# echo /etc/sysconfig/network-scripts/|sed -r 's@(^/.*/)([^/]+)/?$@\1@'
/etc/sysconfig/
[root@centos6 ~]# echo /etc/sysconfig/network-scripts/|sed -r 's@(^/.*/)([^/]+)/?$@\2@'
network-scripts
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容