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

前端 未结 19 1585
被撕碎了的回忆
被撕碎了的回忆 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:21
     grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"
    
    0 讨论(0)
  • 2020-11-28 03:21

    Everyone here is using really long-handed regular expressions but actually understanding the regex of POSIX will allow you to use a small grep command like this for printing IP addresses.

    grep -Eo "(([0-9]{1,3})\.){3}([0-9]{1,3})"
    

    (Side note) This doesn't ignore invalid IPs but it is very simple.

    0 讨论(0)
  • 2020-11-28 03:24

    You could use grep to pull them out.

    grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' file.txt
    
    0 讨论(0)
  • 2020-11-28 03:25

    I have tried all answers but all of them had one or many problems that I list a few of them.

    1. Some detected 123.456.789.111 as valid IP
    2. Some don't detect 127.0.00.1 as valid IP
    3. Some don't detect IP that start with zero like 08.8.8.8

    So here I post a regex that works on all above conditions.

    Note : I have extracted more than 2 millions IP without any problem with following regex.

    (?:(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)\.){3}(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)
    
    0 讨论(0)
  • 2020-11-28 03:26

    for centos6.3

    ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | awk 'BEGIN {FS=":"} {print $2}'

    0 讨论(0)
  • 2020-11-28 03:27

    I usually start with grep, to get the regexp right.

    # [multiple failed attempts here]
    grep    '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'                 file  # good?
    grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file  # good enough
    

    Then I'd try and convert it to sed to filter out the rest of the line. (After reading this thread, you and I aren't going to do that anymore: we're going to use grep -o instead)

    sed -ne 's/.*\([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\).*/\1/p  # FAIL
    

    That's when I usually get annoyed with sed for not using the same regexes as anyone else. So I move to perl.

    $ perl -nle '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/ and print $&'
    

    Perl's good to know in any case. If you've got a teeny bit of CPAN installed, you can even make it more reliable at little cost:

    $ perl -MRegexp::Common=net -nE '/$RE{net}{IPV4}/ and say $&' file(s)
    
    0 讨论(0)
提交回复
热议问题