[ale] Riddle me this awk man

Geoffrey Myers lists at serioustechnology.com
Thu Feb 17 20:10:45 EST 2011


Greg Freemyer wrote:
> All,
> 
> I'm trying to re-format the first field of a CSV file using AWK.
> 
> This is what I'm trying which is very close to working:
> 
> cat supertimeline.csv | awk -F" " '(NR == 1) {print};(NR > 1) { printf
> "%s-%s-%s %s", $3,$2,$4,$5; $1=""; $2="";$3="";$4=""; $5=""; print
> substr($0, 5) }' > mysupertimeline.csv

You need to add a newline to your printf statements.

> 
> But:
> 
> # wc supertimeline.csv mysupertimeline.csv
>    541986   4392932  82668394 supertimeline.csv
>    174130    702866  23434337 mysupertimeline.csv
> 
> I end up with only about a third as many lines.  How can that be?  The
> missing lines seem to be spread around, so it's not like it did the
> first 174,130 lines and died.
> 
> I simplified it to:
> 
> cat supertimeline.csv | awk -F" " ' { printf "%s-%s-%s %s",
> $3,$2,$4,$5; $1=""; $2="";$3="";$4=""; $5=""; print substr($0, 5) }' >
> mysupertimeline.csv
> 
> and I get the same result.  (I have lots of free diskspace, so don't
> blame that.)
> Boo Hiss!
> 
> FYI: this is ubuntu.  I'll try it on cygwin in a second just because I
> have the csv file there already.
> 
> 


-- 
Until later, Geoffrey

"I predict future happiness for America if they can prevent
the government from wasting the labors of the people under
the pretense of taking care of them."
- Thomas Jefferson


More information about the Ale mailing list