|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | 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 and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
awk saving field of first file into array
Hello guys, I just start trying out AWK and encounter a problem, I try to think a bit but seems my way is incorrect. I have two input file, with the first file has only one field, the second file has 3 fields, I suppose to do stuffs to them by writing an awk program, kinda sort them out. Since I am not suppose to know how many records the first file has, I cant just use a for loop. I would like to save each record of the first file into an array so I can begin to compare to second file and sort thing out. But I am not sure how, can you guys give me an idea? Sorry for my english, I am not native to the language. I did a silly attemp by doing: Code:
{ while (NR == FNR)}but it obvious give me an infinite loop, what I want to know is how can I tell that I can stop assign fields to array when it begins to read the second file? So what I desire is: File1 Code:
CoCo Hiel Euda SJHF Euda CoCo File2: Code:
CoCo $39.4 paid CoCo $34 due Euda 45 paid If there is no $, that line is invalid, also the two digits after decimal points. first field can appear several times. If first field in file 1 not appear in file 2, print out balance 0, same thing with invalid line. Desired output: Code:
CoCo balance:$5.4 Hiel balance:$0.00 Last edited by RozenKristal; 12-10-2012 at 11:02 PM.. Reason: additional info |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
How about it? Code:
awk '{sub(/\$/,"",$2)}/paid/{a[$1]+=$2}/due/{a[$1]-=$2}
END{for (i in a) printf "%s balance:$%.2f \n",i,a[i]}' infile
CoCo balance:$5.40
Euda balance:$45.00 |
| The Following User Says Thank You to rdcwayx For This Useful Post: | ||
RozenKristal (12-10-2012) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Quote:
Code:
File1: CoCo Hiel Euda SJHF File 2: CoCo $39.40 paid CoCo $34.00 due Euda 45 due CoCo $16.05 due Hiel $50 paid Euda $12.45 due Out put: CoCo balance:$-10.65 Euda balance:$-57.45 Hiel balance:$50.00 What I think my desired output is: Code:
CoCo balance: $-10.65 Euda balance: $-12.45 Hield balance: $50.00 SJHF balance: $0.00 |
|
#4
|
|||
|
|||
|
Code:
awk 'NR==FNR{sub("\\$","");s=$2;if($3 == "due"){s=0-$2};A[$1]=A[$1]?A[$1]+s:s;next}{print $0,"balance: \$",A[$0]?A[$0]:"0\.00"}' file2 file1also with printf Code:
awk 'NR==FNR{sub("\\$","");s=$2;if($3 == "due"){s=0-$2};A[$1]=A[$1]?A[$1]+s:s;next}
{printf "%s balance:$%.2f \n", $0,A[$0]?A[$0]:"0\.00"}' file2 file1
CoCo balance:$-10.65
Hiel balance:$50.00
Euda balance:$-57.45
SJHF balance:$0.00Last edited by pamu; 12-11-2012 at 01:00 AM.. |
| The Following User Says Thank You to pamu For This Useful Post: | ||
RozenKristal (12-11-2012) | ||
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Thank pamu, but the output file is like this: Code:
CoCo $39.40 paid balance: $ 0.00 CoCo $34.00 due balance: $ 0.00 Euda 45 due balance: $ 0.00 CoCo $16.05 due balance: $ 0.00 Hiel $50 paid balance: $ 0.00 Euda $12.45 due balance: $ 0.00 it would be great if paid can - balance and give out the after calculated result, is there a way to do so? With the same tested input files above, I hope to get something like this: Code:
CoCo balance: $-10.65 Euda balance: $-12.45 Hield balance: $50.00 SJHF balance: $0.00 I think what rdc did was correct, but I would need exception for when there is no $, or not 2 digits after decimal points. Last edited by RozenKristal; 12-11-2012 at 01:01 AM.. Reason: Additional info |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Quote:
file2 file1 file2 should be first. Please check my previous post.. Just added.. this ![]() |
| The Following User Says Thank You to pamu For This Useful Post: | ||
RozenKristal (12-11-2012) | ||
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Thank you sir. Oh, how I flip the code so file 1 will go before file 2? The order how it goes is pretty important for me. Also, how do I make exception when the amount column doesnt have dollars sign, or 2 digits after decimal points to be ignored? That would make Euda to have $-12.45, not $-57.45
Last edited by RozenKristal; 12-11-2012 at 01:08 AM.. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| awk assign output of array to specific field-number | sdf | Shell Programming and Scripting | 1 | 05-07-2012 04:22 PM |
| Saving file content in arrays using AWK | atikan | Shell Programming and Scripting | 11 | 02-10-2011 09:05 AM |
| saving values in file in an array in awk | npatwardhan | Shell Programming and Scripting | 4 | 01-13-2009 08:56 PM |
| saving values from awk expression into shell array | npatwardhan | Shell Programming and Scripting | 4 | 01-07-2009 07:58 PM |
| saving awk value in a bash array variable | npatwardhan | Shell Programming and Scripting | 1 | 12-17-2008 08:46 PM |
|
|