【问题标题】:remove all lines in a file containing a string from another file从另一个文件中删除包含字符串的文件中的所有行
【发布时间】:2018-11-21 02:33:24
【问题描述】:

我想根据匹配另一个文件中的字符串来删除文件的所有行。这是我用过的,但它只删除了一些:

grep -vFf to_delete.csv inputfile.csv > output.csv

以下是我的输入文件 (inputfile.csv) 中的示例行:

Ata,Aqu,Ama3,Abe,0.053475,0.025,0.1,0.11275,0.1,0.15,0.83377
Ata135,Aru2,Aba301,A29,0.055525,0.025,0.1,0.082825,0.075,0.125
Ata135,Atb,Aca,Am54,0.14695,0.1,0.2,0.05255,0.025,0.075,0.8005,
Adc,Aru7,Ama301,Agr84,0.002075,0,0.025,0.240075,0.2,0.

我的文件“to_delete.csv”如下所示:

Aqu
Aca

所以任何包含这些字符串的行都应该被删除,在这种情况下,第 1 行和第 3 行应该被删除。示例所需的输出:

Ata135,Aru2,Aba301,A29,0.055525,0.025,0.1,0.082825,0.075,0.125
Adc,Aru7,Ama301,Agr84,0.002075,0,0.025,0.240075,0.2,0.

【问题讨论】:

  • 不清楚,请在您的帖子中提及更清晰的示例,然后告诉我们?
  • 我刚刚用更多示例和所需输出示例更新了问题。我希望这很清楚?
  • 能否请您试一下我的代码并告诉我?
  • 当你尝试你的命令时,实际输出是什么?
  • @RavinderSingh13 - 基于 wc -l 它没有删除任何行。 @ Benjamin W - 它删除了一些行但不是全部。我认为它只删除了字符串首先出现的行?但是几千行就很难说了。

标签: awk grep match carriage-return


【解决方案1】:

编辑:由于 OP 在他的文件中有回车字符,所以现在也为此添加解决方案。

cat -v Input_file     ##To check if carriage returns are there or not.
tr -d '\r' < Input_file > temp_file  &&  mv temp_file Input_file

由于您的 Input_file 样本和预期输出不清楚,因此无法对其进行全面测试,请您尝试以下操作。(如果您对 awk 没问题),请在代码中附加 &gt; temp_file &amp;&amp; mv temp_file Input_file 以将输出保存到 Input_file自己。

awk -F, 'FNR==NR{a[$0];next} {for(i=1;i<=NF;i++){if($i in a){next}}} 1'  to_delete.csv  Input_file  > temp_file  && mv temp_file  Input_file

解释:现在也为上面的代码添加解释。

awk -F, '                          ##Setting field separator as comma here.
FNR==NR{                           ##checking condition FNR==NR which will be TRUE when first Input_file is being read.
  a[$0]                            ##Creating an array named a whose index is $0.
  next                             ##next will skip all further statements from here.
}
{
  for(i=1;i<=NF;i++){              ##Starting a for loop from value i=1 to till value of NF.
     if($i in a){                  ##checking if $i is present in array a if yes then go into this condition block.
       next                        ##next will skip all further statements(since we DO NOt want to print any matching contents)
     }                             ##Closing if block now.
  }                                ##Closing for block here.
}                                  ##Closing block which should be executed for 2nd Input_file here.
1                                  ##awk works on pattern and action method so making condition TRUE here and not mentioning any action so by default print of current line will happen.
'  to_delete.csv  Input_file       ##Mentioning Input_file names here now.

【讨论】:

  • @NKN,如果您的文件中有回车符,也尝试通过 cat -v Input_file 进行检查?如果是,那么您可以通过 tr -d '\r' &lt; Input_file &gt; temp_file &amp;&amp; mv temp_file Input_file 删除它们,让我知道吗?
  • 啊哈!这就是问题所在。我最初想检查一下,但我想它们不会出现在某些编辑器中。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 2013-05-10
  • 2014-04-24
  • 1970-01-01
  • 2018-06-28
相关资源
最近更新 更多