LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-02-2007, 10:23 PM   #16
ccfc1986
LQ Newbie
 
Registered: Dec 2007
Posts: 14

Original Poster
Rep: Reputation: 0

As you can see in my post... i have the ` ` around my code...that part works fine - it gives me 25, which is the exact number it should be...my problem is with my while loop i think and the variables i use etc...thanks anyway though for your time
 
Old 12-03-2007, 01:01 AM   #17
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I recommend you use the options
set -xv
see http://tldp.org/LDP/abs/html/options.html to see what's really going on.
Also, personally I wouldn't use 2 very similarly named vars eg $count, $counter... how about $ctr & $assoc.
It's less confusing to read/think about.
 
Old 12-03-2007, 02:32 AM   #18
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN { FS=","
    while ( ( getline l < "products") > 0 ){
      n=split(l,line,":")
      products[line[1]] = line[3] 
    }
    while ( (getline ass < "associates" ) > 0 ) {
      m =split(ass, assoc,"/")
      associates[assoc[1]] = assoc[2]
      position[assoc[1]] = assoc[4]
    }       
}
{
    total[$4] += products[$1]
}
END{
   for ( value in total ){
    print total[value],value
   }
}' sales
output:
Code:
# ./test.sh
0.99 21
0 22
0 23
1.98 24

Last edited by ghostdog74; 12-03-2007 at 02:51 AM.
 
Old 12-03-2007, 05:36 AM   #19
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Any time you test variables it's always a good idea to put them in quotes:

while [ $var1 -lt $var2 ];do
s/b
while [ "$var1" -lt "$var2" ];do

Same thing if you use if, until or test.
 
Old 12-03-2007, 10:52 AM   #20
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Here is a little help, not a complete answer but you can tweek it to do what you want.
Code:
#!/bin/sh
MYDIR="$HOME/tmp"
YEAR="2007"
SALES=sales
PROD=products
ASSOC=associates
cd "$MYDIR"

