If I have two files. File A looks like:
a 1
a 2
a 3
b 4
c 5
and I have file B which has content:
a
b
For everything that appears in file B and also appears in column 1 in file A, I would like to remove those lines. So the expected output for file A should be:
c 5
Any help is greatly appreciated!
GNU Awk:
When processing the first file (
ARGIND
is 1), enter$0
(each entire line) into an associative arraydel
by incrementing its entry.When processing the second file, print if the first field
$1
is not associated with a nonzero count indel
.Of course, we make
B
the first file andA
second.(The printing action is implicit when the
ARGIND == 2 && !del[$1]
pattern expression yields a Boolean true. A pattern without an action has an implict action equivalent to{ print }
).ARGIND
is not in POSIX. In portable Awk code, an ugly hack may be used to distinguish the first file from the second:When the first file is processed, the "file record number" (record number in the current file) is equal to the "total record number" (absolute record number processed across all files). Of course, this breaks if the first file contains no records at all. See What is "NR==FNR" in awk?
The following will do the work,