One-liner to convert a file to a comma-delimited list (Bash)

paste -d, -s filename | sed 's/,/","/g' | sed 's/^/"/' | sed 's/$/"/'

Example:

file1.txt contains:
alpha
bravo
charlie
delta
echo

paste -d, -s file1.txt | sed 's/,/","/g' | sed 's/^/"/' | sed 's/$/"/' > file2.txt

file2.txt contains:
"alpha","bravo","charlie","delta","echo"

2 thoughts on “One-liner to convert a file to a comma-delimited list (Bash)”

  1. Cool! I found the challenge interesting so I came up with one too.

    sed -e ‘:a;N;$!ba;s/\n/”,”/g;s/^/”/;s/$/”/’ file2.txt

Leave a Reply

Your email address will not be published. Required fields are marked *