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?
,,,,,,TS21is as it is shown or is itABC,SSSD,12345,NSS,12345,xxx,TS21?