Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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","[email protected]","A","6.0.12”,”A”,”N”  
"1/30/2017 11:14:55 AM","I","M","k6.0.1","E","jim","JimK","JIM","[email protected]","A","6.0.12”,”A”,”N”  
"1/30/2017 11:14:55 AM","I","M","k6.0.1","E","jim","JimK","JIM","[email protected]","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","[email protected]","A","6.0.12”,”A”,”N””  

$awk -F "," '{printf $0}' mycsv.csv 
 ","M","k6.0.1","E","jim","JimK","JIM","[email protected]","A","6.0.12”,”A”,”N””  

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

share|improve this question
4  
printf != print – Sato Katsura 13 hours ago
    
@SatoKatsura: print adds a new line that I dont want – Jim 13 hours ago
3  
printf needs a format string. – Sato Katsura 13 hours ago
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 13 hours ago
up vote 10 down vote accepted

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

share|improve this answer
    
This has the same result: awk -F "," '{printf "%s", $0}' mycsv.csv as the OP – Jim 8 hours ago
    
@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 7 hours ago
    
Doing sed -n l mycsv.csv I only see \r$ at the end of each line – Jim 5 hours ago
    
@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 4 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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