将每个x(dynamic)数量的行移动到一行

所以我有这样的数据

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

所以我不能做特定数量的行,所以认为是使用/作为分隔符,并把每一行后,直到/在同一行(逗号分隔?)理想的预期输出:

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

首选shell / bash / ksh,但是excel解决scheme也可以。

这是一个awk解决scheme:

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

请注意,它假定input文件以类似于/blah的行开头,但不以一个结尾。

塞进一个(不太可读的)单行:

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

一个sed解决scheme

 sed -r ':a;N;$!ba;s/\n\s+/,/g' input | sed 's/$/,,/' 

你得到,

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