Using arrays with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using arrays with awk
# 1  
Old 08-23-2012
Using arrays with awk

I'm a little stuck and would be grateful of some advice!

I have three files, two of which contain reference data that I want to add to a line of output in the third file. I can't seem to get awk to print array contents as I would expect.

The input files are:

Code:
# Input file
AAA,OAA,0313
AAB,OAB,0314

# Ref file1
NAA,123
NAB,456

# Ref file2
0313,123
081A,456
0314,789

I need to match a line in Ref File2 based on the last field of the main input file. I then need to match the last field in Ref File2 to Ref File1.
The output should then be:

Code:
AAA,OAA,0313,123,NAA
AAB,OAB,0314,456,NAB

Currently my code doesn't quite work as the last field of my output is empty. I have tried numerous field numbers and $NF for the f1 array, but can't seem to get any output. My code is:

Code:
FILE1=reffile1
FILE2=reffile2
FILE3=inputfile
awk -F"," -v "file1=$FILE1" -v "file2=$FILE2" '
FILENAME == file1 {
        f1[$2] = $0
        next
}
FILENAME == file2 {
        f2[$1] = $2
        next
}
{
        if ( $NF in f2 ) {
                if ( f2[$NF] in f1 ) {   ## I'm not sure why $NF works here and $2 does not, but I seem to get the output I want..
                        print $0","f2[$NF]","f1[$1]  # This line does not produce the relevant output from the f1 array (I want field 1 to print)
                }
        }
}' $FILE1 $FILE2 $FILE3

Can anyone suggest why this is not working and how the above can be modified to work. I seem to be 95% of the way there, I just would like to understand why this is not working and how output is taken from an array.

Thanks.
# 2  
Old 08-23-2012
awk -F, -v file1=mac1.txt -v file2=mac2.txt -f mac.awk OFS =, mac1.txt mac2.txt macI.txt
mac.awk:
Code:
FILENAME==file1 { f1[$2]=$1;next}
FILENAME==file2 { f2[$1]=$2;next}
$NF in f2 && f2[$NF] in f1 { $0=$0 OFS f2[$NF] OFS f1[f2[$NF]]}
1

# 3  
Old 08-23-2012
Thanks for the response.

I forgot to mention I did also try f2[$2]=$1 and it still didn't work.

I'm not able to test this out at the moment and I sort of follow your suggestion:

Code:
$NF in f2 && f2[$NF] in f1 { $0=$0 OFS f2[$NF] OFS f1[f2[$NF]]}
1

Would you (or anyone else) mind commenting on why the above works and what I had written didn't, so I can understand where I was going wrong?
Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Arrays

So I'm back once again beating my head off a wall trying to figure out how to get this to work. My end goal is to take input such as what's below, which will be capture in real time with a tail -f from a file or piped output from another command: ... (5 Replies)
Discussion started by: ShadowBlade72
5 Replies

2. Shell Programming and Scripting

help in awk arrays!

Hi, buddies I am new to shell scripting and trying to solve a problem. I read about arrays in awk that they are quite powerful and are associative in nature. Awk Gurus Please help! I have a file: Id1 pp1 0t4 pp8 xy2 Id43 009y black Id6 red xy2 Id12 new pp1 black I have... (5 Replies)
Discussion started by: geli21
5 Replies

3. UNIX for Dummies Questions & Answers

awk arrays

Hi Can someone please explain the logic of awk arrays. I have been doing some reading but I dont understand this: #!/usr/bin/gawk -f { arr++; } end { for(i in arr) { print arr,i } } As I understand arr refs the arrays index, so while $2 is a string that cant... (2 Replies)
Discussion started by: chronics
2 Replies

4. Shell Programming and Scripting

arrays in awk

Hi, I have the following data in a file for example: Name="Fred","Bob","Peterson","Susan","Weseley" Age="24","30","28","23","45" Study="English","Engineering","Physics","Maths","Psychology" Code="0","0","1","1","0" Name="Fred2","Bob2","Peterson2","Susan2","Weseley2"... (14 Replies)
Discussion started by: james2009
14 Replies

5. Shell Programming and Scripting

awk arrays can do this better - but how?

Hi, I have spent the afternoon trawling Google, Unix.com and Unix in a Nutshell for information on how awk arrays work, and I'm not really getting too far. I ahve a batch of code that I am pretty sure can be better managed using awk, but I'm not sure how to use awk arrays to do what I'm... (1 Reply)
Discussion started by: littleIdiot
1 Replies

6. Shell Programming and Scripting

Arrays in awk

Hi, I've written the following code to manipulate the first 40 lines of a data file into my desired order: #!/bin/awk -f { if (NR<=(4)){ a=a$0" "} else { if ((NR >= (5)) && (NR <= (13))) { b=b$0" " } else {if ((NR >= (14)) && (NR <= (25))){ c=c$0" "} ... (5 Replies)
Discussion started by: catwoman
5 Replies

7. Shell Programming and Scripting

Need Help with awk and arrays

now its owkring - thanks fo rthe help all . (7 Replies)
Discussion started by: fusionX
7 Replies

8. Shell Programming and Scripting

awk arrays

Guys, OK so i have been trying figure this all all day, i guess its a pretty easy way to do it. Right, so i have to column of data which i have gotten from one huge piece of data. What i would like to do is to put both of these into one array using awk. Is this possible?? If so could... (1 Reply)
Discussion started by: imonthejazz
1 Replies

9. Shell Programming and Scripting

arrays in awk???

Been struggling with a problem, I have been trying to do this in awk, but am unable to figure this out, I think arrays have to be used, but unsure how to accomplish this. I have a input file that looks like this: 141;ny;y;g 789;ct;e;e 23;ny;n;u 45;nj;e;u 216;ny;y;u 7;ny;e;e 1456;ny;e;g... (3 Replies)
Discussion started by: craigsky
3 Replies

10. UNIX for Advanced & Expert Users

Two or more arrays in Awk

Hi All, I have been working on awk and arrays. I have this small script: cat maillog*|awk -F: '$2=="SMTP-Accept" && $5~/string/ {lastdate=substr($1,1,8); internaluser=$5; v++} END {for (j in v) {print lastdate, v, j}'| sort>> mail.list This gives me the number of mails users are getting. ... (1 Reply)
Discussion started by: nitin
1 Replies
Login or Register to Ask a Question