The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 06:48 PM
sorting fields of a line Digby UNIX for Dummies Questions & Answers 6 03-12-2008 09:29 AM
Sorting 2 positional fields ganapati UNIX for Dummies Questions & Answers 1 02-01-2008 09:16 AM
printing select fields in awk maverix Shell Programming and Scripting 5 06-22-2007 09:25 AM
Sorting Compressed Fields ndoggy020 UNIX for Dummies Questions & Answers 1 06-05-2007 05:59 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-30-2007
ccfc1986 ccfc1986 is offline
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.
  #2 (permalink)  
Old 12-01-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,953
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 ?
  #3 (permalink)  
Old 12-01-2007
ccfc1986 ccfc1986 is offline
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
  #4 (permalink)  
Old 12-01-2007
ccfc1986 ccfc1986 is offline
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
  #5 (permalink)  
Old 12-01-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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 03:16 PM..
  #6 (permalink)  
Old 12-01-2007
ccfc1986 ccfc1986 is offline
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0