My input file had customer info and auto info. My goal was to combine records where a customer had more than one auto, so only one letter would be sent. Random notes:
comptxt is what I am comparing, the customer info
autotxt is the vehicle specifics
comptxts hold the saved information
you will see me write lots of "|" pipes, so I can later easily manipulate the data
the script must do a last write since I fall out of the loop when done reading, but I did not write the last record
For you, compare cut -c1-15 similar to my comptxt, and cut -c17-20 similar to my autotxt.
Lastly, there might be a few other variables in that code section you can ignore as they were more for my purposes (like vehcnt).
#combine lines where key is same
echo "** extracting info from "$wkfile30" to "$wkfile40
comptxts=""
vehicle=""
recno=0
vehcnt=0
while read zf
do
recno=$((recno + 1))
#grab the key name for comparison
comptxt=$(echo "$zf" | cut -d"|" -f2)
autotxt=$(echo "$zf" | cut -d"|" -f3)
#need to skip first time through since of course different
if [ -n "$comptxts" ]
then
if [ "$comptxts" != "$comptxt" ]
then
#format output dataline
vehcnttxt=$(printf "%.4d" "$vehcnt")
autotxto=$(printf "%-100s" "$autotxts")
echo "$outtxt1""|""$vehcnttxt""|""$autotxto""|-" >>$wkfile40
comptxts="$comptxt"
autotxts="$autotxt"
vehcnt=0
else
autotxts="$autotxts""$autotxt"
fi
else
comptxts="$comptxt"
autotxts="$autotxt"
fi
vehcnt=$((vehcnt+1))
outtxt1="$zf"
done < $wkfile30
#still need to output the last record !!
vehcnttxt=$(printf "%.4d" "$vehcnt")
autotxto=$(printf "%-100s" "$autotxts")
echo "$outtxt1""|""$vehcnttxt""|""$autotxto""|-" >>$wkfile40
Please don't hijack threads - start a new thread for new unrelated questions.
Forum's rules can be found here.
Yes, it is possible with either awk or perl.