如何同时读取两个文件 [英] How to read from two files in the same time shell

查看:108
本文介绍了如何同时读取两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,

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

我想做的是创建另一个文件,其中仅文件A的平均得分大于或等于8.5,在B中的分数也等于或大于8.5,因此在我的示例中,C文件将包含只有Rox,因为只有她符合条件.

What i want to do is create another file with only the persons who have in file A their average greater or equal with the 8.5 and in B their mark also greater or equal to 8.5, so in my example the C file would contain only Rox because only she fulfil the criteria.

我有这个

#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文件中具有满足文件A标准的人员,但我的问题是如何将他们与filename2中的人员进行比较并从中创建结果"? 我尝试了两个while循环,但出现错误,有人可以帮忙吗? 谢谢!

so I have in the temp.txt files the persons who fulfil the criteria for file A but my question is how can i compare them with the persons from filename2 and create "results" from them ? I've tried with two while loops but i get an error, could someone please help ? Thanks!

推荐答案

如果您真的想同时读取两个文件(这似乎不是您的实际问题-join确实是您正在做的事情的正确工具),您可以在不同的FD上打开它们:

If you really want to read two files at the same time (which doesn't appear to be your actual question -- join is indeed the right tool for what you're doing), you can open them on different FDs:

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

这篇关于如何同时读取两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