Vishal Pitrubhakt
Vishal Pitrubhakt

Reputation: 11

Compare two column in two different files using awk

I have two files as given below

file1

1|a
2|b
3|c
4|d
5|e
6|g
7|h
8|i

file2

2
3

expected output using awk in linux

1|a
4|d
5|e
6|g
7|h
8|i

Tried below but not getting expected op

awk '{k=$1 FS $2} NR!=FNR{a[k]; next} !(k in a)' file1 file2

expected output using awk in linux

1|a
4|d
5|e
6|g
7|h
8|i

Upvotes: 1

Views: 100

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133750

We have to set different FS for both Input_file(s) since once Input_file is PIPE(|) delimited and other is not, could you please try following. This will only print those lines which are NOT in Input_file2 and present in Input_file1.

awk 'FNR==NR{a[$0];next} !($1 in a)'  Input_file2  FS="|"  Input_file1

Output will be as follows.

1|a
4|d
5|e
6|g
7|h
8|i


Explanation: Adding explanation for above command.

awk '                                       ##Starting awk program from here.
FNR==NR{                                    ##Checking condition FNR==NR which will be TRUE when Input_file2 is being read.
  a[$0]                                     ##Creating an array named a whose index is $0 here.
  next                                      ##next function is awk out of the box function which skips all further statements from here onward.
}                                           ##Closing BLOCK for FNR==NR condition here.
!($1 in a)                                  ##Checking condition if $1 is NOT present in Input_file1, this condition will be executed when Input_file named Input_file1 is being read.
'  Input_file2  FS="|"  Input_file1         ##Mentioning Input_file2 name then setting FS as pipe and mentioning Input_file1 here.

Upvotes: 2

Related Questions