[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash, sed: extracting regex subexpressions



On Tue, May 27, 2008 at 02:49:59PM +0000, John O'Hagan wrote:

> in other words, treating backreferences as pseudo-variables, but it
> doesn't, AFAIK.

Use the right tool for the job. If you want to treat grouped matches as
variables, use perl because perl explicitly supports this:

    $ echo foobarbaz | \
	perl -ne '$_ =~ /(foo)(bar)(baz)/; print "$3, $2, $1\n"'  
    baz, bar, foo

If you don't want to use perl, you'll have to use multiple invocations
of grep's -o flag and build up your own capture variables. Good luck!

-- 
"Oh, look: rocks!"
	-- Doctor Who, "Destiny of the Daleks"


Reply to: