The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK - printing certain fields when field order changes in data file eric4 Shell Programming and Scripting 3 04-15-2008 03:48 PM
sorting fields of a line Digby UNIX for Dummies Questions & Answers 6 03-12-2008 06:29 AM
Sorting 2 positional fields ganapati UNIX for Dummies Questions & Answers 1 02-01-2008 06:16 AM
printing select fields in awk maverix Shell Programming and Scripting 5 06-22-2007 06:25 AM
Sorting Compressed Fields ndoggy020 UNIX for Dummies Questions & Answers 1 06-05-2007 02:59 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-30-2007
Registered User
 

Join Date: Nov 2007
Posts: 14
Sorting Files / Combing Fields / Printing

I am completely new to unix and scripting so please hang in there for me!

ey Folks. I have to write a script that will basically take input from 3 different files which contain the following:

product (file contains the following - FS=".")
-product ID (Int number)
-description (alphanumeric text)
-price (floating pt. number, with 2 sig. digits)

sales (file contains the following - FS=",")
-product ID (Int number)
- quantity (int number)
- date (in form DD:MM:YYYY)
- associate ID (int number)

associates (file contains the following - FS="/")_
- associate ID (int number)
- name (alphanumeric text)
- salary (int number)
- Position (aplhanumeric text)

I have to basically take these 3 files and use any method (would like to use awk, arrays, - the very basics if possible, not expert stuff, minmal the better - the better ill understand it).
I need to compute the saleas amount per associate for the year 2007 (not all dates are in 2007, some are 2006) and print them out listed ranked on sales totals.

I have an idea of how i would like to it by studying some notes.

Would something like this work?

awk -F/ {print $4 " " $1 " " $2 > file1} sales

(This is obviously really basic - im just trying to get my head around the best way to do this sort of a problem... - this really doesnt help me... as i would ideally like to add up ... say the bold part below:
associate ID 24 sold item #104, 5 times so that later on i could use the product file to find the product ID with its associated cost... and do something like
associate24Sales = productID * productCost

Obviously this will be more difficult that i think - or maybe not. Please if i could just get one example - i would understand it...the first step is the hardest for me!

Sales file is:

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,3,03:02:2007,24

113,92,12:02:2007,24
.... about 20 more entries (ID's range from 21-26 if that helps)
product ID, quantity, date, empoyee ID (form of data)

How can i add up the associates total of sales per each item. For examle, associate 24 above sold items 109, 104 and 113...but sold 104 to two different customers (2 items to one, 3 to the other customer)... how can you add such a thing up? The above effort should be along the right lines, but how could i set it to do multiple...

Hope this is easy to understand for someone out there. Any ideas would be awesome. I just need a kick up the rear and ill get this working. Thanks folks.
Reply With Quote
Forum Sponsor
  #2  
Old 12-01-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
How to differentiate items are sold to different customers, is there any field to differentiate that ?

Is it just to count the number of products sold by each of the representative ?

Can you post a sample input and output file ?
Reply With Quote
  #3  
Old 12-01-2007
Registered User
 

Join Date: Nov 2007
Posts: 14
Files examples

Sales:
(product ID, quantity, date, associate ID - FS = ",")
/home/lx/z109079 : cat sales
110,1,01:02:2007,22
110,2,02:02:2007,23
...
112,1,05:03:2007,23
104,9,05:03:2007,21

Associates:
(associate ID/name/salary/position - FS = "/")
/home/lx/z109079 : cat associates
21/John Doe/39000/Clerk
...
26/Dennis Miller/88000/Commedian

products:
(product ID: description: price - FS = ".")
/home/lx/z109079 : cat products
103:sway bar:49.99
101ropeller:104.99
....
111:lock:31.00
102:trailer hitch:97.95

The idea is to get something that would work out as:
Person Name Position Sales Amount
Person 1 Pos. 1 Total Sales for Person 1 (largest)
Person 2 Post. 2 Total Sales Person 2 (2nd largest)
.... ..... .....

Does this help?

Thanks guys
Reply With Quote
  #4  
Old 12-01-2007
Registered User
 

Join Date: Nov 2007
Posts: 14
Anyone have any idea how i could start with just a basic starting point. Once i get the first line of script, ill be able to understand the ideas behind it and can do the rest. There just seems to be too much information - maybe i have to search through the files, grab the stuff i need, and stsore it into a temp file... then use these temp files with the more appropriate information ... maybe sort a temp file for each employee...
so have temp.emp1
temp.emp2
etc etc
And have each of there sales in each?

How would this work? Or would it?

Thanks
Reply With Quote
  #5  
Old 12-01-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
something along these lines to start with:

Code:
#!/usr/bin/ksh

prod='products.txt'
ass='associates.txt'
sales='sales.txt'

nawk -v prod="${prod}" -v ass="${ass}" -v sales="${sales}" '
   sales == FILENAME && FNR==1 { FS=",";$0=$0 }
   ass == FILENAME && FNR==1   { FS="[/]" ;$0=$0}
   prod == FILENAME && FNR==1  { FS=":" ;$0=$0}

   prod==FILENAME  { prodA[$1] = $NF; next }
   ass==FILENAME   { assNameA[$1] = $2; assPosA[$1] = $NF; next }
   sales==FILENAME { salesA[$NF] += ($2 * prodA[$1]) }

   END {
      for( i in salesA) 
         printf("%s\t[%s] [%s] : [%s]\n", salesA[i], assNameA[i],assPosA[i], salesA[i])
   }' "${prod}" "${ass}" "${sales}" | sort -nr -k1 | cut -f2-

Last edited by vgersh99; 12-01-2007 at 12:16 PM.
Reply With Quote
  #6  
Old 12-01-2007
Registered User
 

Join Date: Nov 2007
Posts: 14
Thanks for the reply vgersh99. I was working in bash (the machine my code has to run on wont work with ksh). Thank you for your effort and help though, i see what you are getting at, and now i just need to be able to understand what some of your code means, and convert it into something bash can handle..right?

Few questions...
FILENAME - should these be changed to say... salesFile1, assFile1, prodFile1 etc?
also - what do these statements do?
FS="," ; $0=$0
the FS is the field spacer being set to ',' and thats fine, but $0 takes the entire line? because $1 is the first element and so on? Right? Am i completely off base here?

Sorry if it seems like im a complete idiot...but once i get the hang of the basics ill be good. Doesnt help working in C++ and then trying to slightly change things to run on UNIX etc. lol.

Thanks folks.
Reply With Quote
  #7  
Old 12-02-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by ccfc1986 View Post
Thanks for the reply vgersh99. I was working in bash (the machine my code has to run on wont work with ksh).
The solution should work in bash as well - just change '/usr/bin/ksh' to '/usr/bin/bash'.
Also.... on most system - if there's a 'bash' - there should be a 'ksh' as well. Check your system and the ksh location - your mileage may vary.
Quote:
Originally Posted by ccfc1986
Thank you for your effort and help though, i see what you are getting at, and now i just need to be able to understand what some of your code means, and convert it into something bash can handle..right?

Few questions...
FILENAME - should these be changed to say... salesFile1, assFile1, prodFile1 etc?
No, not for the FILENAME. If your filenames are not what I have in my script, simply change the names in shell variable definitions like so - do not change the variable names:
Code:
prod='MYproducts.txt'
ass='MYassociates.txt'
sales='OTHERsales.txt'
Quote:
Originally Posted by ccfc1986
also - what do these statements do?
FS="," ; $0=$0
These reset the FS (FieldSeparator) definitions based on the type of the file that is to be processed - all of your files have different FSs.
Quote:
Originally Posted by ccfc1986
the FS is the field spacer being set to ',' and thats fine, but $0 takes the entire line? because $1 is the first element and so on? Right? Am i completely off base here?
Quote:
'$0' reference the entire record. In your case 'line==record', which does not have to be the case. You can have records spanning multiple lines.
Either way, once you change the FS 'in-line' like what the script does, you need to 'reevaluate' the current record to make the change happen right away. You do it once for the FIRST record/line of the new file and it will hold its value for the rest of the file.
Sorry if it seems like im a complete idiot...but once i get the hang of the basics ill be good. Doesnt help working in C++ and then trying to slightly change things to run on UNIX etc. lol.

Thanks folks.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0