help in awk arrays!


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help in awk arrays!
# 1  
Question 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:
Code:
Id1 pp1 0t4 pp8 xy2
Id43 009y black
Id6 red xy2
Id12 new pp1 black

I have another file :
Code:
Id pp1 black new pp8

The output should be like: (Ids of file1 and their matching elements from file2)
Code:
Id1 pp1 pp8
Id43 black
Id6 
Id12 new

looking forward for your suggestions.
Cheers!
# 2  
Code:
$ cat f1
Id1 pp1 0t4 pp8 xy2
Id43 009y black
Id6 red xy2
Id12 new pp1 black

Code:
$ cat f2
Id pp1 black new pp8

Code:
$ nawk  'NR==FNR{for(i=0;++i<=NF;){A[$i]=$i};next}{for(k=0;++k<=NF;){for(j  in A){if($k~A[j]) {x=((length(x))?x" ":x) $k;break}}};print x;x=y}' f2  f1
Id1 pp1 pp8
Id43 black
Id6
Id12 new pp1 black

---------- Post updated at 06:28 PM ---------- Previous update was at 06:08 PM ----------

nawk 'NR==FNR{when scanning the first file
for(i=0;++i<=NFSmilie{loop over all field of lines
A[$i]};store those field in an associative array
next}process next line
{for(k=0;++k<=NFSmilie{Loop over all fields of file to analyse
for(j in A){Loop over the previously built associativ array
if($k~j) {if field match a pattern of associativ array
x=((length(x))?x" ":x) $kThe field is retain to be prompted
breakat first match, go stop looping over the array
} 
}end of field checking over the array
}when all fields have been analyzed
print xprint the concatenation of thos who have been retained
x=yclear x to make it ready for scanning next line
}' f2 f1note that the file contaning the pattern is passed as first argument

could be shorten a little by removing what is in red in previous code.
Code:
nawk  'NR==FNR{for(i=0;++i<=NF;){A[$i]};next}{for(k=0;++k<=NF;){for(j in A){if($k~j) {x=((length(x))?x" ":x) $k;break}}};print x;x=y}' f2  f1


Last edited by ctsgnb; 09-24-2011 at 08:39 PM..
This User Gave Thanks to ctsgnb For This Post:
# 3  
Thanks a lot! ctsgnb for such an elaborate explaination. Its very informative for me.
But, I was wondering that the last line is printed as it is because it should be
Code:
Id12 new

otherwise.
# 4  
Why would that be? All 3 elements (new pp1 black) are in file2, no?
This User Gave Thanks to Scrutinizer For This Post:
# 5  
O! Ya! sorry, I even miss wrote in my thread and then just compared it with the same.
Thanks! again.

---------- Post updated at 08:43 PM ---------- Previous update was at 08:34 PM ----------

I have one question. How does it know that the code below is now for second file?
Code:
 
{for(k=0;++k<=NF;)

and can we deal with more than two files with awk?
# 6  
Quote:
Originally Posted by geli21
---------- Post updated at 08:43 PM ---------- Previous update was at 08:34 PM ----------

I have one question. How does it know that the code below is now for second file?
Code:
 
{for(k=0;++k<=NF;)

and can we deal with more than two files with awk?
1) This is because of the instruction "next", next skip any further code and reprocess next line from the beginning of awk commands

2) yes, awk can deal with more than 2 files, consider the following code :

Code:
$ cat f3
line1
line2
line3
line4
line5

Code:
$ nawk '{print NR,FNR,FILENAME,$0}' f3 f1 f2 f3 f2
1 1 f3 line1
2 2 f3 line2
3 3 f3 line3
4 4 f3 line4
5 5 f3 line5
6 1 f1 Id1 pp1 0t4 pp8 xy2
7 2 f1 Id43 009y black
8 3 f1 Id6 red xy2
9 4 f1 Id12 new pp1 black
10 1 f2 Id pp1 black new pp8
11 1 f3 line1
12 2 f3 line2
13 3 f3 line3
14 4 f3 line4
15 5 f3 line5
16 1 f2 Id pp1 black new pp8


Last edited by ctsgnb; 09-24-2011 at 08:59 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #203
Difficulty: Medium
Perl is not an interpreted language.
True or False?

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

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: # Input file AAA,OAA,0313... (2 Replies)
Discussion started by: maccas17
2 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. UNIX for Dummies Questions & Answers

Help to know the various possible uses of awk arrays

Hi to all, I write this time to ask for different syntax or ways of arrays within awk and uses. Since I don't know how actually could work and the real potencial of awk arrays, I look for a proactive reply/help in order to get more information than it seems I'm trying to get. I think I... (2 Replies)
Discussion started by: cgkmal
2 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

Featured Tech Videos