Need to limit the status printed

Hi,

I have the data set as below,

0221500612134|Nutro 30-35 lb. Dry Dg 3 of 10 08/29/13~
0221503074850|Nutro 30-35 lb. Dry Dg 1 of 10 09/23/13~
0221503499660|Blue Buff 24-30lb Dog F 1 of 10 02/26/13~
0221503499660|Iams 15.5-20lb Dog Food 2 of 10 11/12/12~
0221503499660|Nat Blnc 25-35lb Dog Fd 9 of 10 04/03/12~
0221503499660|Iams 35-40lb Dog Food++ 1 of 10 08/02/13~
0504400020771|Nutro 4-5lb Dog Food+++ 7 of 10 04/25/13~
0504400188767|Nutro 15-20 lb. Dry Dg 5 of 10 11/21/11~
0504400188767|Nutro 30-35 lb. Dry Dg 9 of 10 02/18/13~
0504400188767|Nutro 4-5lb Dog Food+++ 1 of 10 06/05/12~
0504400262575|Sci Diet 7-10lb Cat Foo 7 of 10 06/14/13~
0504400262575|Sci Dt 15.5-17.5lb Cat 1 of 10 02/07/12~
0504400323702|Pro Plan 7-8 lb Cat Fd+ 11 of 10 03/14/13~
0504400323702|Pro Plan 3.5-4lb Cat Fo 1 of 10 04/02/12~

The following shell will check for the first field and append the second field values to a single record if the first field value is common, say it will be like,

0504400323702|Pro Plan 7-8 lb Cat Fd+ 11 of 10 03/14/13~Pro Plan 3.5-4lb Cat Fo 1 of 10 04/02/12~|

Now the requirement is, if we have more than two records for the same first field, we should only append the second filed of these first two records, others has to be omitted,

Please let me know what modifications need to be done in below script

#!/bin/bash
###############################################################################
# Initialize variables required for this run.
###############################################################################
FILES=$1
OUTPUT_FILE=$PETC_OUT/Sample_Full_`date +%Y_%m_%d`
OUTPUT_FILE_FINAL=$PETC_OUT/Sample_Full.txt
counter=0
counter_final=0
first_record=1
###############################################################################
# For every record in the flat file check if belongs to the same houshold and
# concatenate to a single record. Attach the number of lines at the end for
# receipt printing.
###############################################################################
while read EachLine
do
Household_Id_curr=`echo $EachLine|cut -f1 -d'|'`
###############################################################################
# Handle the first record alone as there won't be a previous record for it.
###############################################################################
if [ $first_record = 1 ]
then
Temp_message=$Temp_message`echo $EachLine|cut -f2 -d'|'`
Household_Id_prev=`head -1  $FILES|cut -f1 -d'|'`
first_record=0
counter=`expr $counter + 1`
###############################################################################
# Handle the other subsequent records in the else part.
###############################################################################
else
###############################################################################
# Check if new houshold record is encountered during the process of reading.
###############################################################################
if [ ! "${Household_Id_curr}" = "${Household_Id_prev}" ]
then
Line_item=`expr $counter + 2`
###############################################################################
# Prepare the static message portion along with the number of lines to be 
# prined at the end.
###############################################################################
Static_end_mssage="~Status reflects purchases thru "`date +%m/%d/%y`".~|"$Line_item"|"
Receipt_message=$Household_Id_prev"|01|"$Temp_message$Static_end_mssage
echo  $Receipt_message>>$OUTPUT_FILE
###############################################################################
# Reset the variables to use it for the next household records.                    
###############################################################################
Receipt_message=""
Temp_message=`echo $EachLine|cut -f2 -d'|'`
counter=1
else
###############################################################################
# Concatenate the messages to one line if they belong to the same household.
###############################################################################
Temp_message="$Temp_message"`echo $EachLine|cut -f2 -d'|'`
counter=`expr $counter + 1`
fi
Household_Id_prev=$Household_Id_curr
###############################################################################
# Take the counter value for the final record as there are no subsequent records
# to process.           
###############################################################################
counter_final=`expr $counter + 2`
fi
done < $FILES
###############################################################################
# Extract the formated record for the last household as there are no subsequent
# households to be processed.
###############################################################################
echo  $Household_Id_curr"|01|"$Temp_message"~Status reflects purchases thru "`date +%m/%d/%y`".~|"$counter_final"|">>$OUTPUT_FILE
sed 's/+/\x20/g' $OUTPUT_FILE>$OUTPUT_FILE_FINAL
sed "s/$/$(date +%Y-%m-%d --date="Next Year")|/" $OUTPUT_FILE_FINAL>Sample_Full_temp
mv Sample_Full_temp $OUTPUT_FILE_FINAL
rm -f $OUTPUT_FILE

not clear what you want. may be this helps you..

This gives you unique values for column 1.

$awk -F "|" '!X[$1]++' file
0221500612134|Nutro 30-35 lb. Dry Dg 3 of 10 08/29/13~
0221503074850|Nutro 30-35 lb. Dry Dg 1 of 10 09/23/13~
0221503499660|Blue Buff 24-30lb Dog F 1 of 10 02/26/13~
0504400020771|Nutro 4-5lb Dog Food+++ 7 of 10 04/25/13~
0504400188767|Nutro 15-20 lb. Dry Dg 5 of 10 11/21/11~
0504400262575|Sci Diet 7-10lb Cat Foo 7 of 10 06/14/13~
0504400323702|Pro Plan 7-8 lb Cat Fd+ 11 of 10 03/14/13~