簡體   English   中英

在bash中同時讀取兩個文件

[英]Reading from two files at the same time in bash

我有兩個文件。 說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下一行的代碼。

我了解我的示例有點令人困惑且難以解釋,但我已盡力而為。 我將不勝感激。 我是個新手。

#! /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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM