awk saving field of first file into array | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

awk saving field of first file into array

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-10-2012
RozenKristal RozenKristal is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 December 2012, 2:37 AM EST
Posts: 8
Thanks: 7
Thanked 0 Times in 0 Posts
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  
Old 12-10-2012
rdcwayx rdcwayx is offline Forum Advisor  
Use nawk in Solaris
 
Join Date: Jun 2006
Last Activity: 22 March 2014, 12:27 PM EDT
Posts: 2,759
Thanks: 44
Thanked 418 Times in 406 Posts
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  
Old 12-10-2012
RozenKristal RozenKristal is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 December 2012, 2:37 AM EST
Posts: 8
Thanks: 7
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by rdcwayx View Post
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

Almost giving me the right output, sorry for confusion, in file one, there is no repeating for the field, so it appear only one. Only in file 2 that it can repeat. And the calculation I think is a bit off since line without $ and without 2 numbers after decimal points are treated as invalid, so nothing should be done. Here what I test with the code:

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  
Old 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts

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 file1

also 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.00


Last 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  
Old 12-11-2012
RozenKristal RozenKristal is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 December 2012, 2:37 AM EST
Posts: 8
Thanks: 7
Thanked 0 Times in 0 Posts
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  
Old 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by RozenKristal View Post

Code:
CoCo balance: $-10.65
Euda balance: $-12.45
Hield balance: $50.00
SJHF balance: $0.00

please check file sequence.. it should be
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  
Old 12-11-2012
RozenKristal RozenKristal is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 December 2012, 2:37 AM EST
Posts: 8
Thanks: 7
Thanked 0 Times in 0 Posts
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 11:42 PM.