spi*_*lok 8 bash gnu-coreutils
我有两个文件A和B.我想找到A中不在B中的所有行.在bash /使用标准linux实用程序中,最快的方法是什么?这是我到目前为止所尝试的:
for line in `cat file1`
do
if [ `grep -c "^$line$" file2` -eq 0]; then
echo $line
fi
done
Run Code Online (Sandbox Code Playgroud)
它有效,但速度很慢.有更快的方法吗?
Cha*_*ffy 22
# Subtraction of file1 from file2
# (i.e., only the lines unique to file2)
comm -13 <(sort file1) <(sort file2)
Run Code Online (Sandbox Code Playgroud)
diff不太适合这个任务,因为它试图在块而不是单独的行上操作; 因此,它必须使用的算法更复杂,内存效率更低.
从SUS2(1997)开始, comm一直是Single Unix Specification的一部分.