2013-03-26 31 views
1

我有兩個文件,如何從兩個文件在同一時間閱讀shell

A 

john 1 2 3 4 5 6 7 
Ely 10 9 9 9 9 9 9 
Maria 3 5 7 9 2 1 4 
Rox 10 10 10 10 10 10 10 

B 
john 7.5 
Ely 4.5 
Maria 3,7 
Rox 8.5 

我想要做的就是創建另一個文件,只有誰在文件中的他們的平均大於或等於與人8.5和B中它們的標記也大於或等於8.5,所以在我的示例中,C文件將僅包含Rox,因爲只有她滿足標準。

我有這個

#shell program 
echo "Fiserul are numele $1" 
filename=$1 
filename2=$2 
echo "">temp.txt 
touch results 
compara="8.5" 
cat $filename | while read -r line 
do 
    nota=0 
    media=0 
    echo " $line" 
    rem=$(echo "$line"| cut -f 2- -d ' ') 
    for word in $rem 
    do 
     echo "$word" 
     nota=$(($nota+$word)) 
     echo "Nota=$nota" 
    done 
    media=$(($nota/7)) 
    if [ "$(echo $media '>=' $compara | bc -l)" -eq 1 ]; 
    then 
     nume=$(echo "$line"| cut -f 1 -d ' ') 
     echo "$nume $media" >> temp.txt 
    fi 
    echo "Media : $media" 
done 
cat $filename2 | while read -r line 
do 

,所以我必須在TEMP.TXT文件誰履行文件中的標準的人,但我的問題是如何通過用文件名2的人比較,並創建「結果「 從他們 ? 我試過兩個while循環,但我得到一個錯誤,有人可以幫忙嗎? 謝謝!

+0

在awk中這樣做會容易得多,但我沒有時間爲你勾畫出來。祝你好運。 – shellter 2013-03-26 17:38:59

+0

嘿,謝謝你的提示! – JackRobinson 2013-03-26 17:56:08

+0

@shellter - 史上最差的答案!恭喜! 問:「我怎麼騎自行車到芝加哥?」 A:「好吧,因爲我有一輛非常好的汽車,我會很容易把你開到那裏,我也很聰明,但是抱歉,我沒有時間,祝你好運!」 – user3133172 2015-08-12 08:08:34

回答

2

如果你真的同時讀取兩個文件(這並不似乎是您的實際問題 - join確實是你在做什麼合適的工具),你可以打開它們不同的FD:

while read -r -u 4 line1 && read -r -u 5 line2; do 
    echo "Line from first file: $line1" 
    echo "Line from second file: $line2" 
done 4<file1 5<file2 
1

使用join命令A和B合併成一個文件C:

$ join A.txt B.txt 
john 1 2 3 4 5 6 7 7.5 
Ely 10 9 9 9 9 9 9 4.5 
Maria 3 5 7 9 2 1 4 3,7 
Rox 10 10 10 10 10 10 10 8.5 

它應該是簡單修改當前的腳本以這種形式來處理數據。