LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to keep Field Separator in AWK when using a sub statement (http://www.linuxquestions.org/questions/programming-9/how-to-keep-field-separator-in-awk-when-using-a-sub-statement-703389/)

tmcguinness 02-09-2009 12:16 PM

how to keep Field Separator in AWK when using a sub statement
 
Ok so here is my little one liner:

cat somefile.txt | awk -F\| '{ sub(//,"999",$3); print }'

The delimiter is a | and the results end up without anything but spaces which ruins my output.

I know there is a fairly easy way to avoid this - anyone?

jan61 02-09-2009 02:09 PM

Moin,

Code:

awk -F\| 'BEGIN {OFS="|"}{ sub(//,"999",$3); print }' somefile.txt
Jan

P.S.: Please - no useless use of cat award parties here ;-)

tmcguinness 02-09-2009 02:16 PM

Gruetzi!

Ah - yes - thanks I thought it was something similar to setting OFS!

Cheers,
todd

tmcguinness 02-09-2009 02:19 PM

Oh and the reason I was using a cat was due to the data actually being piped into the statement. It was really for simplicity.

jan61 02-09-2009 02:24 PM

Moin,

Quote:

Originally Posted by tmcguinness (Post 3437509)
Oh and the reason I was using a cat was due to the data actually being piped into the statement. It was really for simplicity.

oh - in this case I can live with it and won't receive any nightmares tonight ;-)

Jan


All times are GMT -5. The time now is 08:10 PM.