LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   awk script forgets tabs (https://www.linuxquestions.org/questions/linux-software-2/awk-script-forgets-tabs-250576/)

iluvatar 11-03-2004 07:18 AM

awk script forgets tabs
 
I have the following awk command:

awk -F"\t" '$1=="ART" && $2=="49011000" {$2="49111090";$6="00"}; {print $0}' test

the file is tab seperated, when the 1st field is "ART" and the 2nd field is "49011000" then the 2nd and the 6th field are changed. works great, except that the output lines wich are changed don't have the tabs anymore, those are replaced with spaces... the unaffected lines come through with all tabs correct. anyone knows why and how to solve this?

thanx in advance,
.-=~ iluvatar ~=-.

druuna 11-03-2004 11:24 AM

Hi,

The moment you start 'editing' the content of the fields, the standard output field seperator (OFS) is used, which is a space. That explains why unchanged lines have tabs and changed lines don't.

Best way to tackle this: Set the OFS value to a tab:

awk -F"\t" 'BEGIN { OFS="\t" } $1=="ART" && $2=="49011000" {$2="49111090";$6="00"}; {print $0}' test

Hope this helps.

iluvatar 11-04-2004 01:56 AM

ah that explains a lot :D thanks!

greetz,
.-=~ iluvatar ~=-.


All times are GMT -5. The time now is 06:54 PM.