LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Help with regex to match an IP (https://www.linuxquestions.org/questions/linux-server-73/help-with-regex-to-match-an-ip-766286/)

abefroman 11-02-2009 01:09 PM

Help with regex to match an IP
 
I have output in this format:
(?@94.21.54.x)

I want list just the IP's, what is the command to do that?

I tired this:
cat file.txt | sed '/^[0-9].*$/!d'

But it didn't work.

TIA

AlucardZero 11-02-2009 01:38 PM

Edit: wrong

pixellany 11-02-2009 01:38 PM

Quote:

I tired this:
cat file.txt | sed '/^[0-9].*$/!d'
That says:
Match, as the only thing on the line, a single digit, followed by any number of characters.

What book or reference have you been using for BASH and scripting?

This will match an IP, but it is not perfect:
Code:

([0-9][0-9]?[0-9]?\.){3}[0-9][0-9]?[0-9]?
This is "extended regex" syntax. If used--eg--in SED, you would need the -r flag.

This works also (slightly shorter):
Code:

([0-9]([0-9]?){2}\.){3}[0-9]([0-9]?){2}

BBPS 11-02-2009 01:56 PM

If you want to remove all the rubbish from around the IP Address, like the "(?@" and the ")", then either of the following should do what you want:

cat file.txt | awk '{ gsub("^.*@|)$","",$0); print $0 }'
cat file.txt | sed -r 's/^.*@|\)$//g'

Hope this helps.

Thanks,


Paul.

pixellany 11-02-2009 02:40 PM

Quote:

Originally Posted by AlucardZero (Post 3741570)
grep "[0-9]\.[0-9]\.[0-9]\.[0-9]" file
or
grep -P "([0-9].?){4}" file

Nope----the first one will not match:
23.4.56.7
124.6.7.98

etc.

the last one says: match exactly 4 occurences of (a single digit followed by an optional character)
It will match:
1a3b4c7h---and a zillion other combos....

AlucardZero 11-02-2009 03:07 PM

I'm dumb, forgot my {1-3}s..


All times are GMT -5. The time now is 01:26 AM.