D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

[LUG] grep/sed print matching group only

 

Afternoon all.

How can I make grep or sed print a matching group *only*?
So far the only solution I have been able to find is to use grep to pick out the line and then use sed to substitute the entire line for just the matching group, like this:

grep '^foo .* bar$' | sed 's/foo \(.*\) bar/\1/'

This is clearly stupid and there must be a better way. Why can't I do something like this?

grep '^foo (.*) bar$' -[some option]
or:
sed '/^foo (.*) bar$/\1/'

This isn't the first time I've been driven completely insane by this problem. I don't understand why it's so difficult.

Can anyone help me?

Thanks.
Simon

--
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/listfaq