Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fixed with string manipulation with sed or awk ?

Status
Not open for further replies.

Exie

Programmer
Sep 3, 2003
156
AU
Hi,

I've got a fixed with data file, and I need to alter 1 field before it gets processed.

The last field has ( and ) and _ in it, and we need to take them out.

I started with

Code:
echo "word1word2wo(r)d3_  " | tr -d '()_'

This has 2 problems (apart from been somewhat risky if it affects other fields). Firstly this altered my record length and secondly I actually want to change to size of the last field from 10 to 8

how can I get the output to be:
"word1word2word3 "

I've tried SED and AWK, but feel like my head is going to explode.
 
Something like this perhaps, although it poses the same risk to the other fields:

Code:
echo "word1word2wo(r)d3_  " | awk '{gsub("[()_]","");printf "%-18s\n",$0}'

To avoid that risk you might need to separate that field from the rest first before performing the replacement, but the printf() usage should be useful for you.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top