bash - 如果第一列出现在另一个文件中,则从文件中删除行

标签 bash unix terminal

我必须这样的文件:

文件1:

id1 junk junk junk 

id2 junk junk junk 

id3 junk junk junk 

id4 junk junk junk

文件2:

id2

id3

如何从 File1 中删除在 File2 中具有 id 的 2 行。所以输出将是

id1 junk junk junk

id4 junk junk junk

谢谢

最佳答案

使用 awk 你可以这样做:

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

id1 junk junk junk
id4 junk junk junk

或者使用 grep -v 进行进程替换:

grep -vf <(sed 's/.*/^&[[:blank:]]/' file2) file1

id1 junk junk junk
id4 junk junk junk

关于bash - 如果第一列出现在另一个文件中,则从文件中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39214614/

相关文章:

linux - 替换匹配行中的某些位置

unix ksh 如何打印 $1 和 $2 的前 n 个字符

macos - 如何在 Mac 上恢复 .bash_profile?我的 unix 终端都没有工作

linux - 期望:从标准输入解析变量

bash - 导入shell脚本函数

json - 输出多行 bash 变量以 curl json

python - 使用 Python 完全清除终端窗口

linux - 无法回显 "for loop"内的变量值

bash - 如何使用 bash 删除和替换终端中的最后一行?

python - 我如何打开不同的 linux 终端以在 python 中输出不同种类的调试信息?