awk saving field of first file into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk saving field of first file into array
# 1  
Old 12-10-2012
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-11-2012 at 12:02 AM.. Reason: additional info
# 2  
Old 12-11-2012
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

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 12-11-2012
Quote:
Originally Posted by rdcwayx
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
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 02:00 AM..
This User Gave Thanks to pamu For This Post:
# 5  
Old 12-11-2012
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 02:01 AM.. Reason: Additional info
# 6  
Old 12-11-2012
Quote:
Originally Posted by RozenKristal
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 Smilie
This User Gave Thanks to pamu For This Post:
# 7  
Old 12-11-2012
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 02:08 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

2. Shell Programming and Scripting

awk to print array that occurs the most with matching value in another field

In the below awk I am splitting $7 on the : and then counting each line or NM_xxxx. If the $1 value is the same for each line then print the $7 that occurs the most with the matching $1 value. The awk seems close but I am not sure what is going on. I included a description as well as to what I... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

4. Shell Programming and Scripting

How to split file into multiple files using awk based on 1 field in the file?

Good day all I need some helps, say that I have data like below, each field separated by a tab DATE NAME ADDRESS 15/7/2012 LX a.b.c 15/7/2012 LX1 a.b.c 16/7/2012 AB a.b.c 16/7/2012 AB2 a.b.c 15/7/2012 LX2 a.b.c... (2 Replies)
Discussion started by: alexyyw
2 Replies

5. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

6. Shell Programming and Scripting

Saving file content in arrays using AWK

Hi, im new to shell scripting. i have a query for which i have searched your forums but coulndt get what i need. i have a file that has two records of exactly the same length and format and they are comma seperated. i need to save the first and the second columns of the input file to 2 different... (11 Replies)
Discussion started by: atikan
11 Replies

7. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

8. Shell Programming and Scripting

saving values in file in an array in awk

hi i am trying to save values in a file in an array in awk..the file is as follows: 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, so far i have this: awk 'BEGIN {RS="\n";FS=","} { for(i=1;i<=NR;i++) { for(j=1;j<=NF;j++) { a=$j; } } (4 Replies)
Discussion started by: npatwardhan
4 Replies

9. Shell Programming and Scripting

saving values from awk expression into shell array

hi i am trying to save the values i extract from a file with the help of awk in a bash shell array. i have: exec 10<file2 while read LINE <&10; do ARRAY1=$(awk '{print $1}' file2) ((count++)) done echo ${ARRAY1} it prints just blank lines. file1 has two columns and i... (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies
Login or Register to Ask a Question