There is a text file, with aboot 10000+ lines, in the format:

field1 field2 field3 field4 field5 ... fieldX

It needs to be converted to look like:

field1,field2,field4,field3,field5 to fieldX

The last field must contain everything from field5 until the end of the line. And the final output must be exported to a csv file.

Therefore, for:

123 abc XYZ qwe Abcd Efgh Iklmno Pqr Stu

The output should look like:

123,abc,qwe,XYZ,Abcd Efgh Iklmno Pqr Stu

This is what I am using to try and achieve the desired output:

awk '{printf $1 "," $2 "," $4 "," $3 ",";{for(i=5; i<NF; i++) printf "%s",$i OFS; if(NF) printf "%s",$NF; printf ORS}}' file.txt > file1.txt

(echo "Heading1,H2,H3,H4,H5" ; cat file1.txt) > file.csv

The desired output is generating a new line at the end of every entry, I am not entirely sure why. I am very new to awk and Unix, in general. Any form of help is appreciated.

  • Works fine for me. Can you provide more details. – John1024 Nov 25 '15 at 3:55
  • There is another step that send this file via mail: mutt -a "/path/to/file1.csv" -- user@domain.com The resultant csv file has a blank line at the end of every entry. So, there is a row with data, then a blank row, another row of data followed by a blank row.. and so on. – Cartwig Nov 25 '15 at 4:20
  • So, if you run cat /path/to/file1.csv, do you see those blank lines? Or, do you just see them in the email? – John1024 Nov 25 '15 at 4:28
  • No, there are no blank lines seen when cat file1.csv is run. The email is sent to a recipient using a Windows machine. When the file is opened on Microsoft Excel, the blanks are seen. – Cartwig Nov 25 '15 at 4:31
  • John, I found this article which helped me fix my issue: link. @danben says The ORS (output record separator) variable in AWK defaults to "\n" and is printed after every line. You can change it to " ". Thanks to the both of you! – Cartwig Nov 25 '15 at 7:39

Found the solution here awk without printing newline.

The ORS (output record separator) variable in AWK defaults to "\n" and is printed after every line.

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.