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

David L. Anselmi anselmi at anselmi.us
Wed Sep 3 18:45:54 MDT 2008


David L. Willson wrote:
> $ echo -e "98.76.54.32 Alice\nBob 123.45.67.89\nCarol or Dave 064.032.016.008\n" \
>> | sed -e 's/\(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}\).*/\1/'
> 98.76.54.32
> Bob 123.45.67.89
> Carol or Dave 064.032.016.008
> 
> As you can see, the Alice line is properly trimmed, but not the other lines.  Now, I
> have two questions:  Why don't the single quotes preserve the sed command properly
> without all the escaping of the special chars?  And, why doesn't the second close-paren
> close the pattern-capture?  Regexes are hard...

You only want \(...\) where you're doing a capture.  If you're doing 
grouping just use (...).  The single quotes do preserve the command for 
sed.  You aren't using \( because the shell interprets ( but rather 
because \( and ( are different parts of a regex.  But there are lots of 
variations.

I think perl's regexes are defined the most clearly so I'd use that. 
Otherwise I typically use awk vs grep | sed.

I think your stated requirements already justify perl, and you'll wind 
up switching to it if you add anything to them.

Dave



More information about the clue-tech mailing list