2013-10-31 33 views
0

我有兩個文件,都有很多數據,我需要的是比較每個文件的第一個單詞(每個文件始終以數字開頭,每個數字可能有很多數字)。在Ubuntu中個性化區分命令

當這些數字相同時,這些文件是相同的。

例子: 我有3個文件:A.TXT,b.txt和c.txt

a.txt content is "1 a b c 3 5 6 hjkj" 
b.txt content is "1 c f a 1234 h" 
c.txt content is "2 a b c 3 5 6 hjkj" 

diff a.txt b.txt should return "files are identical" 
diff a.txt c.txt should return "files are different" 

如何使用diff命令我對它們進行比較?

+0

請更新一些示例輸入問題。當我們不必猜測時,使我們更容易提供一個很好的答案:-) –

+0

你知道這個數字有多少數字嗎? –

回答

1

看起來你只是想檢查文件匹配的第一個「字」。這個shell函數應該這樣做。

check_first_word(){ 
    read FIRST GARBAGE < $1 
    read SECOND GARBAGE < $2 
    [ "$FIRST" == "$SECOND" ] && echo "files are identical" || echo "files are different" 
} 

用法:

check_first_word file1 file2 
1

嘗試使用awk

#!/bin/bash 

awk 'FNR==1 { if(NR==1) a=$1; else b=$1 } END { if(a==b) print "files are identical"; else print "files are different" }' $1 $2 

商店上面命名mydiff文件命令,使用chmod +x給可執行權限,然後你可以按照如下

mydiff file1 file2 
1

此執行個性化差異命令應該做的工作,把這個功能您bashrc文件。

function mydiff() { 
     DIGITS=10 
     file_1=`head -c ${DIGITS} $1` 
     file_2=`head -c ${DIGITS} $2` 

     if [ "$file_1" == "$file_2" ] 
     then echo "Files are identical" 
     else 
     echo "Files are different" 
     fi 
} 

用法:

mydiff file_1 file_2 
+0

優秀!非常感謝,這就是我需要的! – acidboy

+0

如果能解決您的問題,您可以接受答案嗎?謝謝 –