[clue-tech] Re: regex smarties - what am I doing wrong?

David L. Willson DLWillson at TheGeek.NU
Wed Sep 3 12:34:16 MDT 2008


I'm OK with what grep was doing.  I wasn't able to get sed to do what I wanted.  In
short, find an IP address anywhere in a line and output it.  I was trying to use
pattern-storage and recall pattern 1, and it wasn't working.  I can recall pattern 0
(the whole line), but something in my storing and recall of pattern 1 wasn't right.

Your awk example will work, but I think it depends on the IP address appearing first in
the line.  What if the file to search doesn't obey a hosts schema, and the IP appears
after the hostname, rather than before?

On Wed, 03 Sep 2008 12:19:52 -0600, David Rudder wrote
> Grep will print the whole line. You can see the pattern matched, because 
> grep colorizes the match.
> 
> Try using awk instead. I simplified the Regexp, since awk was choking on 
> it. This seems to work:
> grep newwave hosts | awk '/[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\./ 
> {print $1}'
> 
> This will also match some invalid IP addresses, like those with 4 digits 
> numbers. A better awk person than I can probably fix this up.
> 
> -Dave
> 
> clue-tech-request at cluedenver.org wrote:
> > Message: 6
> > Date: Wed, 3 Sep 2008 10:16:56 -0600
> > From: "David L. Willson" <DLWillson at TheGeek.NU>
> > Subject: [clue-tech] regex smarties - what am I doing wrong?
> > To: CLUE tech <clue-tech at cluedenver.org>
> > Message-ID: <20080903161413.M5097 at TheGeek.NU>
> > Content-Type: text/plain;	charset=utf-8
> >
> > I'm trying to capture and return only the IP address from a line of text.  I can match
> > it, but something's wrong with the way I'm trying to capture it.  Any thoughts?
> >
> > dlwillson at aurora:~
> > $ grep newwave hosts | grep -E --color '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
> > 69.88.86.210 newwaveenviro
> >
> > ^ That line does successfully color the IP address.
> >
> > dlwillson at aurora:~
> > $ grep newwave hosts | sed -e 's/(([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3})/\1/p'
> > sed: -e expression #1, char 48: invalid reference \1 on `s' command's RHS
> > dlwillson at aurora:~
> > $ grep newwave hosts | sed -e 's/(([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3})/\0/p'
> > 69.88.86.210 newwaveenviro
> > dlwillson at aurora:~
> > $ grep newwave hosts | sed -e 's/(([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3})/\1/p'
> > sed: -e expression #1, char 48: invalid reference \1 on `s' command's RHS
> > dlwillson at aurora:~
> >
> > Huh...
> >
> > -- David
> >
> >
> >
> > ------------------------------
> >
> > _______________________________________________
> > clue-tech mailing list
> > clue-tech at cluedenver.org
> > http://www.cluedenver.org/mailman/listinfo/clue-tech
> >
> > End of clue-tech Digest, Vol 30, Issue 3
> > ****************************************
> >
> 
> _______________________________________________
> clue-tech mailing list
> clue-tech at cluedenver.org
> http://www.cluedenver.org/mailman/listinfo/clue-tech


-- David



More information about the clue-tech mailing list