如何从另一个文件中获取一个文件的内容
假设我有一个文件“file1”,它有
a
b
c
另一个文件“file2”包含
a
b
c
d
e
f
我通常会这样做
cat file2 | grep -v a | grep -v b | grep -v c
有没有办法用文件做到这一点
grep -v file2 file1
答案1
用于diff
查找差异并sed
选择、格式化和打印仅添加的行:
diff file1 file2 | sed -n "/^>/{s/> //;p}"
添加:
如果文件的顺序不同,则sort
先排序它们两个,然后再排序diff
它们。
sort file1 > file1.s
sort file2 > file2.s
diff file1.s file2.s | sed -n "/^>/{s/> //;p}"
如果您正在使用bash
,则可以使用<(...)
流程替换:
diff <(sort file1) <(sort file2) | sed -n "/^>/{s/> //;p}"
答案2
GNU grep (我想其他人也一样) 可以采用以下-f
选项:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty
file contains zero patterns, and therefore matches
nothing. (-f is specified by POSIX.)
因此,你可以这样做:
grep -vFf file1 file2
下列的妮可·汉密尔顿根据评论中的建议,我添加了“-F”选项,这导致 grep 将其 PATTERN(在本例中为 file1 中的每一行)解释为固定字符串而不是正则表达式:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by
newlines, any of which is to be matched. (-F is specified by
POSIX.)
我也写过PERL 脚本还有更多选项:
$ list_compare.pl -h
USAGE: compare_lists.pl FILE1 FILE2
This script will compare FILE1 and FILE2, searching for the
contents of FILE1 in FILE2 (and NOT vice-versa). FILE one must
be one search pattern per line, the search pattern need only be
contained within one of the lines of FILE2.
OPTIONS:
-c : Print patterns COMMON to both files
-f : Search only the first characters (until the 1st space) of each line of
FILE2 for the search pattern given in FILE1. So, if FILE1 contains
"foo bar", only "foo" will be taken as a pattern (MUCH faster).
-d : Print duplicate entries
-m : Print patterns MISSING in FILE2 (default)
-h : Print this help and exit