get_sales() {
# This puts all items sold by EMP_NO in array ALLSALES
ALLSALES=( $(sed "/$EMP_NO/!d; /$YEAR/!d" $SALES) )
# This gets the number of elements in the array
NUM=${#ALLSALES[@]}
# Now we loop through the sales entries
until [ "$NUM" -eq "0" ];do
  NUM="$(( --NUM ))"
  # Arrays are 0 based so we decrement NUM before
  # accessing the array element
  SALE=( ${ALLSALES[$NUM]//,/ } )
  get_price
done
printit
}

get_price() {
# This looks up item description from products file
# The product description is stored in array DESC
OLDIFS=$IFS;IFS=: 
DESC=( $(grep "^${SALE[0]}" $PROD) )
UNITNO=${DESC[0]}
UNITNAME=${DESC[1]}
UNITPRICE=${DESC[2]}
IFS=$OLDIFS 
get_total
}

get_total() {
echo "Item = ${SALE[0]} Qty =  ${SALE[1]} Price = $UNITPRICE"
#You should use what you have and get the total
}

printit() {
echo "Print a formatted output"
}

# Here is where we start
# This will read employee no from associate file
while read DATA;do
  EMP_NO=${DATA/\/*/}
  #Function to search sales file and get sales for $EMP_NO
  echo "Employee $EMP_NO"
  get_sales
done <$ASSOC
$cat products
103:sway bar:49.99
101:propeller:104.99
104:fishing line:0.99
108:wheel:49.99
110:widget:15.00
111:lock:31.00
112:gadget:5.00
102:trailer hitch:97.95

$cat sales
110,1,01:02:2007,22
110,2,02:02:2007,23
103,2,02:02:2005,21
109,1,03:03:2006,24
104,2,03:02:2007,24
103,2,02:02:2007,21
104,2,05:03:2007,24
112,1,05:03:2007,23
104,9,05:03:2007,21

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


Here is the output:
$./doit
Employee 21
Item = 104 Qty = 9 Price = 0.99
Item = 103 Qty = 2 Price = 49.99
Print output
Employee 22
Item = 110 Qty = 1 Price = 15.00
Print output
Employee 23
Item = 112 Qty = 1 Price = 5.00
Item = 110 Qty = 2 Price = 15.00
Print output
Employee 24
Item = 104 Qty = 2 Price = 0.99
Item = 104 Qty = 2 Price = 0.99
Print output
Employee 25
Print output
Employee 26
Print output

Last edited by /bin/bash; 12-03-2007 at 10:53 AM. Reason: Disable smilies
 
Old 12-03-2007, 05:49 PM   #21
ccfc1986
LQ Newbie
 
Registered: Dec 2007
Posts: 14

Original Poster
Rep: Reputation: 0
thanks for the reply /bin/bash

Thanks for the reply... i have been working on a similar script as the one you wrote out for me... and i have it pretty much working minus 2 things (pretty much)...

I am working now on getting the names from the associate file to store into an array such as:
Code:
 NAMES[$ID]=`awk -F"/" '{print $2}' associates`
But as i found out that will go through associates and store ALL names into the array.

Is there a way around this - for it to take line one, find the name ($2), store into [1], take line two, find the name ($2), store into [2], and so on....?

Here is my script (please note there may be variations of the above code in multiple places, this is me trying to find the right place, until i realized that it just wont work the way i have coded the line)...

The idea is that if i can get the names into an array[1-6] so that
($ID = 1)
${NAMES[$ID]} = "John Doe"
($ID = 2)
${NAMES[$ID]} = "George Bush"
etc...

I could then move on and do similar thing for their respective job titles (obviously it will be same thing but different array name).

Then i can just use a while loop such as:

Code:
ID=1
while [ "$ID" -le 6 ]
  do
  echo "${NAMES[$ID]}      ${POSITION[$ID]}        ${TOTALSALES[$ID]}"
  ID=$ID+1
  done
So if i could get a hand how i can handle and store the names into an array, that would be awesome - if i get it before someone posts, ill edit the post accordingly, thanks guys.

***************EDIT****************
You need to see my entire code?

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

ID=1
NAMES=()
while [ $ID -le 6 ]
do
  awk /[2][$ID]$/ /tmp/newSales > /tmp/sales.2$ID
  let ID=$ID+1
done
GRANDTOTAL=0
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}'`
    TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
    GRANDTOTAL=$(echo "scale=2; $GRANDTOTAL+$TOTAL" | bc)
done < "/tmp/sales.2$ID"

#  NAMES[$ID]=`awk -F"/" '{print $2}' associates`
 # echo "NAME[$ID]: ${NAMES[$ID]}"
  echo Grand total for 2$ID: $GRANDTOTAL

  GRANDTOTAL=0
let ID=$ID+1
done

#NAME[1]=`awk -F"/" '{print $2' associates`
#echo new names array : ${NAMES[1]}

ID=1
#while ["$ID" -le 6]
#  do
#    echo "2$ID     ${NAMES[$ID]}    TOTAL (LATER )"
#    ((ID+=1))
#  done
# removes all created files
rm /tmp/sales.2*
rm /tmp/newSales
A run of the script produces:

/home/lx/z109079 : ./newtest.sh
SALES FOR ASSOCIATE 21
Grand total for 21: 284.74
SALES FOR ASSOCIATE 22
Grand total for 22: 1059.67
SALES FOR ASSOCIATE 23
Grand total for 23: 279.52
SALES FOR ASSOCIATE 24
Grand total for 24: 2504.83
SALES FOR ASSOCIATE 25
Grand total for 25: 151.00
SALES FOR ASSOCIATE 26
Grand total for 26: 1.98

Last edited by ccfc1986; 12-03-2007 at 05:51 PM.
 
Old 12-04-2007, 09:28 AM   #22
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
As was stated earlier since you have different field separators in each file, when you access them you need to set IFS. You can see I did that when I accessed the products file:
OLDIFS=$IFS;IFS=:
You should always store the IFS before you change it, then you can replace it when you are done, just like I did:
IFS=$OLDIFS
Also as was mentioned earlier you should break the task into small chunks (functions) and work on them one at a time, like I did:


get_sales()
get_price()
get_total()
printit()

If you look at the start of my script, (actually at the end) that is where I read the employee data form the associates file. I could easily read into an array by changing the IFS to '/' instead. Remember I would have to put back the original IFS before accessing any other files which have different IFS.

So I could change this:

# Here is where we start
# This will read employee no from associate file
while read DATA;do
EMP_NO=${DATA/\/*/}
#Function to search sales file and get sales for $EMP_NO
echo "Employee $EMP_NO"
get_sales
done <$ASSOC


to this:



# Here is where we start
# This will read employee no from associate file
ORIGIFS=$IFS;IFS=/
while read -a EMP_DATA;do
echo "Employee No: ${EMP_DATA[0]} Name: ${EMP_DATA[1]} Job Title: ${EMP_DATA[3]}"
# Change IFS back to original before we call any functions that might use it.
IFS=$ORIGIFS
get_sales
IFS=/
done <$ASSOC
IFS=$ORIGIFS


By making that simple change you now have an array with all employee data. Also note I changed
the read to read -a, this tells read the input is an array. Also since I changed a
variable $EMP_NO to ${EMP_DATA[0]} I would have to change it everywhere it occurs in the
program, or just EMP_NO=${EMP_DATA[0]} would take care of it.

Last edited by /bin/bash; 12-04-2007 at 09:35 AM. Reason: Fix formatting
 
Old 12-04-2007, 09:40 AM   #23
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
One other thing. You probably are not the only one in your class who knows about this forum. Your professor probably knows about it and might even be a member.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to compare two files swatward Linux - Newbie 17 04-18-2012 06:14 PM
run files using BASH script kkpal Programming 2 11-02-2007 08:21 AM
bash script, for all files in folder do ............ Simon_6162 Programming 2 07-18-2007 02:40 PM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
bash script on manipulating files fiomba Linux - Software 8 10-30-2004 08:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration