•
•
•
•
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 456,444 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 2,633 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: Programming Forums
Views: 3738 | Replies: 23 | Solved
![]() |
| |
•
•
Join Date: Feb 2005
Posts: 92
Reputation:
Rep Power: 4
Solved Threads: 0
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 -
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...
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
doneAs 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 9:11 pm.
•
•
Join Date: Feb 2005
Posts: 92
Reputation:
Rep Power: 4
Solved Threads: 0
New attempt, slightly different..help?
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!
#! /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
donegets 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!
•
•
Join Date: Feb 2005
Posts: 92
Reputation:
Rep Power: 4
Solved Threads: 0
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:
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
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
donewhy 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
•
•
Join Date: Oct 2007
Posts: 302
Reputation:
Rep Power: 3
Solved Threads: 37
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
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 1:51 am. Reason: fix errors
•
•
Join Date: Feb 2005
Posts: 92
Reputation:
Rep Power: 4
Solved Threads: 0
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:
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...
/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...
•
•
Join Date: Oct 2007
Posts: 302
Reputation:
Rep Power: 3
Solved Threads: 37
Your syntax errors are because of these 2 lines.
They should be (Need a ( bracket in line 1 and you have a missing quote in line 2)
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)
TOTAL=${echo "scale=2; $PRICE*QUANT" | bc)
echo "Associate 2$ID made $TOTAL for selling $PRODIDThey 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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Bash Shell Script - sales/product/cost (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: Need Help! Could someone please check the two tiny bash scripts to see if they work?
- Next Thread: Please help


Hybrid Mode