2012-05-03 29 views

回答

9

把分差的兩個文件的前k行:

$ diff <(head -k file1) <(head -k file2) 

與之相似,差異比較的最後k個行:

$ diff <(tail -k file1) <(tail -k file2) 

要DIFF線i到j:

diff <(sed -n 'i,jp' file1) <(sed -n 'i,jp' file2) 
+0

'p'在'i,jp'中表示什麼? –

+0

'p'表示打印。 – dogbane

+0

並檢查是否相等:'if diff ...;然後回聲「平等」;其他回聲「不等於」; fi' –

0

顯示兩個文件的第一行。

krithika.450> head -1 temp1.txt temp4.txt 
==> temp1.txt <== 
Starting CXC <...> R5x BCMBIN (c) AB 2012 

==> temp4.txt <== 
Starting CXC <...> R5x BCMBIN (c) AB 2012 

如果兩個文件中的第一行相等,則下面的命令顯示爲yes。相比羅布麻的上方時

krithika.451> head -1 temp4.txt temp1.txt | awk '{if(NR==2)p=$0;if(NR==5){q=$0;if(p==q)print "yes"}}' 
yes 
krithika.452> 
1

我的解決方案看起來相當的基礎和初學者,但在這裏它是一樣的!

echo "Comparing the first line from file $1 and $2 to see if they are the same." 

FILE1=`head -n 1 $1` 
FILE2=`head -n 1 $2` 

echo $FILE1 > tempfile1.txt 
echo $FILE2 > tempfile2.txt 

if diff "tempfile1.txt" "tempfile2.txt"; then 
    echo Success 
else 
    echo Fail 
fi 
1

我的解決方案使用patchutils程序收集的filterdiff程序。以下命令顯示從行號j到k的file1和file2之間的區別:

diff -U 0 file1 file2 | filterdiff --lines j-k 
相關問題