如果出现在另一个文件中,则从文件中删除字符串

问题描述 投票:0回答:2

如果我有:

文件1

04/01/2018     a100:a101    a100:b100
02/12/1990     c100:c101    d100:d101

和文件2:

a100
c100

我如何从文件1中删除出现在文件2中的所有字符串实例

即:

04/01/2018     :a101    :b100
02/12/1990     :c101     d100:d101
bash awk sed grep tr
2个回答
0
投票

对于大文件大小,较慢的Shell解决方案。

while IFS= read -r line_in_file1 <&3; do 
  read -r line_in_file2
  printf '%s %s\n' "${line_in_file1//$line_in_file2/}" 
done 3< file1 <file2

我敢打赌awk可以做得更好,甚至可以达到目标。


0
投票

在线,两个叉子:

sed "s/\($(xargs <file2 |sed 's/ /\\|/g')\)//g" file1
© www.soinside.com 2019 - 2024. All rights reserved.