Need to limit the status printed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to limit the status printed
# 1  
Old 09-26-2012
Need to limit the status printed

Hi,

I have the data set as below,
Code:
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,
Code:
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
Code:
#!/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


Last edited by Franklin52; 09-26-2012 at 10:22 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 09-26-2012
not clear what you want. may be this helps you..

This gives you unique values for column 1.

Code:
$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~

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

How to track table status delete/update/insert status in DB2 V10 z/os?

Dear Team I am using DB2 v10 z/os database . Need expert guidance to figure out best way to track table activities ( Ex Delete, Insert,Update ) Scenario We have a table which is critical and many developer/testing team access on daily basis . We had instance where some deleted... (1 Reply)
Discussion started by: Perlbaby
1 Replies

2. Shell Programming and Scripting

Value not getting printed outside loop

Hi all I'm using below code #!/bin/bash export fileclob cd /home/appsuser/dataload file='EG.mdd' chmod 777 $file dos2unix -ascii -k -q -o $file $file sed -e '${/^$/d}' $file cat $file | while read LINE do echo "line is" if then echo "line is $LINE" echo " " ... (10 Replies)
Discussion started by: Pratiksha Mehra
10 Replies

3. Shell Programming and Scripting

Variable value not getting printed

Hi, I ma trying to do this but don't know why it is not happening? $r1=10 for i in "1" "2" "3" "4"; do x=`eval echo $i`; echo r${x}; done output: r1 r2 r3 r4 also tried for i in "1" "2" "3" "4"; do x=`eval echo $i`; echo $r${x}; done output: 1 (2 Replies)
Discussion started by: abhi1988sri
2 Replies

4. Solaris

Limit: stacksize: Can't remove limit

Hi all, I'm using to Solaris machine. When I run a simple script this messenger come out:"limit: stacksize: Can't remove limit". Any one know the way to resolve this problem without reboot the machine? Thanks in advance. (3 Replies)
Discussion started by: Diabolist9
3 Replies

5. Programming

Value printed by gdb does not consist with the right value

Hello, I find the value printed by gdb does not consist with the right value.The following is the output. (gdb) 7 while ( ( optc = getopt(argc, argv, ":b:B:h" ) ) != -1 ) { (gdb) 8 printf( "%c %d %s\n", optc, optind, optarg); (gdb) B 5 1-2 7 while ( ( optc =... (1 Reply)
Discussion started by: 915086731
1 Replies

6. UNIX for Dummies Questions & Answers

Messages printed in the shell

Hi, I would like to be able to return to the messages printed in the shell when a process is done, but I have no idea where to look for them. Done nohup script.sh (wd: ~/somesubdir) Can anyone give me a hint? Are these messages printed by bash? They're definitely not... (7 Replies)
Discussion started by: mregine
7 Replies

7. Shell Programming and Scripting

background jobs exit status and limit the number of jobs to run

i need to execute 5 jobs at a time in background and need to get the exit status of all the jobs i wrote small script below , i'm not sure this is right way to do it.any ideas please help. $cat run_job.ksh #!/usr/bin/ksh #################################### typeset -u SCHEMA_NAME=$1 ... (1 Reply)
Discussion started by: GrepMe
1 Replies

8. UNIX for Advanced & Expert Users

lp - order of files printed

I have a shell script that is looping through a list of Postscript files to print. ls -1tr *.PS > print.lst ... PRINT_LIST=`cat print.lst` ... for DMFILE in $PRINT_LIST do lp -d $PRINTER_NAME -o legal $DMFILE ... done The files in print.lst are in the order that they should be... (2 Replies)
Discussion started by: mabrownawa
2 Replies

9. UNIX for Dummies Questions & Answers

Couldn't open status file /var/samba/STATUS.LCK

I believe i have most of samba configured right but i get this error each time time try to run it. I was given suggestion that i touch the file, i did, but i still cannot rid myself of this error. Any suggestions (2 Replies)
Discussion started by: macdonto
2 Replies
Login or Register to Ask a Question