逐行读取两个文件并同时处理它们 [英] Read from two files line by line and process them simultaneously

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

问题描述

大家好,我是这个游戏的新手,所以我的问题可能很简单,但我在这个问题上坚持了很长时间.我想逐行同时处理两个文件列表中的两个文件.

Hello everyone I'm very new to the game so my question is probably rather simple but I'm stuck at this for a long time. I want to process two files from two list of files simultaneously line by line.

我正在尝试:

 read file1 && read file2; 
    do 
    echo "$file1 and $file2"
    done 

回声当然只是脚本其余部分的空格,但是我没有设法从读取操作中获取任何变量.

The echo is of course just a spaceholder for the rest of the script but i didn't manage to get any variables out of the read operation.

推荐答案

您需要两个单独的文件描述符,才能一次读取两个文件.其中之一可以是标准输入.

You need two separate file descriptors to read from two files at once. One of them can be standard input.

while IFS= read -r line1 && IFS= read -r line2 <&3; do
  echo "File 1: $line1"
  echo "File 2: $line2"
done < file1 3< file2

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

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