This question already has answers here:
How to delete rows from a csv file based on a list values from another file?
(3个答案)
7天前关闭。
现在,我有两个文件,如下所示:
理想结果:
详细信息:
(3个答案)
7天前关闭。
现在,我有两个文件,如下所示:
$ cat file1.txt
john 12 65 0
Nico 3 5 1
king 9 5 2
lee 9 15 0
$ cat file2.txt
Nico
king
现在,我想删除第一行中第二个文件中包含名称的每一行。理想结果:
john 12 65 0
lee 9 15 0
谁能告诉我该怎么做?我已经尝试过这样的代码:for i in 'less file2.txt'; do sed "/$i/d" file1.txt; done
但是它不能正常工作。 最佳答案
这个工作套件awk
:
awk 'NR == FNR {a[$1]; next} !($1 in a)' file2.txt file1.txt
john 12 65 0
lee 9 15 0
详细信息:
NR == FNR { # While processing the first file
a[$1] # store the first field in an array a
next # move to next line
}
!($1 in a) # while processing the second file
# if first field doesn't exist in array a then print
关于linux - 如何删除基于另一个文件的行? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57038167/
10-12 15:33