Issue in shell script

item,quantity,unit price,total
tea,1,1,1
coffee,3,4,12
sugar,5,2.5,12.5
tea,2,1,3
coffee,2,4,8

i have got the above file with following questions:
need to get the total value: which i got using :

a=0
for b in `cut -f4 -d ',' log`
do
c=`echo $a + $b | bc`
a=$c
done
echo "$a and $c"
  1. highest selling item in terms of quantity?
    3.most orders item?

guys can you please with the same.

---------- Post updated at 09:32 PM ---------- Previous update was at 08:11 PM ----------

a=0
for b in `cut -f4 -d ',' log`
do
c=`echo $a + $b | bc`
a=$c
done
echo "Total sales for today is : $a"
                                                                                
                                                                                
cut -f1 -d ',' log |grep -v item| sort -u >> 1
for a in `cat 1`
do
touch $a
done
                                                                                
                                                                                
for a in `cat log`
do
b=`echo $a|cut -f1 -d ','`
c=`grep $b 1| wc -l`
if [ $c -ge 1 ]
then
echo $a >> $b
fi
done
 
for a in `cat 1`
do
c=0
for b in `cat $a |cut -f2 -d ','`
do
d=`echo $b + $c | bc` #expr $b + $c`
c=$d
done
echo "$a quantity with : $c" >> bhu_out
done
f=`cut -f2 -d ":" bhu_out | sort -r | head -1`
g=`grep $f bhu_out | cut -f1 -d ' '`
h=`grep $f bhu_out | cut -f2 -d ':'`
echo "$g is the highest selling item with quantity : $h"

Before you look at the script, spot the obvious mistake in the file:

Should be

tea,2,1,2

And then, after you clean up the input data, please tell us what operating system and shell you're using. What you have is highly inefficient. If you tell us what operating system and shell you're using, we can suggest something faster and much more efficient using awk or shell built-ins (depending on your environment).

Thank you very much for pointing out the mistake....

Operating system is red hat 9.0 and Aix 5.3...even I was thinking to use awk but was not sure of syntax....

Please suggest a better way..

Is this homework, bhupeshchavan ?

Please answer Scrutinzer's question: Is this homework?

If it isn't there are a few other questions that need to be answered:

  1. What shell are you using?
  2. Note that in your sample data, you have two items that have the same "highest quantity" sold and two other items that have the "highest number of sales transactions". How do you determine which should be given the "highest" label? (Or, more to the point, why don't you report all items with the same statistics when there is a tie?)
  3. You said you wanted your script to print three values. Are all of the files your script creates used for later processing? Or, are they just artifacts left over after your script completes?
  4. Could you please explain why you believe that the following code is needed in your script?:
	cut -f1 -d ',' log |grep -v item| sort -u >> 1
	for a in `cat 1`
	do
	touch $a
	done
1 Like

This is not a homework....

based on the logic i will another script which might have similar..as of now the request is not yet received.....

  1. at home i have got bash...in office it is ksh..but it has be ksh complaint since
    i will writing it in ksh later one.
    2.missed out pointing if there is tie we need to report those as well....
    3.the files which i created are used for my calculation to get the end result.....we need to delete them as the script as finished....need to add rm statement at the end of the script.
    4.The code which you are referring will create a text file with all the unique item so that we can add similar item in those files and get the result....

note : we might have some other data for which we might have to use specific grep pattern....but since this is sample date or similar data that i will have deal with hence i m preparing for it...

Hope it helps.

OK. The following ksh script will also work with bash .

OK. The following ksh / awk script will report ties.

OK. The following script just doesn't create those files.

It doesn't create any files that won't be created by later steps in your code. And, more importantly, if files already exist with those names, it won't discard data created in earlier runs. The touch commands in that loop don't do anything but set the timestamps on those files to the current time if the files exist, or create them if they don't already exist. This loop does not put any data in any of those files.

My crystal ball isn't good enough to guess at what you might want to do but haven't specified. The following code does produce some output you said you wanted that I don't see being produced by your script.

Maybe this script:

#!/bin/ksh
awk -F, '
FNR > 1 {
	total_sales += $4
	if(++count[$1] > mc)
		mc = count[$1]
	if((quantity[$1] += $2) > mq)
		mq = quantity[$1]
	if((sales[$1] += $4) > ms)
		ms = sales[$1]
}
END {	printf("Total sales for today is : %.2f\n\n", total_sales)
	for(item in quantity)
	    if(quantity[item] == mq)
		printf("%s is the highest selling item with quantity : %d\n",
		    item, mq)
	print ""
	for(item in count)
	    if(count[item] == mc)
	    	printf("%s had the most times sold : %d\n", item, mc)
	print ""
	for(item in sales)
	    if(sales[item] == ms)
	    	printf("%s had the highest income : %.2f\n", item, sales[item])
}' log

which, with your sample data produces the output:

Total sales for today is : 35.50

sugar is the highest selling item with quantity : 5
coffee is the highest selling item with quantity : 5

tea had the most times sold : 2
coffee had the most times sold : 2

coffee had the highest income : 20.00

(but the order of the output of items in each section of the output may vary with different version of awk ) will give you an idea of how to report similar results for other questions that might arise from your data.

This will work as is on both Red Hat and AIX. If someone wants to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

Note that it only reads your data file once and produces no temporary files. The awk code used in this script is pretty straight forward. If you don't understand how it works (after looking at the awk man page), feel free to ask questions.

Hi Don,

Thank you very much for the script, it is working as it should be.

Since my knowledge on awk is minimal, i was not able to understand the script...could you please explain the script in your free time for our benefit.

cheers...

regards,
bhupesh

Hi bhupesh,
Did you look at the awk man page on your system? (i.e., run the command man awk ?)

I would be happy to try to explain any part of that script that you are having trouble understanding. What part(s) of the script were you not able to understand?

  • Don