linux - "No such file or directory"在 bash 的 while 读取循环中使用 cut

标签 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

最佳答案

I would like to loop through the file line by line, and extract each column value

awk不合适吗?

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

关于linux - "No such file or directory"在 bash 的 while 读取循环中使用 cut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57116450/

相关文章:

Jquery:如何跳过第一个元素时间

asp.net - 如何将自定义文件扩展名映射到单声道?

c++ - 我无法理解的段错误错误

linux 0.01内核交叉引用

linux - 用于提取应用程序名称的 Shell 脚本

Python 子进程 .check_call 与 .check_output

php - sSMTP 不再有效 - 'Invalid response: 501 5.5.4 HELO/EHLO argument MYEMAILADDRESS@gmail.com invalid, closing connection.'

linux - bash - 将 ls 重定向到自定义脚本

javascript - 如何将查询结果推送到node.js中的数组变量?

javascript - 根据 id 比较两个数组并根据值返回一个新数组