2013-07-10 27 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行 ,因此執行此操作將會過長 –

相關問題