Mysta Mysta - 6 days ago 5
Bash Question

Move every x(dynamic) number of lines to a single line [Shell]

So I have data that looks like this

/blah
etc1: etc
etc2
etc3: etc
etc4
/blah
etc1: etc
etc2
etc3
/blah
etc1: etc
etc2
etc3: etc
etc4
/blah
etc1
etc2


So I can't do a specific number of lines, so thought was to use / as a delimiter and put every line after until / on same line(comma delimited?)
Ideal Expected Output:

/blah,etc1: etc,etc2,etc3: etc,etc4,,
/blah,etc1,etc2,etc3,,
/blah,etc1: etc,etc2,etc3: etc,etc4,,
/blah,etc1,etc2,,


Prefer shell/bash/ksh but an excel solution would work too.

Answer

Here's an awk solution:

awk ' 
  /^\// { if (NR > 1) print ","; printf "%s,", $0; next } 
  { gsub(/^ +| +$/, ""); printf "%s,", $0 } 
  END { print "," }
' file

Note that it assumes that the input file starts with a /blah-like line, but doesn't end with one.

Crammed into a (less readable) one-liner:

awk '/^\// {if(NR>1) print","; printf"%s,",$0; next} {gsub(/^ +| +$/, ""); printf"%s,",$0} END {print","}' file