在bash中使用while读取循环切入“没有这样的文件或目录”

时间:2019-07-19 16:32:24

标签: linux bash loops

我有这个示例文本文件text.txt,格式为

fruits    vegetables
apples    cucumbers
oranges    squash

它是制表符分隔的。

我想逐行遍历文件,并提取每个列的值。

下面是我尝试过的代码。

while read p
do
  echo "Line"
  fruit="$(cut -f 1 $p)"

  echo "${fruit}"
done <test.txt

我的预期输出应为:

Line
fruits
Line
apples
Line
oranges

相反,我得到以下输出:

Line
cut: fruits: No such file or directory
cut: vegetables: No such file or directory

Line
cut: apples: No such file or directory
cut: cucumbers: No such file or directory

Line
cut: oranges: No such file or directory
cut: squash: No such file or directory

1 个答案:

答案 0 :(得分:0)

  

我想逐行循环浏览文件,并提取每个文件   列值

awk不适合吗?

awk '{ print "Line"; print $1 }' < test.txt