Convert text file into a comma delimited string

  • A+
Category:Languages

I don't seem to locate an SO question that matches this exact problem.

I have a text file that has one text token per line, without any commas, tabs, or quotes. I want to create a comma delimited string based on the file content.

Input:

one two three 

Output:

one,two,three 

I am using this command:

csv_string=$(tr '/n' ',' < file | sed 's/,$//') 

Is there a more efficient way to do this?

 


The usual command to do this is paste

csv_string=$(paste -sd, file.txt) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: