bash case statement output help

greetings,

I have a script that is taking input like this:

a
b
c
d
aa
bb
aaa
bbb
ccc
ddd

and formating it to be like this:
a b c d
aa bb
aaa bbb ccc ddd

however, the only way i can get this to work and not have the last line clipped off is putting the echo in the *) of the case statement which actually gives me output like this:

a
a b
a b c
a b c d
aa
aa bb
aaa
aaa bbb
aaa bbb ccc
aaa bbb ccc ddd

is there a) any way to not have to put the echo in the *) of the case statement and still get the 3 lines i need? or b) is there anything that i can use to just print out the longest line that starts with a/aa/aaa (please keep in mind i would like to be able to set the delimiter to be :'s)

If this is too confusing, let me know and i will post some sanitized output and code.

thanks!

It seems to me you should put an echo statement after your while loop, not in the case statement. Otherwise please post what you have tried so far, otherwise it remains a bit of a stab in the dark.

Gives weird cascading output
file output.txt:

a
b
c
d
aa
bb
aaa
bbb
ccc
ddd

attempts:
weird cascading output:

cat output.txt|while read LINE; do case "$LINE" in a*) unset FOO; FOO[0]="$LINE";; *) FOO[$[${#FOO[@]}+1]]="$LINE";; esac; echo ${FOO[*]}; done

and

cat output.txt|while read LINE; do case "$LINE" in a*) unset FOO;  FOO[0]="$LINE";; *) FOO[$[${#FOO[@]}+1]]="$LINE"; echo ${FOO[*]};; esac;  done

cuts off last line:

cat output.txt|while read LINE; do case "$LINE" in a*) echo ${FOO[*]}; unset FOO;  FOO[0]="$LINE";; *) FOO[$[${#FOO[@]}+1]]="$LINE";; esac;  done

gives nothing:

cat output.txt|while read LINE; do case "$LINE" in a*) unset FOO;  FOO[0]="$LINE";; *) FOO[$[${#FOO[@]}+1]]="$LINE";; esac;  done; echo ${FOO[*]}

Hi, try:

while read LINE
do
  case "$LINE" in 
      a*) echo ${FOO[*]}
          unset FOO
          FOO[0]="$LINE";;
       *) FOO[$[${#FOO[@]}+1]]="$LINE";;
  esac
done < output.txt
echo ${FOO[*]}

-or-

while read LINE
do
  case "$LINE" in
      a*) printf "\n"
  esac
  printf "%s" "$LINE"
done < output.txt
printf "\n"

The first one worked really well. Thanks!!! one last question -- what if rather than a file, i needed to grab the input from stdin? (and i can't write it out to a file first)

thanks!

[edit]

I ended up making what you gave me into its own script, and then called that from my first script and it all worked perfectly! thanks again!!