Unexpected output from awk printf

awkcsvosx

I have the following csv file:

$ cat mycsv.csv 
"1/30/2017 11:14:55 AM","I","M","k6.0.1","E","jim","JimK","JIM","jim@gmail.com","A","6.0.12”,”A”,”N”  
"1/30/2017 11:14:55 AM","I","M","k6.0.1","E","jim","JimK","JIM","jim@gmail.com","A","6.0.12”,”A”,”N”  
"1/30/2017 11:14:55 AM","I","M","k6.0.1","E","jim","JimK","JIM","jim@gmail.com","A","6.0.12”,”A”,”N”  

Why does the following not print all the lines?

$awk -F "," '{printf}' mycsv.csv    
 ","M","k6.0.1","E","jim","JimK","JIM","jim@gmail.com","A","6.0.12”,”A”,”N””  

$awk -F "," '{printf $0}' mycsv.csv 
 ","M","k6.0.1","E","jim","JimK","JIM","jim@gmail.com","A","6.0.12”,”A”,”N””  

Both should print all the lines right? What am I doing wrong?

Best Answer

The first argument to printf, whether it's C printf() or the printf utility or awk's printf() is required1 and is the format.

You want:

awk '{printf "%s", $0}'

here. If you don't want an output record separator, you can also do:

awk -v ORS= '{print}' < mycsv.csv

Or even:

awk -v ORS= 1 < mycsv.csv

({print} is the default action, true is the default condition, but you need to specify at least one action or condition, 1 is one way to say true).

Though here, tr would be enough:

tr -d '\n' < mycsv.csv

Or if you still want one trailing newline character so that output is still text:

paste -sd '\0' mycsv.csv

It also seems like your file has Microsoft-style CRLF line delimiters, so you may want to also delete the CR characters:

tr -d '\r\n' < mycsv.csv

Or only the CRLF sequences with awk implementations that support more than single-character RS (which includes gawk and mawk but not macOS awk):

awk -v RS='\r\n' -v ORS= 1 < mycsv.csv

Or:

awk -v RS='\r?\n' -n ORS= 1 < mycsv.csv

that is with the \r optional to handle either Unix or MS-DOS line delimiters.

Or use things like dos2unix or d2u to convert the file to Unix format first.

Notes

1 the format argument to printf is required in the standard specification of the awk utility. In gawk and mawk omitting it results in an error. In busybox awk, it's equivalent to printf "" and in awk derived from the original implementation (like on macOS), it's equivalent to printf $0 (of little usefulness as it's still considered as a format, you'll still get an error if $0 contains % characters).

Related Question