-1

file1:

G A 4
H B 3
I C 2
J D 1

file2:

M H 6
N H 5
O K 4
P J 3
Q I 2
R I 1
S G 0
T L 1

Desired:

M H 6
N H 5
P J 3
Q I 2
R I 1
S G 0

print only rows where $2 from file2 matches $1 from file1

I'm trying to construct if statement but it gives me syntax error for below

awk 'NR==FNR{a[NR]=$1; b[NR]=$2; next} ; {if{a[FNR]==b[FNR]}; {next}; { print }' file1 file2

syntax error at source line 1
 context is
    NR==FNR{a[NR]=$1; b[NR]=$2; next} ; >>>  if <<< {a[FNR]==b[FNR]; next}; { print }

or no output if don't use if verb

awk 'NR==FNR{a[NR]=$1; b[NR]=$2; next} ; {a[FNR]==b[FNR]; next}; { print }' file1 file2
6
  • 1
    Besides other problems that could have the command, I would say that your if syntax is wrong. The condition goes between parenthesis if(a[FNR]==b[FNR]) {...}. Also there's a missing bracket closure }. May 21 at 21:27
  • "print only rows where $3 from file2 matches $1 from file1" doesn't seem to make sense ("$3 from file2" are digits, whereas "$1 from file1" are all letters). Did you mean to write "$2 from file2 matches any $1 from file1"? May 21 at 21:43
  • 3
    You've asked a lot of awk questions recently, Please get the book Effective AWK Programming, 5th Edition, by Arnold Robbins to learn how to write awk scripts as you seem to be just throwing bits and pieces of various scripts together and hoping for the best without really understanding what they do or the basic syntax of the language.
    – Ed Morton
    May 22 at 0:01
  • @steeldriver apology, you're right, I made a correction.
    – Sollosa
    May 22 at 7:36
  • 2
    No need to apologize but writing if{a[FNR]==b[FNR]}; (i.e. with {...} instead of (...) and a ; to terminate the if statement before doing anything in it) has nothing to do with how many files are being processed. I'm just saying it'd be good for you to get that book to learn some of the basic syntax (though in this case it's the same syntax as C and many other languages).
    – Ed Morton
    May 22 at 12:38

2 Answers 2

3

You are over complicating your code. Try like this:

$ awk 'NR==FNR{a[$1]; next} $2 in a' file1 file2
M H 6
N H 5
P J 3
Q I 2
R I 1
S G 0

Explanation:

  1. The filter NR==FNR filters only the rows from file1, since for that file the total record number will be the same as the file record number

  2. For the lines of the first file, we add the first field ($1) as a key to array a, with a[$1]. Then we skip the rest of the commands with next

  3. Since we called next in the first block that applies to all lines of the first file, the second filter $2 in a only applies to the lines of the second file.

  4. The filter $2 in a checks if the second word $2 is a key in array a. If the key exists, then the default action is performed, which is to print the current line.

-1
#!/usr/bin/python
k=open('f1','r')
for file1 in k:
    file1_split=file1.split(' ')
    m=open('f2','r')
    for file2 in m:
        file2_split=file2.split(' ')
        if (file1_split[0].strip() == file2_split[1].strip()):
            print file2.strip()
~                              

output

M H 6
N H 5
P J 3
Q I 2
R I 1
S G 0

First it will open file1 and read line by line and it open file2 and check for each line whether file1 first line column 1 matches with any line column2 for file2 . if it matches it prints file2 matched line and it continue for further line for file1 and file2

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.