user3461823 user3461823 - 1 year ago 103
Linux Question

Combining two lines with bash

I want to merge two lines from csv file:


to get:


Can you help me?

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.


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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download