noober noober - 7 months ago 8
Bash Question

sed - removing last comma from listed value after doing a replace

I'm using

sed
to replace my file of new lines
\n
with
','
which works fine however, in my last item, I don't want the
,
.

How can I remove this?

Example:

sed 's/\n/,/g' myfile.out > myfile.csv


Output:

1,2,3,4,5,6,

Answer

Well you can use labels:

$ cat file
1
2
3
4
5
6
$ sed ':a;N;s/\n/,/;ba' file
1,2,3,4,5,6

You can also use paste command:

$ paste -sd, file
1,2,3,4,5,6
Comments