我有2个文件; file1
和file2
。 File1有许多行/行和列。 File2只有一列,有几行/行。 file2
中的所有字符串都可以在file1
中找到。我想创建一个新文件(file3
),以便删除包含file1
中任意字符串的file2
中的行。
例如,
File1中:
Sally ate 083 popcorn
Rick has 241 cars
John won 505 dollars
Bruce knows 121 people
文件2:
083
121
所需文件3:
Rick has 241 cars
John won 505 dollars
请注意,我不想手动将文件2中的字符串输入到命令中(实际文件比示例中的大得多)。
谢谢!
答案 0 :(得分:1)
试试这个 -
#cat f1
Sally ate 083 popcorn
Rick has 241 cars
John won 505 dollars
Bruce knows 121 people
#cat f2
083
121
#grep -vwf f2 f1
Rick has 241 cars
John won 505 dollars
答案 1 :(得分:1)
grep
比行编辑器更适合您的目的
grep -v -f File2 File1 >File3
答案 2 :(得分:1)
awk 方法:
awk 'BEGIN{p=""}FNR==NR{if(!/^$/){p=p$0"|"} next} $0!~substr(p, 1, length(p)-1)' file2 file1 > file3
p=""
将变量视为包含file2
FNR==NR
确保为第一个输入文件执行下一个表达式,即file2
if(!/^$/){p=p$0"|"}
表示:如果它不是空行!/^$/
(因为它可以根据您的输入)将模式部分与|
连接起来,所以它最终看起来像{{1} }
083|121|
- 检查第二个输入文件($0!~substr(p, 1, length(p)-1)
)中的一行是否与模式(即file1
列值不匹配)
file2
内容:
file3