How to select and print IP address from a set of strings?

16,046

Solution 1

It's possible, but not elegant:

echo 'This sentence contains an ip number 1.2.3.4 and port number 50, i want to print the IP address only.' \
| sed 's/.*\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/'

[0-9] matches any digit, \{1,3\} means it can be repeated 1 to 3 times. \. matches a dot. The whole IP is captured by the \(...\) parentheses, what comes before and after is matched by .*, i.e. anything repeated zero or more times. The whole matching string (i.e. the whole line) is then replaced by the contents of the first matching group.

You can make it more readable by introducing a variable:

n='[0-9]\{1,3\}'
... | sed "s/.*\($n\.$n\.$n\.$n\).*/\1/"

It prints the whole string if the IP is not found. It also doesn't check for invalid IPs like 256.512.999.666.

Solution 2

This is a grep solution:

echo "$sentence" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'
  • -o print only the matching part
  • -E switches to extended regex
  • the pattern matches every digit ([0-9]) one or more times (+) then a dot (\.) and again digits...

Here another solution with perl:

echo "$sentence" | perl -l -ne '/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ && print $&'
  • -l specified the line terminator (newline)
  • -n loops trough the input given by echo (could be multiple lines)
  • -e code follows
  • the regex inside the perl code is very much the same as in the grep solution above

Solution 3

Use this command of grep:

grep -Eo '[0-9.]+ ' file

Or even better:

grep -oP '\d+\.\d+\.\d+\.\d+' file

or

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

Solution 4

I use grep:

echo 'This sentence contains an ip number 1.2.3.4 and port number 50, i want to print the IP address only.' | grep -oE '((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.){3}((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]))'

This will print only valid ip adresses, unlike the other answers

Share:
16,046

Related videos on Youtube

Anandu M Das
Author by

Anandu M Das

Security Tester

Updated on September 18, 2022

Comments

  • Anandu M Das
    Anandu M Das over 1 year

    I have a sentence whic contains an IP address. For example,

    This sentence contains an ip number 1.2.3.4 and port number 50, i want to print the IP address only.
    

    From the above sentence, I want to print the IP address only. How can I do this? I heard that it is possible to do this with sed

  • Anandu M Das
    Anandu M Das over 9 years
    Could you please explain me how this works. It might help me more to learn sed.
  • choroba
    choroba over 9 years
    @AnanduMDas: Updated.
  • Anandu M Das
    Anandu M Das over 9 years
    One more help can you do? If I have a new line after this line, then that line is also getting printed. Can I force sed to check for the first line only?
  • choroba
    choroba over 9 years
    For multiline processing, I'd turn to Perl.
  • skeggse
    skeggse over 9 years
    @chaos wouldn't grep match any character for the ., given that you included the -E option?
  • chaos
    chaos over 9 years
    @skeggse Ur right, I changed it, thanks for your note
  • chaos
    chaos over 9 years
    @AvinashRaj It a question of perfection. [0-9]{1,3} would match 987.567.432.999 too and that's not a valid ip address.
  • chaos
    chaos over 9 years
    @skeggse "definitely not valid" and "not valid" is the same. Things can't be more invalid than other things. =)
  • skeggse
    skeggse over 9 years
    @chaos very true :)
  • chaos
    chaos over 9 years
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 9 years
    Sure, things can be more invalid than other things. A lot of product version numbers (at least in the Windows world) look like 38.0.2125.111, and there's value in having a regex that's smart enough not to confuse that with an IP address.