Awk, setting FS = "," doesn't seem to work, are there any reservations I should be aware of?

Ok, so I'm trying to write a simple awk to clear some commas of some csv files that I have.

Here are a few lines of sample data

  PRD,,,,PEWPRV100D,,,EWPRVU457D,,,,12/31/2011  10:09:14 PM,,,,,5,,,4,,
  PRD,,,,PEWPRV100D,,,EWPRVU250D,,,,12/31/2011  10:09:23 PM,,,,,67,,,69,,
  PRD,,,,PEWREF100D,,,EWREFU045D,,,,12/31/2011  10:09:40 PM,,,,,7,,,5,,
  PRD,,,,PEWPRV100D,,,EWPRVU191D,,,,12/31/2011  10:09:40 PM,,,,,6,,,5,,

As a simple first step, I want to accomplish this (what I really want to do is harder, but this is the first thing I need to do, and I can’t even get this right :()

   PRD,PEWPRV100D,EWPRVU457D,12/31/2011  10:09:14 PM,5,4,
   PRD,PEWPRV100D,EWPRVU250D,12/31/2011  10:09:23 PM,67,69,
   PRD,PEWREF100D,EWREFU045D,12/31/2011  10:09:40 PM,7,5,
   PRD,PEWPRV100D,EWPRVU191D,12/31/2011  10:09:40 PM,6,5,

Here is my first attempt at awk script

  #!/bin/awk 
  BEGIN{FS=",";} 
  {print $0,$4,$7,$11,$16,$19 }
  END{print "DONE"}

which produces

  PRD,,,,PEWPRV100D,,,EWPRVU457D,,,,12/31/2011  10:09:14 PM,,,,,5,,,4,,,,,,,
  PRD,,,,PEWPRV100D,,,EWPRVU250D,,,,12/31/2011  10:09:23 PM,,,,,67,,,69,,,,,,,
  PRD,,,,PEWREF100D,,,EWREFU045D,,,,12/31/2011  10:09:40 PM,,,,,7,,,5,,,,,,,
  PRD,,,,PEWPRV100D,,,EWPRVU191D,,,,12/31/2011  10:09:40 PM,,,,,6,,,5,,,,,,,

More conversational script I tried:

  #!/bin/awk 
  BEGIN{FS=",";} 
  {printf("$$%s$$", $0) }
  END{print "DONE"} 

which produces

 $$PRD,,,,PEWPRV100D,,,EWPRVU457D,,,,12/31/2011  10:09:14 PM,,,,,5,,,4,,$$
  $$PRD,,,,PEWPRV100D,,,EWPRVU250D,,,,12/31/2011  10:09:23 PM,,,,,67,,,69,,$$
  $$PRD,,,,PEWREF100D,,,EWREFU045D,,,,12/31/2011  10:09:40 PM,,,,,7,,,5,,$$
  $$PRD,,,,PEWPRV100D,,,EWPRVU191D,,,,12/31/2011  10:09:40 PM,,,,,6,,,5,,$$

( ), FS = "," , . , , , . man awk , FS - var, . hte -F, .

- , ?

+3
1

awk, $0 - . $1 - , $2 .. , , , :

{print $0,$4,$7,$11,$16,$19 }

:

{print $1,$5,$8,$12,$17,$20 }
+3

All Articles