3

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?

  • 4
    printf != print – Satō Katsura Feb 2 '17 at 10:39
  • @SatoKatsura: print adds a new line that I dont want – Jim Feb 2 '17 at 10:43
  • 3
    printf needs a format string. – Satō Katsura Feb 2 '17 at 10:45
  • 2
    Jim, I've removed the bash tag fro your Q, since the question itself doesn't appear to involve bash-specific behavior, just awk. – Jeff Schaller Feb 2 '17 at 11:00
10

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).

| improve this answer | |
  • This has the same result: awk -F "," '{printf "%s", $0}' mycsv.csv as the OP – Jim Feb 2 '17 at 16:16
  • @Jim, check the part about MS-DOS line delimiters. Try to pipe into sed -n l to check if the file contains CR character or other control characters. – Stéphane Chazelas Feb 2 '17 at 16:39
  • Doing sed -n l mycsv.csv I only see \r$ at the end of each line – Jim Feb 2 '17 at 18:55
  • @Jim, that means your file has CRLF delimiters, you'd need to convert it to unix format (see the part about dos2unix). If you remove the \n, the \r is left. \r when sent to a terminal makes the cursor move to the beginning of the line. – Stéphane Chazelas Feb 2 '17 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.