User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 392,094 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,888 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser:
Views: 3172 | Replies: 23 | Solved
Reply
Join Date: Feb 2005
Posts: 74
Reputation: tones1986 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster in Training

BASH script - sales per each associate (3 files)

  #1  
Dec 2nd, 2007
Hey guys - i am working off three files that contain information on associates and their sales.
The files are:

products: (product ID, name, price item)
% cat products
103:sway bar:49.99
101ropeller:104.99
104:fishing line:0.99
...
108:wheel:49.99
111:lock:31.00
102:trailer hitch:97.95

sales: (product ID,num. sold, date sale, associate ID)
% cat sales
110,1,01:02:2007,22
110,2,02:02:2007,23
109,1,03:03:2006,24
104,2,03:02:2007,24
...
104,2,05:03:2007,24
112,1,05:03:2007,23
104,9,05:03:2007,21

associates: (associate ID, name, salary, position)
% cat associates
21/John Doe/39000/Clerk
22/George Bush/99000/President
23/Susan Worker/44000/Manager
24/Fast Buck/21000/Stock Boy
25/Hillary Clinton/99000/Candidate
26/Dennis Miller/88000/Commedian

The output needs to come out something like:
$ ./report.sh
Name Position Sales Amount
========================================
Fast Buck Stock Boy 2712.77
George Bush President 1059.67
Susan Worker Manager 399.52
John Doe Clerk 284.74
Hillary Clinton Candidate 151.00
Dennis Miller Commedian 2.97

command line command: ./report.sh

I have done the following to start -

#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales.$$

COUNTER=21
while [ $COUNTER -le 26 ]
do
        awk '$1 == $COUNTER {print $0}' /tmp/newSales.$$ > /tmp/sales.$COUNTER.$$
        echo "SALES FOR ASSOCIATE $COUNTER"
        cat /tmp/sales.$COUNTER.$$
        let COUNTER=$COUNTER+1
done

#Below removes all created files
rm /tmp.newSales.$$
i=21
while [ $i -le 26 ]
do
        rm /tmp/sales.$COUNTER.$$
        let i=$i+1
done

As i hope you can see - what i want to do is to go through each associate (numbered 21-26) and take their sales and store it into their own sales file.

The first line i have makes sure that ONLY sales from 2007 are now in the sales file i will be working with ... does this make sense so far?

Is there a reason why the files though would end up being empty? Is there something i am overlooking? Thanks guys...
Last edited by tones1986 : Dec 2nd, 2007 at 8:11 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Posts: 74
Reputation: tones1986 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster in Training

Re: BASH script - sales per each associate (3 files)

  #2  
Dec 2nd, 2007
New attempt, slightly different..help?

#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales.$$
LINES=`wc -l newSales.$$`
echo lines in file $LINES
COUNTER=21
while [ $COUNTER -le 26 ]
do
  COUNT=1
  while [ $COUNT -lt $LINES ]
     do
         awk '$4 == $COUNTER {print $0}' /tmp/newSales.$$ > /tmp/sales.$COUNTER.$$
         let COUNT=$COUNT+1
  done
        echo "SALES FOR ASSOCIATE $COUNTER"
        cat /tmp/sales.$COUNTER.$$
        let COUNTER=$COUNTER+1
done

#Below removes all created files
rm /tmp/newSales.$$
i=21
while [ $i -le 26 ]
do
#       rm /tmp/sales.$COUNTER.$$
        let i=$i+1
done

gets me this:


