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.
mutt -a "/path/to/file1.csv" -- [email protected]
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.cat /path/to/file1.csv
, do you see those blank lines? Or, do you just see them in the email?