2009-12-17 35 views
1

現在我的程序生成兩個數據文件。 A.TXT和b.txt 以A.TXT爲例,它的內容就像這樣:關於計算兩個文件中相應內容之間的平均「距離」的另一個問題

0,0 
0,1 
1,0 
-3,1 
1,-2 
1,3 
...... 

b.txt是A.TXT相似。

現在,我希望找出a和b文件中相應內容之間的平均距離。 換句話說,例如,如果b.txt是這樣的:

0,0 
1,1 
1,2 
-3,1 
1,-2 
1,3 
...... 

那麼距離的計算過程是這樣:

sqrt[square(0-0)+square(0-0)] 
    +sqrt[square(0-1)+square(1-1)] 
    +sqrt[square(1-1)+square(0-2)] 
    +sqrt[square((-3)-(-3))+square(1-1)] 
    +sqrt[square(1-1)+square((-2)-(-2))] 
    ....... 
    _____________ 
    /Total number(i.e 10,000) 

獲得這兩個文件的內容之間的平均距離。

問題:如何編寫一個可以執行上述計算過程的shell腳本?並輸出最終的平均距離?

提示:您可以查看兩組座標存儲在兩個文件中。

需要你的親切幫助..很多很多謝謝。

此外: 每個文件約有10,000 - 100,000行。

+0

什麼樣的殼工具可用?例如,awk,純bash,perl,python,...? – csl 2009-12-17 16:00:21

回答

3

AWK對包含分隔數據行的文件進行簡單的數學計算真的很好。有一個很好的AWK蒞臨指導:http://www.vectorsite.net/tsawk.html

針對此程序的一般結構可能是:

Store the first row 
For each additional row, calculate the distance between it and the last row and overwrite the stored values 
Add the distance to a variable containing the distance sum 
Divide at the end by the number of rows seen (conveniently stored for you by AWK) 
Output the result 
1

這對於像perl或python這樣的腳本語言來說要容易得多。然而,在一個shell腳本,你可能會想使用:

  • cut將文件拆分
  • bc做計算
  • 無論循環結構,你在你的腳本語言偏好

如果它是作業,我已經離開了這個模糊的地方。

+0

謝謝你的迴應 – MaiTiano 2009-12-18 04:04:02

相關問題