Filter column matches from two files

Viewed 48

I have two files with the format:

number|string

I want to create a third file with the contents of file 1 and without the matches between both files on the second column.

file1

1|abc
2|bcd
3|cde

file2

1|bcd
2|def

file3

1|abc
3|cde

Is this correct?

awk -F '|' 'NR==FNR{a[$2];next}$2 not in a{print $0}' file1 file2 > file3
1 Answers

You may use this awk:

awk -F '|' 'FNR == NR {skip[$2]; next} !($2 in skip)' file2 file1 > file3

cat file3

1|abc
3|cde
Related