sed - How to extract IP address using sed?

前端 未结 5 2111
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 23:36

This is for an assignment so I have no choice but to use sed.

Given a file messages, how can I extract all the IP addresses and print them?

I first

5条回答
  •  滥情空心
    2021-01-04 00:36

    If you use git-bash on Windows. It is quite handy.

     export LC_ALL=C; ipconfig | sed -n 's/IPv4//gp;' | sed -En 's/.*(10.*)/\1/gp' 
    
    以太网适配器 VMware Network Adapter VMnet1:
    
       连接特定的 DNS 后缀 . . . . . . . :
       本地链接 IPv6 地址. . . . . . . . : fe80::3177:bf7b:590:c787%6
       IPv4 地址 . . . . . . . . . . . . : 192.168.31.1
       子网掩码  . . . . . . . . . . . . : 255.255.255.0
       默认网关. . . . . . . . . . . . . :
    
    以太网适配器 VMware Network Adapter VMnet8:
    
       连接特定的 DNS 后缀 . . . . . . . :
       本地链接 IPv6 地址. . . . . . . . : fe80::c8de:747e:34fe:58cd%12
       IPv4 地址 . . . . . . . . . . . . : 192.168.239.1
       子网掩码  . . . . . . . . . . . . : 255.255.255.0
       默认网关. . . . . . . . . . . . . :
    
    以太网适配器 以太网:
    
       连接特定的 DNS 后缀 . . . . . . . : some.com
       本地链接 IPv6 地址. . . . . . . . : fe80::9d9:bb4d:e77a:3a98%15
       IPv4 地址 . . . . . . . . . . . . : 10.11.68.42
       子网掩码  . . . . . . . . . . . . : 255.255.254.0
       默认网关. . . . . . . . . . . . . : 10.11.168.1
    
    

    The script will only give you the 10 prefixed ip address 10.11.68.42.

    The script can be explained to be

    match lines contain IPv4 and substitute IPv4 with blank and print, then capture something prefixed with 10 and print it

提交回复
热议问题