利用sed 取出ifconfig命令中本機的IPv4地址
sed 常用選項:
編輯命令:
-d //刪除模式空間內容
p //顯示模式空間內容,-n(不顯示)
a \Text //行後追加文字
i \Text //行前插入文字
c \Text //替換文字
w FileName //儲存匹配的內容到指定路徑檔案中
r //讀取指定路徑文字至指定位置 //sed '6r /etc/fstab' /tmp/fstab
= //為匹配的內容列印行號
! //取反模式空間
s/查詢模式空間內容/替換內容/g //查詢替換全域性(g表示每一行出現的所有)
替換標記:g 行內全域性 p 顯示替換成功的行 w 結果儲存至指定檔案
& //引用前面匹配內容
[root@centos ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.50.130 netmask 255.255.255.0 broadcast 192.168.50.255
inet6 fe80::20c:29ff:fe33:c9d4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:33:c9:d4 txqueuelen 1000 (Ethernet)
RX packets 24622 bytes 34273572 (32.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4389 bytes 500399 (488.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 6 bytes 380 (380.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 380 (380.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:c9:ca:ee txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos ~]# ifconfig ens33 | sed -n '/inet /s#\(.*inet \)\(.*\)\( n.*\)#\2#gp'
192.168.50.130