2012-05-03 25 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 
相关问题