Turning multiple lines into one comma separated line [duplicate] Turning multiple lines into one comma separated line [duplicate] unix unix

Turning multiple lines into one comma separated line [duplicate]


Using paste command:

paste -d, -s file


file

aaabbbcccddd

xargs

cat file | xargs

result

aaa bbb ccc ddd 

xargs improoved

cat file | xargs | sed -e 's/ /,/g'

result

aaa,bbb,ccc,ddd 


There are many ways it can be achieved. The tool you use mostly depends on your own preference or experience.

Using tr command:

tr '\n' ',' < somefile

Using awk:

awk -F'\n' '{if(NR == 1) {printf $0} else {printf ","$0}}' somefile