2013-07-10 24 views
0

我比较了两个文件的长度不同,但我不想在更短的文件结束后看到较长文件的输出。 虽然我确实需要知道在较短文件结束之前是否有任何文件中有缺少的行。如何在一个文件结束后停止diff?

我不必使用差异,我可能会使用python来做到这一点,有没有一种简单的方法来做同样的python?

+0

如何使用'WC -l'获得的行数,并传递给'头-n'只输出这么多线。然后管道,以“差异”。 –

+0

只有当差异出现在第n行之前时才会起作用,但并不总能保证 – pocketfullofcheese

+0

您能详细解释一下您需要解决此问题的上下文吗? – pocketfullofcheese

回答

1

看是否有此解决方案适用于您:

[email protected]:~$ cat shortfile 
this is a 
short file 
created 
for example 
[email protected]:~$ 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. 

[email protected]:~$ 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***** 
+0

thanx,但这对于短文件很有用,我的文件可以达到原始文件的最高20M行 ,因此执行此操作将会过长 –

相关问题