gpt4 book ai didi

linux - 在bash中同时读取两个文件

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:16 26 4
gpt4 key购买 nike

我有两个文件。说 f1 和 f2。

Contents of f1
pointiac
cadillac
mustang

f1 和 f2 不需要有相同的行数

Contents of f2
black
blue

我的循环读取

cat f2|while read linex
do
<something to read the next line in f1, which in iteration 1 of this loop is the first line, into a variable liney>
#do something with linex and liney
#terminate when all lines in f2 run out, it doesn't matter if there are unprocessed lines in f1
done

我需要允许我在读取 f2 的行的循环中读取 f1 的下一行的代码。

我知道我的示例有点令人困惑且难以解释,但我已尽力而为。我将不胜感激任何帮助。我是 bash 的新手。

最佳答案

#! /bin/bash    
while true; do
read -r lineA <&3
read -r lineB <&4
if [ -z "$lineA" -o -z "$lineB" ]; then
break
fi
echo "1: "$lineA
echo "2: "$lineB
done 3<fileA 4<fileB

关于linux - 在bash中同时读取两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24257767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com