mstra001 mstra001 - 4 months ago 6
Bash Question

Shell command for inserting a newline every nth element of a huge line of comma separated strings

I have a one line csv containing a lot of elements. Now I want to insert a newline after every n-th element in a bash/shell script.

Bonus: I'd like to prepend a line with descriptors and using the count of descriptors as 'n'.

Example:

"4908041eee3d4bf98e606140b21ebc89.16","7.38974601030349731","45.31298584267982221","94ff11ce7eb54642b0768dde313e8b25.16","7.38845318555831909","45.31425320325949713", (...)


into

"id","lon","lat"
"4908041eee3d4bf98e606140b21ebc89.16","7.38974601030349731","45.31298584267982221"
"94ff11ce7eb54642b0768dde313e8b25.16","7.38845318555831909","45.31425320325949713"
(...)


Edit: I made a first attempt, but the comma delimiters are missing then:

(...) | xargs --delimiter=',' -n3

"4908041eee3d4bf98e606140b21ebc89.16" "7.38974601030349731" "45.31298584267982221"
"94ff11ce7eb54642b0768dde313e8b25.16" "7.38845318555831909" "45.31425320325949713"


trying to replace the " " with ","

(...) | xargs --delimiter=',' -n3 -i echo ${{}//" "/","}
-bash: ${{}//\": bad substitution

Answer

Note: What distinguishes this solution is that it derives the number of output columns from the number of columns in the header line.

Assuming that the fields in your CSV input have no embedded , instances (in which case you'd need a proper CSV parser), try awk:

awk -v RS=, -v header='"id","lon","lat"' '
  BEGIN { 
    print header
    colCount = 1 + gsub(",", ",", header) 
  }
  { 
    ORS = NR % colCount == 0 ? "\n" : ","
    print
  }
' file.csv
Comments