![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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 101 .... 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
|
|||
|
|||
|
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
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||||
|
|||||
|
Quote:
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:
Code:
prod='MYproducts.txt' ass='MYassociates.txt' sales='OTHERsales.txt' Quote:
Quote:
|
|||||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|