/home/lx/z109079 : ./report.sh
wc: newSales.21066: No such file or directory
lines in file
./report.sh: line 9: [: 1: unary operator expected
SALES FOR ASSOCIATE 21
cat: /tmp/sales.21.21066: No such file or directory
./report.sh: line 9: [: 1: unary operator expected
SALES FOR ASSOCIATE 22
cat: /tmp/sales.22.21066: No such file or directory
./report.sh: line 9: [: 1: unary operator expected
SALES FOR ASSOCIATE 23
cat: /tmp/sales.23.21066: No such file or directory
./report.sh: line 9: [: 1: unary operator expected
SALES FOR ASSOCIATE 24
cat: /tmp/sales.24.21066: No such file or directory
./report.sh: line 9: [: 1: unary operator expected
SALES FOR ASSOCIATE 25
cat: /tmp/sales.25.21066: No such file or directory
./report.sh: line 9: [: 1: unary operator expected
SALES FOR ASSOCIATE 26
cat: /tmp/sales.26.21066: No such file or directory


Hope this gets me closer to some help - answer or something else!
Reply With Quote  
Join Date: Feb 2005
Posts: 74
Reputation: tones1986 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster in Training

Re: BASH script - sales per each associate (3 files)

  #3  
Dec 2nd, 2007
output from new script:


lines in file 25
SALES FOR ASSOCIATE 21
SALES FOR ASSOCIATE 22
SALES FOR ASSOCIATE 23
SALES FOR ASSOCIATE 24
SALES FOR ASSOCIATE 25
SALES FOR ASSOCIATE 26
rm: cannot remove `/tmp/sales.27.22499': No such file or directory
rm: cannot remove `/tmp/sales.27.22499': No such file or directory
rm: cannot remove `/tmp/sales.27.22499': No such file or directory
rm: cannot remove `/tmp/sales.27.22499': No such file or directory
rm: cannot remove `/tmp/sales.27.22499': No such file or directory
rm: cannot remove `/tmp/sales.27.22499': No such file or directory


script:
#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales.$$
cat /tmp/newSales.$$
LINES=`wc -l /tmp/newSales.$$ | cut -c1-2`
echo lines in file $LINES
COUNTER=21
while [ $COUNTER -le 26 ]
do
  COUNT=1
  while [ $COUNT -lt $LINES ]
     do
         awk -F, '$4 == $COUNTER {print $1","$2","$3","$4}' /tmp/newSales.$$ >> /tmp/sales.$COUNTER.$$
         let COUNT=$COUNT+1
     done
        echo "SALES FOR ASSOCIATE $COUNTER"
        cat /tmp/sales.$COUNTER.$$
        let COUNTER=$COUNTER+1
done

#Below removes all created files
rm /tmp/newSales.$$
i=21
while [ $i -le 26 ]
do
        rm /tmp/sales.$COUNTER.$$
        let i=$i+1
done

why arent my associates.$COUNTER.$$ files being filled with any information - they should be filled with all sales for the associate number, so something like this for associate 21.
104,9,03:01:2007,21
111,4,12:02:2007,21
107,9,03:01:2007,21
104,9,03:03:2007,21
104,9,05:03:2007,21

and then the same for each associate (similar files - notice how the $4 is 21, thats the associates ID)

I think there is something wrong with my while statements - please let me know... thanks
Reply With Quote  
Join Date: Oct 2007
Posts: 30
Reputation: stilllearning is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
stilllearning stilllearning is offline Offline
Light Poster

Re: BASH script - sales per each associate (3 files)

  #4  
Dec 3rd, 2007
Ok, I think this is roughly what you are looking for. You will still need to fine tune it to get the final answers you need. Some parts are hard coded, like assuming your associate id's range from 21 - 26. You might want to read those values in if you are trying to make something more generic. I hope this helps some.

Also, there is nothing wrong with your while loop. Your awk syntax is messed up.

What you are looking for, I think perhaps this ?

awk -F"," '/$COUNTER/ {print $0}' sales

#! /bin/bash

#generate a newSales file for each associate
ID=1
while [ $ID -le 6 ]
do
  awk /[2][$ID]/ sales > /tmp/newSales.$ID
  let ID=$ID+1
done

#read each associate file line by line, find the price
#for the product id sold by the associate. Assuming
#product id is in a file called product with records
#as follows '110.abc.22.13'
#compute total for each prod id using the quantity and
#price for the product
ID=1
while [ $ID -le 6 ]
do
  echo "SALES FOR ASSOCIATE 2$ID"
  while read line
  do
    PRODID=`echo $line|cut -d"," -f1`
    QTY=`echo $line|cut -d"," -f2`
    PRICE=`awk /$PRODID/ product|awk -F"." '{print $3"."$4}'`
    TOTAL=$(echo "scale=2; $PRICE*$QTY" | bc)
    echo "Associate 2$ID made $TOTAL for selling $PRODID"
  done < "/tmp/newSales.$ID"

  let ID=$ID+1
done

#Below removes all temp files
rm /tmp/newSales*
Last edited by stilllearning : Dec 3rd, 2007 at 12:51 am. Reason: fix errors
Reply With Quote  
Join Date: Feb 2005
Posts: 74
Reputation: tones1986 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster in Training

Re: BASH script - sales per each associate (3 files)

  #5  
Dec 3rd, 2007
I worked on what you said - and have come across a couple of problems ... first off i get errors while running the script:

/home/lx/z109079 : ./newtest.sh
./newtest.sh: line 25: unexpected EOF while looking for matching `"'
./newtest.sh: line 36: syntax error: unexpected end of file

Using this script:

#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales.$$
#cat /tmp/newSales.$$
#LINES=`wc -l /tmp/newSales.$$ | cut -c1-2`
#echo lines in file $LINES

ID=1
while [ $ID -le 6 ]
do
  awk /[2][$ID]/ /tmp/newSales.$$ >> /tmp/sales.2$ID
  let ID=$ID+1
done

ID=21
while [ $ID -le 6 ]
do
  echo "SALES FOR ASSOCIATE 2$ID"
  while read line
  do
    PRODID=`echo $line|cut -d"," -f1`
    QUANT=`echo $line|cut -d"," -f2`
    PRICE=`awk /$PRODID/ product|awj -F"." '(print $3"."$4}'`
    TOTAL=${echo "scale=2; $PRICE*QUANT" | bc)
    echo "Associate 2$ID made $TOTAL for selling $PRODID
    done < "/tmp/newSales.2$ID"

  let ID=$ID+1
done

# removes all created files
#rm /tmp/newSales.*

Also - i commented out the rm line at the bottom to see what, if anything was being stored in the sales.2$ID files within /tmp/ ... they all looked good except the sales for user ID 21 as shown below - for whatever reason, they include sales for associate 22 also - is this because some of associate 22's field are '21'??? That is the only reason i can see...

/home/lx/z109079 : cat sales.21
104,9,03:01:2007,21
111,4,12:02:2007,21
107,9,03:01:2007,21
108,21,03:02:2007,22
104,9,03:03:2007,21
104,9,05:03:2007,21
104,9,03:01:2007,21
111,4,12:02:2007,21
107,9,03:01:2007,21
108,21,03:02:2007,22
104,9,03:03:2007,21
104,9,05:03:2007,21
104,9,03:01:2007,21
111,4,12:02:2007,21
107,9,03:01:2007,21
108,21,03:02:2007,22
104,9,03:03:2007,21
104,9,05:03:2007,21


I really hope you can help me out some more - seems like im getting the hang of it and understanding it more and more -

Thanks again...
Reply With Quote  
Join Date: Oct 2007
Posts: 30
Reputation: stilllearning is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
stilllearning stilllearning is offline Offline
Light Poster

Re: BASH script - sales per each associate (3 files)

  #6  
Dec 3rd, 2007
Your syntax errors are because of these 2 lines.

TOTAL=${echo "scale=2; $PRICE*QUANT" | bc)
echo "Associate 2$ID made $TOTAL for selling $PRODID


They should be (Need a ( bracket in line 1 and you have a missing quote in line 2)

TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
echo "Associate 2$ID made $TOTAL for selling $PRODID"


Also since you are only looking to isolate values based on the match the associate id, which in your case is luckily the final field . Try this instead (adds a $ at the end of the pattern to anchor it to the end of the line)

awk /[2][$ID]$/ /tmp/newSales.$$ >> /tmp/sales.2$ID
Reply With Quote  
Join Date: Feb 2005
Posts: 74
Reputation: tones1986 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster in Training

Re: BASH script - sales per each associate (3 files)

  #7  
Dec 3rd, 2007
Hey - i fixed the things you mentioned and it seems to run - but now im getting slightly different errors and alot of them...


Associate 25 made for selling 112
(standard_in) 1: illegal character: Q
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: N
(standard_in) 1: illegal character: T
Associate 25 made for selling 111
(standard_in) 1: illegal character: Q
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: N
(standard_in) 1: illegal character: T
Associate 25 made for selling 112
(standard_in) 1: illegal character: Q
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: N
(standard_in) 1: illegal character: T
Associate 25 made for selling 111
(standard_in) 1: illegal character: Q
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: N
(standard_in) 1: illegal character: T
Associate 25 made for selling 112
(standard_in) 1: illegal character: Q
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: N
(standard_in) 1: illegal character: T
Associate 25 made for selling 111
SALES FOR ASSOCIATE 26
(standard_in) 2: illegal character: Q
(standard_in) 2: illegal character: U
(standard_in) 2: illegal character: N
(standard_in) 2: illegal character: T
Associate 26 made 0 for selling 104
(standard_in) 2: illegal character: Q
(standard_in) 2: illegal character: U
(standard_in) 2: illegal character: N
(standard_in) 2: illegal character: T
Associate 26 made 0 for selling 104
(standard_in) 2: illegal character: Q
(standard_in) 2: illegal character: U
(standard_in) 2: illegal character: N
(standard_in) 2: illegal character: T
Associate 26 made 0 for selling 104
(standard_in) 2: illegal character: Q
(standard_in) 2: illegal character: U
(standard_in) 2: illegal character: N
(standard_in) 2: illegal character: T
Associate 26 made 0 for selling 104
(standard_in) 2: illegal character: Q
(standard_in) 2: illegal character: U
(standard_in) 2: illegal character: N
(standard_in) 2: illegal character: T
Associate 26 made 0 for selling 104
(standard_in) 2: illegal character: Q
(standard_in) 2: illegal character: U
(standard_in) 2: illegal character: N
(standard_in) 2: illegal character: T
Associate 26 made 0 for selling 104
(standard_in) 2: illegal character: Q
(standard_in) 2: illegal character: U
(standard_in) 2: illegal character: N
(standard_in) 2: illegal character: T
Associate 26 made 0 for selling 104
(standard_in) 2: illegal character: Q
(standard_in) 2: illegal character: U
(standard_in) 2: illegal character: N
(standard_in) 2: illegal character: T
Associate 26 made 0 for selling 104

Script:

#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales.$$
#cat /tmp/newSales.$$
#LINES=`wc -l /tmp/newSales.$$ | cut -c1-2`
#echo lines in file $LINES

ID=1
while [ $ID -le 6 ]
do
  awk '$4 == /[2][$ID]$/ {print $0}' /tmp/newSales.$$ >> /tmp/sales.2$ID
  let ID=$ID+1
done

ID=1
while [ $ID -le 6 ]
do
  echo "SALES FOR ASSOCIATE 2$ID"
  while read line
  do
    PRODID=`echo $line|cut -d"," -f1`
    QUANT=`echo $line|cut -d"," -f2`
    PRICE=`awk /$PRODID/ products|awk -F"." '{print $3"."$4}'`
    TOTAL=$(echo "scale=2; $PRICE*QUANT" | bc)
    echo "Associate 2$ID made $TOTAL for selling $PRODID"
    done < "/tmp/sales.2$ID"

  let ID=$ID+1
done

# removes all created files
rm /tmp/newSales.*

I noticed i had a ( instead of a { around a print for awk and i mistyped awk awj ... fixed those and i still got the above errors - am i reading the wrong part of the file?

Any ideas? Ill try playing around with stuff - but am unsure about some of the codes u have written - i will look up such things as aw
Reply With Quote  
Join Date: Oct 2007
Posts: 30
Reputation: stilllearning is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
stilllearning stilllearning is offline Offline
Light Poster

Re: BASH script - sales per each associate (3 files)

  #8  
Dec 3rd, 2007
You accidentally removed the $ before the QUANT. Put that back in, and you'll be fine.
Reply With Quote  
Join Date: Feb 2005
Posts: 74
Reputation: tones1986 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster in Training

Re: BASH script - sales per each associate (3 files)

  #9  
Dec 3rd, 2007
Yeah i noticed that - just changed it... and got this:


/home/lx/z109079 : ./newtest.sh
SALES FOR ASSOCIATE 21
SALES FOR ASSOCIATE 22
Associate 22 made 0 for selling 110
Associate 22 made 0 for selling 108
Associate 22 made 0 for selling 110
Associate 22 made 0 for selling 108
Associate 22 made 0 for selling 110
Associate 22 made 0 for selling 108
Associate 22 made 0 for selling 110
Associate 22 made 0 for selling 108
SALES FOR ASSOCIATE 23
Associate 23 made 0 for selling 110
Associate 23 made 0 for selling 112
Associate 23 made 0 for selling 110
Associate 23 made 0 for selling 112
Associate 23 made 0 for selling 110
Associate 23 made 0 for selling 112
Associate 23 made 0 for selling 110
Associate 23 made 0 for selling 112
Associate 23 made 0 for selling 110
Associate 23 made 0 for selling 112
Associate 23 made 0 for selling 110
Associate 23 made 0 for selling 112
Associate 23 made 0 for selling 110
Associate 23 made 0 for selling 112
Associate 23 made 0 for selling 110
Associate 23 made 0 for selling 112
SALES FOR ASSOCIATE 24
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0 for selling 113
Associate 24 made 0
0 for selling 104
Associate 24 made 0 for selling 105
Associate 24 made 0 for selling 109
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0 for selling 113
Associate 24 made 0
0 for selling 104
Associate 24 made 0 for selling 105
Associate 24 made 0 for selling 109
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0 for selling 113
Associate 24 made 0
0 for selling 104
Associate 24 made 0 for selling 105
Associate 24 made 0 for selling 109
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0
0 for selling 104
Associate 24 made 0 for selling 113
Associate 24 made 0

Very strange - and once again, no information for sales associate 21, he did sell somethings!

Haha - well deal with that once the addition part work!
Reply With Quote  
Join Date: Oct 2007
Posts: 30
Reputation: stilllearning is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
stilllearning stilllearning is offline Offline
Light Poster

Re: BASH script - sales per each associate (3 files)

  #10  
Dec 3rd, 2007
It will be easier for you to debug this, if you break it up into smaller scripts to make sure each part is being done correctly.

A couple of things. $$ is usually the process id of -bash, so you may just want to use a file name like newSales, so that you have a constant. It'll be easier to check the contents while you're debugging.

Also this line

awk '$4 == /[2][$ID]$/ {print $0}' /tmp/newSales.$$ >> /tmp/sales.2$ID

If all you want to do is send particular associate id's to one file, this will suffice. Not sure if you want to append to the existing file or overwrite.

awk /[2][$ID]$/ /tmp/newSales.$$ > /tmp/sales.2$ID

Make sure you have the correct content in the sales.2$ID files. Then see that you get the correct product id, quantity and price for each record associated with the files, and do the total price calculations.

Try this.

#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales
#cat /tmp/newSales.$$
#LINES=`wc -l /tmp/newSales.$$ | cut -c1-2`
#echo lines in file $LINES

ID=1
while [ $ID -le 6 ]
do
  awk /[2][$ID]$/ /tmp/newSales > /tmp/sales.2$ID
  let ID=$ID+1
done

ID=1
while [ $ID -le 6 ]
do
  echo "SALES FOR ASSOCIATE 2$ID"
  while read line
  do
    PRODID=`echo $line|cut -d"," -f1`
    QUANT=`echo $line|cut -d"," -f2`
    PRICE=`awk /$PRODID/ products|awk -F"." '{print $3"."$4}'`
    echo $PRODID $QUANT $PRICE
    TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
    echo "Associate 2$ID made $TOTAL for selling $PRODID"
  done < "/tmp/sales.2$ID"

  let ID=$ID+1
done

# removes all created files
#rm /tmp/newSales.*
Last edited by stilllearning : Dec 3rd, 2007 at 1:29 pm. Reason: add code
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Shell Scripting Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 12:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC