Coder's Guild Mailing List

Pattern matching with unix/linux

Posted by Benjamin Johnston on 2000-02-27

Hi,

I'm having trouble finding a simple solution to this problem,

Say I've got a file which contains
a ( b      c d e f ) g
a c d e ) g
a d
a ( b )
a ( c
d e )
a ( b ) c ( d ) e

And I want to search through it for a particular pattern, and then display
the text that matches the pattern. (I want to return anything between a "(",
and a ")" -- and its okay, if there is a ")" without a preceding "(").

ie. I want to get the output
( b      c d e f )
( b )
( c
d e )
( b )
( d )

I've searched the manpages of sed awk and grep, but the best I've come up
with is:
cat filename | sed "s/\([^\)]*[\)]//g"

which produces:
a  g
a c d e ) g
a d
a
a ( c
d e )
a  c  e

But that isn't what I had in mind, because
1. it displays the text that doesn't match the pattern (instead of the text
that does), and
2. it doesn't ignore newline characters when doing the match

I'm going to write a java program to do this (and some other processing of
the file), so its not that important anymore, but I'm still interested in
finding a solution.

Anyone? (I'd prefer it to be something that could be written as one command
line (in bash)).

BTW, If you know a simple way, an even better solution would be one that
took out excess white space and newline characters that are inside the
matched pattern, ie:
( b c d e f )
( b )
( c d e )
( b )
( d )

-Benjamin Johnston
s355171@xxxxxxx.xx.xxx.xx


To signoff send a mail to listserver@xxxx.xx.xx with 
  "signoff tcg" in the body of your message.