如何从文件中读取变量,每行有多个变量? [英] How to read variables from file, with multiple variables per line?

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

问题描述

我正在尝试从一个文件读取,该文件有多行,每行包含3个我想分配给变量并使用的信息.

I'm trying to read from a file, that has multiple lines, each with 3 informations I want to assign to the variables and work with.

我想出了如何在终端上简单地显示它们,但是却无法弄清楚如何将它们实际分配给变量.

I figured out, how to simply display them each on the terminal, but can't figure out how to actually assign them to variables.

while read i
do
  for j in $i
  do
    echo $j
  done
done < ./test.txt

test.txt:

1 2 3
a b c

因此,我想在外循环中读取该行,然后分配3个变量,然后使用它们,然后再转到下一行.

So I want to read the line in the outer loop, then assign the 3 variables and then work with them, before going to the next line.

我猜我必须在没有内部循环的情况下读取行的值,但是我现在无法弄清楚.

I'm guessing I have to read the values of the lines without an inside loop, but I can't figure it out right now.

希望有人可以指出我正确的方向.

Hope someone can point me in the right direction.

推荐答案

我想您要寻找的是每行读取多个变量:read命令可以自行将单词分配给变量.

I think all you're looking for is to read multiple variables per line: the read command can assign words to variables by itself.

while read -r first second third; do
    do_stuff_with "$first"
    do_stuff_with "$second"
    do_stuff_with "$third"
done < ./test.txt

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

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