Transpose Column of Data to Rows

I can no longer find my commands, but I use to be able to transpose data with common fields from a single column to rows using a command line. My data is separated as follows:

NAME=BOB
ADDRESS=COLORADO
PET=CAT
NAME=SUSAN
ADDRESS=TEXAS
PET=BIRD
NAME=TOM
ADDRESS=UTAH
PET=DOG

I would like my data to be transposed so that it appears as the following, I do not care about keeping the Name, Address, or Pet:

BOB,COLORADO,CAT
SUSAN,TEXAS,BIRD
TOM,UTAH,DOG

Thanks,
Dave

count=1
while read line; do
  output=`echo $line | sed "s/.*=//"`
  echo -n $output
  if [ $count -lt 3 ]; then 
    echo -n ","
    let "count++"
  else
    echo
    count=1
  fi
done < dave.txt

Solution using awk program:

awk -F'=' '{$0=$NF}!(NR%3){ORS=RS}NR%3{ORS=","}1' file
$ sed "s/^[^=]*=//;N;N;s/\n[^=]*=/,/g" file
BOB,COLORADO,CAT
SUSAN,TEXAS,BIRD
TOM,UTAH,DOG

The simplest solution is probably to use the standard cut and paste tools:

cut -d= -f2 file | paste -d, - - -

Regards,
Alister

1 Like

Yes, that's by far the best solution. The "paste - - -" is very nice.

Thanks, the paste command looks familiar.

awk variation
awk -F= '/NAME/ {printf "%s,",$2;getline;printf "%s,",$2;getline;printf "%s\n",$2}' file