linux - Append row conditionally as column bash script -


i have been attempting write bash script formats output of command. output puts multiple columns single list of records:

host="host1" disk agent="a.06.20" general media agent="a.06.20" host="host2" disk agent="a.06.20" general media agent="a.06.20" host="host3" disk agent="a.06.20" host="host4" disk agent="a.06.20" general media agent="a.06.20" 

i have script format such:

host="host1",disk agent="a.06.20",general media agent="a.06.20" host="host2",disk agent="a.06.20",general media agent="a.06.20" host="host3",disk agent="a.06.20", host="host4",disk agent="a.06.20",general media agent="a.06.20" 

as can see, not every host has 3 values, can't iterate list.

there hundreds of hosts in output , it's frustrating command doesn't have options creating table or report.

the output has bunch of other garbage in i've been able sed out, i'm new sed , awk it's giving me headache.

thanks!

here sed script:

sed '/host/{:loop; n; /\nhost/!s/\n/,/; t loop; p; d}' foo.txt 

it works matching host, appending next line. if next line not starting host, substitutes \n comma. loop terminates when reach next "host" line. p command prints portion of multiline pattern space before \n, , d deletes portion , transfers control top of script, next "host" line becomes current line , script starts again.

which outputs:

host="host1",disk agent="a.06.20",general media agent="a.06.20" host="host2",disk agent="a.06.20",general media agent="a.06.20" host="host3",disk agent="a.06.20" host="host4",disk agent="a.06.20",general media agent="a.06.20" 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -