1

I have this sample text file text.txt that is in the form

fruits    vegetables
apples    cucumbers
oranges    squash

and it is tab delimited.

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

Below is the code the code I have tried.

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

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

My expected output should be something like:

Line
fruits
Line
apples
Line
oranges

Instead I get this output:

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
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Andrew Hamel
  • 336
  • 1
  • 13

1 Answers1

0

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

Is awk not suitable ?

awk '{ print "Line"; print $1 }' < test.txt
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440