How do you extract IP addresses from files using a regex in a linux shell?

前端 未结 19 1698
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 02:43

How to extract a text part by regexp in linux shell? Lets say, I have a file where in every line is an IP address, but on a different position. What is the simplest way to e

19条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 03:27

    You can use some shell helper I made: https://github.com/philpraxis/ipextract

    included them here for convenience:

    #!/bin/sh
    ipextract () 
    { 
    egrep --only-matching -E  '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' 
    }
    
    ipextractnet ()
    { 
    egrep --only-matching -E  '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/[[:digit:]]+' 
    }
    
    ipextracttcp ()
    { 
    egrep --only-matching -E  '[[:digit:]]+/tcp' 
    }
    
    ipextractudp ()
    { 
    egrep --only-matching -E  '[[:digit:]]+/udp' 
    }
    
    ipextractsctp ()
    { 
    egrep --only-matching -E  '[[:digit:]]+/sctp' 
    }
    
    ipextractfqdn ()
    { 
    egrep --only-matching -E  '[a-zA-Z0-9]+[a-zA-Z0-9\-\.]*\.[a-zA-Z]{2,}' 
    }
    

    Load it / source it (when stored in ipextract file) from shell:

    $ . ipextract

    Use them:

    $ ipextract < /etc/hosts
    127.0.0.1
    255.255.255.255
    $
    

    For some example of real use:

    ipextractfqdn < /var/log/snort/alert | sort -u
    dmesg | ipextractudp
    

提交回复
热议问题