Compare strings between 2 arrays and print number in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare strings between 2 arrays and print number in AWK
# 1  
Old 10-22-2011
Compare strings between 2 arrays and print number in AWK

Hi to everyone,

Please some help over here.

Hi have array a with 6 elements and array b with 3 elements as shown inside BEGIN{} statement.

I need help to get the correct sintax (the part in red) to compare if string from array b is in array a and print the number
related for each match.
Code:
awk 'BEGIN{
    a[1]="4|Mike F"
    a[2]="7|John M"
    a[3]="3|Carl L"
    a[4]="8|Mike F"
    a[5]="5|John M"
    a[6]="9|Carl L"
    
    b[1]="Carl L"
    b[2]="John M"
    b[3]="Mike F"}
{    
for ( j=1; j<=length(b); j++ ) { 
    
        if(a[j] ~ b[j]) {  # this is only the idea, I don't know the correct syntax
            print Number     
        }    
    }        
}'

E.g. If b[1]="Carl L" is in array a, then print
3
9

for b[2]="John M" print
7
5

and for b[3]="Mike F" print
4
8

I need to do this in awk because the loop will be part of a main awk code. In this sample only 2 occurences of b are in a,
but b values could be many times in a, so I need to print all matches for each value in b.

Many thanks in advance,

Grettings
# 2  
Old 10-22-2011
Try this...

Code:
awk 'BEGIN{
    a[1]="4|Mike F"
    a[2]="7|John M"
    a[3]="3|Carl L"
    a[4]="8|Mike F"
    a[5]="5|John M"
    a[6]="9|Carl L"
    b[1]="Carl L"
    b[2]="John M"
    b[3]="Mike F"
}
{
  for(j in b){
    for(i=1;i<=length(a);i++){
      if(b[j] ~ a[i]){
        split(a[i],arr,"|")
        print arr[1]
      }
    }
  }
}'

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 10-22-2011
Hi again ahamed,

Thanks for all your help. Learning from you. As usual it works!

Many thanks.

Grettings
# 4  
Old 10-23-2011
You are welcome! Smilie

--ahamed
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 sum of 2 arrays and compare

i'm new to awk, and i've been searching on the forum for sum of a column but all the scripts does sum a column of an entire file. I've a file like this: cat file.txt 1234 5678 5678 1234 I want to use awk to do sum of each column per line not entire file, compare the two then write the... (1 Reply)
Discussion started by: chofred
1 Replies

2. Shell Programming and Scripting

Compare multiple arrays elements using awk

I need your help to discover missing elements for each box. In theory each box should have 4 items: ITEM01, ITEM02, ITEM08, and ITEM10. Some boxes either have a missing item (BOX02 ITEM08) or might have da duplicate item (BOX03 ITEM02) and missing another one (BOX03 ITEM01). file01.txt ... (2 Replies)
Discussion started by: alex2005
2 Replies

3. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

4. Shell Programming and Scripting

Problem facing to compare different column and print out record with smallest number

Hi, Input file 1 : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Desired Output file 1 : 37170 37196 77 51 Input file 2 : 37174 37195 73 0 37170 37196 77 0 Desired Output file 2 : 37174 37195 73 0 (1 Reply)
Discussion started by: cpp_beginner
1 Replies

5. UNIX for Dummies Questions & Answers

awk: don't print sub-arrays

Hi ! I have this input: 12{11}{11110}{80}3456 {123}15{60}9876{8083}34 I try to work on individual numbers between braces. 3 possible cases (here I used colours to be clearer only): - there is no "0" among the characters between braces: so we don't touch anything. - there is a "0" among... (4 Replies)
Discussion started by: beca123456
4 Replies

6. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

7. Shell Programming and Scripting

awk arrays - compare value in second column to variable

Hello, I am trying to redirect files to a directory by using a config file. The config files is as such: xxxxxx,ID,PathToDirectory xxxxxx,ID2,PathToDirectory2 and so on... I have a variable that should match one of these IDs. I want to load this config file into an awk array, and... (2 Replies)
Discussion started by: jrfiol
2 Replies

8. UNIX for Dummies Questions & Answers

Print number of occurrences of many different strings

People, I need your help with making a script which will 1. take as an input the number of lines, smth like this: ((RUBROBACTER_1_PE1288 (((SALINISPORA_1_PE1863 SALINISPORA_1_PE1828)100 ((NOCARDIOIDES_2_PE2419 PROPIONIBACTERIUM_1_PE1395)96 ((((((((CORYNEBACTERIUM_1_PE1119... (3 Replies)
Discussion started by: roussine
3 Replies

9. Shell Programming and Scripting

Compare two strings, and print lines containing mismatches

pls help me on this... and im really sorry because i really don't know where to start here... FILE1 ABC DEF 10 2 DEF GHI 11 3 GHI JKL 12 5 JKL MNO 13 7 MNO PQR 14 5 requirements: 1. The third string should only be 10 or 12 2. The fourth string should only be 2 or 3 3. Prinnt... (1 Reply)
Discussion started by: kingpeejay
1 Replies

10. Shell Programming and Scripting

How to print only lines in between two strings using awk

Hi, I want to print only lines in between two strings and not the strings using awk. Eg: OUTPUT top 2 bottom 1 left 0 right 0 page 66 END I want to print into a new file only top 2 bottom 1 left 0... (4 Replies)
Discussion started by: jisha
4 Replies
Login or Register to Ask a Question