BASH - remove line if first column content appears in another file

旧街凉风 提交于 2019-12-13 08:26:29

问题


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!


回答1:


GNU Awk:

awk 'ARGIND == 1 { del[$0]++ } ARGIND == 2 && !del[$1]' B A

When processing the first file (ARGIND is 1), enter $0 (each entire line) into an associative array del by incrementing its entry.

When processing the second file, print if the first field $1 is not associated with a nonzero count in del.

Of course, we make B the first file and A 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:

awk 'FNR == NR { del[$0]++ } FNR < NR && !del[$1]' B A

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?




回答2:


The following will do the work,

awk 'FNR==NR{map[$1]=1;next;}map[$1]==""{print;}' <fileB> <fileA>


来源:https://stackoverflow.com/questions/39302124/bash-remove-line-if-first-column-content-appears-in-another-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!