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

Jab jborer at gmail.com
Wed Sep 3 12:14:31 MDT 2008


On Wed, Sep 3, 2008 at 10:16 AM, David L. Willson <DLWillson at thegeek.nu>wrote:

> 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...
>
>
I don't know why sed wouldn't work but perl should.

Try something like

perl -lne 'print if s/((\d{1,3}\.){3}\d{1,3}).*\ newwaveenviro\ *.*/\1/'
/etc/hosts

-jacob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cluedenver.org/pipermail/clue-tech/attachments/20080903/e1aed961/attachment.html


More information about the clue-tech mailing list