1748227 Members
4324 Online
108759 Solutions
New Discussion юеВ

awk or sed?

 
SOLVED
Go to solution
David Burgess
Esteemed Contributor

awk or sed?

I've got a file that was created from a database extract. Each field has ()'s round it. I want to strip the brackets out. Simple except some of the fields contain brackets inside them and these need to stay. For example :-

(one) (two) (three (test)) ((test1) five)

Each row has a variable number of fields.

Any ideas?

Regards,

Dave.
8 REPLIES 8
Hein van den Heuvel
Honored Contributor

Re: awk or sed?


Sounds like you can safely remove the leading open and trailing close parens.
Furthermore, you can remove any close-space-open combos.
Suggesting the following solution:

perl -pe 's/^\(//;s/\)$//;s/\) \(/ /g' file

Or if you do not need the space between open/close:

perl -pe 's/^\(|\)$|\) \(//g' x


Hein.
Steven E. Protter
Exalted Contributor

Re: awk or sed?

Hmmm.

sed could be used to prepare the data by strpping out all brackets.

sed s/(/ /g $varname

same thing for the close bracket.

Then a standard awk command

cat $var | awk `{print $1 $2 $3}`

So my answer with some tweaks is probably both.

cut command might be useful in replacing the awk command.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
H.Merijn Brand (procura
Honored Contributor

Re: awk or sed?

What if the parens are to be saved if escaped?

(one \()(two)(\)three) ...

In that case Parse::RecDescent might help

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
David Burgess
Esteemed Contributor

Re: awk or sed?

Hein,

Looks good. Execept I get a trailing ) after the four.

$ perl -pe 's/^\(//;s/\)$//;s/\) \(/ /g' test.txt

one two three (test) (test1) four)

Any ideas?

Regards,

Dave.
Hein van den Heuvel
Honored Contributor
Solution

Re: awk or sed?

Probably a space after the final paren.
Those are hard to 'see' in the itrc forums.
You'd have to append a few lines froma real file to analyze that.

If it is just optional trailing space(s) you can capture those with: \)\s*$

so:

perl -pe 's/^\(|\)\s*$|\) \(//g' x

Hein.

Muthukumar_5
Honored Contributor

Re: awk or sed?

use tr command to do this easily as,

# echo "(one) (two) (three (test)) ((test1) five)" | tr -d '()'
one two three test test1 five


HTH.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: awk or sed?

sorry again.
using sed you can do this as,
# echo "(one) (two) (three (test)) ((test1) five)" | sed "s/(//g;s/)//g"
one two three test test1 five

HTH.
Easy to suggest when don't know about the problem!
David Burgess
Esteemed Contributor

Re: awk or sed?

Thanks guys. The DBA's are happy and the day was saved!

Regards,

Dave.