How to select and print IP address from a set of strings?
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 byecho
(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
Related videos on Youtube
Comments
-
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 over 9 yearsCould you please explain me how this works. It might help me more to learn sed.
-
choroba over 9 years@AnanduMDas: Updated.
-
Anandu M Das over 9 yearsOne 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 over 9 yearsFor multiline processing, I'd turn to Perl.
-
skeggse over 9 years@chaos wouldn't grep match any character for the
.
, given that you included the-E
option? -
chaos over 9 years@skeggse Ur right, I changed it, thanks for your note
-
chaos over 9 years@AvinashRaj It a question of perfection.
[0-9]{1,3}
would match987.567.432.999
too and that's not a valid ip address. -
chaos over 9 years@skeggse "definitely not valid" and "not valid" is the same. Things can't be more invalid than other things. =)
-
skeggse over 9 years@chaos very true :)
-
chaos over 9 years@skeggse see youtube.com/watch?v=F_1zoX5Ax9U
-
G-Man Says 'Reinstate Monica' over 9 yearsSure, 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.