-1

Let's say below is a table data is stored in a variable called "data"

1 apple 50 Mary
2 banana 40 Lily
3 orange 34 Jack

# 

for i in ${data}
do
  echo $i

done


# Expected Output
1 apple 50 Mary
2 banana 40 Lily
3 orange 34 Jack

How do I convert the above table of data into three entires so that i can iterate over it using a for-loop and print that single entry in exact same format.

Any suggestion.

devops-admin
  • 1,447
  • 1
  • 15
  • 26
  • What shell are you using? Is ``table data`` a file? And why didn't you store your ``table data`` in an array? Check out this link [Read lines from a file into a Bash array](https://stackoverflow.com/questions/11393817/read-lines-from-a-file-into-a-bash-array) – Darkman Feb 01 '21 at 05:16

1 Answers1

0

Thanks, @Darkman for this "https://stackoverflow.com/questions/11393817/read-lines-from-a-file-into-a-bash-array" link, it helps in my cause.

# This below line help
# IFS=$'\r\n' GLOBIGNORE='*'

data="1 apple 50 Mary
2 banana 40 Lily
3 orange 34 Jack"

IFS=$'\r\n' GLOBIGNORE='*'
for i in ${data}
do
  echo $i
  echo "test" 
done

# Resulted Output
1 apple 50 Mary
test
2 banana 40 Lily
test
3 orange 34 Jack
test

devops-admin
  • 1,447
  • 1
  • 15
  • 26