我正在比较两个长度不同的文件,但我不想在较短的文件结束后看到较长文件的输出。
不过,我需要知道在较短的文件结束之前,这两个文件中是否有丢失的行。
我不需要使用diff,我可能会使用python来实现这一点,在python上是否有一种简单的方法来实现这一点?

最佳答案

看看这个解决方案是否适合您:

dhruvpathak@dhruvpathak:~$ cat shortfile
this is a
short file
created
for example
dhruvpathak@dhruvpathak:~$ cat longfile
this is a
long file
created
for example.
but also contains
some extra text
which needs to be
ignored when
the small file
ends.

dhruvpathak@dhruvpathak:~$ cat shortfile > /tmp/a && echo "*****ENDMARKER*****" >> /tmp/a && cat longfile > /tmp/b && diff /tmp/a /tmp/b | grep -B 100000 "*****ENDMARKER*****"
2c2
< short file
---
> long file
4,5c4,10
< for example
< *****ENDMARKER*****

10-08 08:31