从另一个文件中删除包含字符串的文件中的所有行 [英] remove all lines in a file containing a string from another file
问题描述
我想基于匹配另一个文件中的字符串来删除文件的所有行.这是我使用过的内容,但只删除了一些内容:
I'd like to remove all the lines of a file based on matching a string from another file. This is what I have used but it only deletes some:
grep -vFf to_delete.csv inputfile.csv > output.csv
这是我的输入文件(inputfile.csv)中的示例行:
Here are sample lines from my input file (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"如下所示:
My file "to_delete.csv" looks like this for example:
Aqu
Aca
因此,具有这些字符串的任何行都应删除,在这种情况下,第1行和第3行应删除.所需输出样本:
So any line with those strings should get deleted, in this case, lines 1 and 3 should get deleted. Sample desired output:
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.
推荐答案
由于OP在他的文件中包含回车符,所以现在也为此添加解决方案./p>
Since OP had carriage characters in his files so adding solution for that too now.
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
没问题),请附加>临时文件&&代码中的mv temp_file Input_file
可以将输出保存到Input_file本身.
Since your samples of Input_file and expected output is not clear so couldn't fully test it, could you please try following.(if you are ok with awk
), append > temp_file && mv temp_file Input_file
in code to save output into Input_file itself.
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
说明: 现在也为上述代码添加了说明.
Explanation: Adding explanation for above code too now.
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.
这篇关于从另一个文件中删除包含字符串的文件中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!