Home > Software design >  Bash: printing out numbers containing commas to csv file
Bash: printing out numbers containing commas to csv file

Time:09-16

I am trying to print out numbers with commas to a csv file using bash.

The command:

printf "1,000" > the.csv

prints out two cells, one with 1, and one with 000. I would like to know how I tell the csv file to ignore the comma so that I can only have one cell containing 1,000.

Side question: am I able to resize the columns as I print to them? i.e. I print a word that is too long for the cell to fully hold, can I make it just big enough as I print the word?

CodePudding user response:

CSV requires commas to be in quoted strings. Because the quotes are parsed by the shell, you are not writing literal quotes with your command. If you want to write literal double quotes, the syntax for that would be

printf \"1,000\" > the.csv

or

printf '"1,000"' > the.csv

(The lack of a final newline and the use of a literal string as the first argument to printf are problematic for other reasons. You really want

printf '"%s"\n' "1,000" > the.csv

which also makes it easy to write multiple lines if you want to.)

  • Related