LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-02-2009, 01:09 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
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
 
Old 11-02-2009, 01:38 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Edit: wrong

Last edited by AlucardZero; 11-02-2009 at 03:07 PM.
 
Old 11-02-2009, 01:38 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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}

Last edited by pixellany; 11-02-2009 at 02:35 PM. Reason: Added quote to clarify what I was responding to
 
Old 11-02-2009, 01:56 PM   #4
BBPS
LQ Newbie
 
Registered: Aug 2003
Posts: 18

Rep: Reputation: 0
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.
 
Old 11-02-2009, 02:40 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by AlucardZero View Post
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....
 
Old 11-02-2009, 03:07 PM   #6
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
I'm dumb, forgot my {1-3}s..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract everything before a regex match onesikgypo Programming 5 10-21-2009 04:49 AM
A single regex to match anything with ".aac" or ".mp3" at the end ? lumix Linux - General 9 05-09-2008 01:11 AM
Perl Regex multiline match issues adymcc Linux - General 2 03-31-2008 09:45 AM
how to look for the shortest match using regex, bascially the opposite of .* new_2_unix Linux - Newbie 8 01-08-2008 09:21 AM
help me match this regex line (easy) JustinHoMi Programming 7 03-17-2002 01:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration