Merging columns in a file using Awk

awktext processing

Input:

ABC,SSSD,12345,NSS,12345,xxx,TS11
,,,,,,TS21
,,,,,,TS22
,,,,,,BS26
,,,,,,GPRS
ABC,SSSD,12356,NSS,12356,xxx,TS11
,,,,,,TS21
,,,,,,TS22
,,,,,,GPRS
,,,,,,BS26

Output:

ABC,SSSD,12345,NSS,12345,xxx,TS11|TS21|TS22|BS26|GPRS
ABC,SSSD,12356,NSS,12356,xxx,TS11|TS21|TS22|GPRS|BS26

I have already tried by writing the below script. But it is performance intensive:

#!/bin/bash
head -2 $1 | tail -1 >> $1"_output"
sed '1,2d' $1 > temp$1.txt
cp temp$1.txt $1
rm -f temp$1.txt
while IFS='' read -r line || [[ -n $line ]]; do
    #    echo "main line -- "$line
    #    echo "prev line -- "$prevLine
    service=`echo $line | cut -d "," -f1`
    value=`echo $line | cut -d "," -f7`
    if [ "$service" != "" ]
    then
        if [ "$prevLine" != "" -a "$mvalue" != "" ]
        then
            echo $prevLine$mvalue >> $1"_output"
        fi
        prevLine=$line
        mvalue=""
    else
        #echo $value
        mvalue=$mvalue"|"$value
        #echo $mvalue
    fi
done < "$1"
echo $prevLine$mvalue >> $1"_output"

Can somebody suggests a better way to do this using awk or any better method?

Best Answer

If you are not too fussy about white space, you do it with two awk rules like this:

 awk '$1 { printf "\n" $0 } !$1 { printf OFS $7 }' FS=, OFS='|' infile

This leaves an empty line at the head of the output, and misses one at the end. To fix this add if and END clauses:

awk '$1 { if(NR>1) printf "\n"; printf $0 } !$1 { printf OFS $7 } END { printf "\n" }' FS=, OFS='|' infile

Output:

ABC,SSSD,12345,NSS,12345,xxx,TS11|TS21|TS22|BS26|GPRS
ABC,SSSD,12356,NSS,12356,xxx,TS11|TS21|TS22|GPRS|BS26

Edit - note about % in $0

As Glenn noted in the comments, if your lines contain %, it is safer to call printf with a format string, e.g.:

awk '$1 { if(NR>1) printf "\n"; printf "%s", $0 } !$1 { printf OFS"%s", $7 } END { printf "\n" }' FS=, OFS='|' infile
Related Question