[NTLUG:Discuss] awk question I think

kbrannen@gte.net kbrannen at gte.net
Thu Aug 8 21:46:37 CDT 2002


Bobby Wrenn wrote:
> OK I'm getting to the point of being dangerous with sed. Now I have a comma 
> delimited file where I need to trim some characters out of a field.
> 
> What I would like to do is take the last 4 characters of the fifth field and 
> place them in a new field. The new field can be at the beggining or end of 
> the record.
> 
> Is this a problem for awk? If so, any pointers?
> 
> TIA
> Bobby

Can we talk you into using Perl?  It's like sed on mega-steroids. :-)

prompt>  perl -ne '@f=split(/,/); $f[4]=~s/(.{4})$//; print join(",", at f,$1)'

so the input of:
asdf,asdf,asdf,asdf,asdf1234,qwer

produces:
1234,asdf,asdf,asdf,asdf,asdf,qwer

Putting the new field on the back is easily doable, but takes a tad more code 
(11 or 12 chars). :-)

In case it's not obvious, you'll want to use file redirection for input (" < 
input_file") and for output (" > output_file") at the end of that command.

HTH,
Kevin





More information about the Discuss mailing list