2013-03-26 52 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 

它应该是简单修改当前的脚本以这种形式来处理数据。