user3461823 user3461823 - 6 months ago 14
Linux Question

Combining two lines with bash

I want to merge two lines from csv file:

x,,x
,x,


to get:

x,x,x


Can you help me?

Edit:
I have many combination of x occurence (x,, or ,x,x ...)

I give you other example. First column of file contains linux distribution name ,second column is command, if in third column exists "x" it means that command is available in centos 5, if in fourth column exists "x" it means that command is in centos 6, and if "x" is in the last field it means that is available in centos 7.

centos,ls,,,x
centos,ls,x,x,


And now I want to have one record which will summarize info about command.

Answer

Try this :

sed ':a;$!N;s/,$//;s/^,//;s/\n//g;ta;s/,,*/,/g' file

This was tested on your last edit sample code :

sed 's/^/,&/;s/$/,&/g;:a;$!N;s/\n/,/g;ta;s/,$//;s/^,//;s/,,*/,/g' file