Help with changing text file layout

Hi there,
I am with this one column input text file to change layout, please help. Thanks. I have awk, sed.

$ cat input

Median
1.0
2.3
3.0

Median
35.0
26.3
45.7
10.1
63.1

Median
1.2
2.3
3.4
4.5

And the desire output should be

Median 1.0 2.3 3.0
Median 35.0 26.3 45.7 10.1 63.1
Median 1.2 2.3 3.4 4.5

try:

awk ' /^ *$/ {print} !/^ *$/ {printf $0 " "} END {print ""}' input
1 Like
awk '/^$/ {print }
       /^[M0-9]/ {printf ("%s ", $0) }
       END {print ""} ' inputfile > newfile
       
1 Like

It's an awk buffet:

awk '/Median/{if(NR!=1)print " ";}{printf("%s ", $0)}END{print " ";}' file
1 Like

more for the smorgasbord:

awk '{printf ($0 ~ /^ *$/) ? "\n" : $0 " " } END {print ""}' input

or, look Ma! no awk!

while read line
do
  printf "$line "
  [ -z "$line" ] && echo ""
done < input
echo ""
1 Like

Thank you so much. They all work! Really appreciate all the helps!

I really need to learn more about regular expressions...

Or

awk 'NR>1{gsub("\n"," ",$0);print "Median "$0}' RS="Median" file
1 Like

And here you see the beauty of awk :slight_smile:

 awk '$1=$1' RS= infile

Note the space after RS= (same as RS="" )

1 Like

it is beautiful, thank you!