LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash - How to make For read lines instead of words in a list ? (https://www.linuxquestions.org/questions/programming-9/bash-how-to-make-for-read-lines-instead-of-words-in-a-list-415325/)

landuchi 02-14-2006 10:25 AM

Bash - How to make For read lines instead of words in a list ?
 
First of all, forgive me for i am noob :) , i searched the forum, but found no answers.

orders.txt contains the following

Code:

one two three
this is a word

And lines.sh contains the following

Code:

#!/bin/bash

ORDERS=`cat ./orders.txt´

for ITEM in $ORDERS ; do
echo $ITEM
done

This will print

one
two
three
this
is
a
word

I need it to print

one two three
this is a word


I know i can achieve this by replacing spaces by _ or any other character, but is there other way to do it ?

schneidz 02-14-2006 11:34 AM

this does it for me:
Code:

for line in `cat ./orders.txt`
anybody got a better way?

hth

marozsas 02-14-2006 12:06 PM

The perl solution is trivial, if it this helps for anything.

while (<>) {
chomp;
print "line: $line\n";
}

I have spend some time on your question and I can not figure out how to do that in pure bash.

I would like to know a solution, if any exists.

phil.d.g 02-14-2006 01:00 PM

Code:

echo $ORDERS | while read line; do
    echo $line
done

should do it

landuchi 02-14-2006 01:46 PM

Thanks for the replies, i tested both ideas,

schneidz's solution gives the same result i got...

one
two
three
this
is
a
word


phil.d.g's gives as a result all the words from the text in the same line:

one two three this is a word

So far i have 3 solutions, but there must be an easier way to show each line separatedly.

Use the tail , head trick
Put a number for each line in the file and use grep
Use lines without spaces.

phil.d.g 02-14-2006 01:53 PM

Oh, seems the line endings are lost when you do
Code:

ORDERS=`cat orders.txt`
if you do
Code:

cat orders.txt | while read line; do
    echo $line
done

that works

jlliagre 02-14-2006 01:57 PM

Code:

while i=$(line)
do
  echo $i
done < orders.txt


landuchi 02-14-2006 02:05 PM

The idea behind all this is a script which reads instructions from a formated text file. Upon reading each line it will know if the task has been completed, or not and if i has to be executed now. It will then write the file if the task
was executed.

This script will be checking the instructions file all the time, so the code should be as simple as posible.

landuchi 02-14-2006 02:08 PM

It works great now, thank you very much!

I think there is a way to tell "for" with a special parameter, to read lines intead of words, i was reading that in the bash manual, but i couldn't understand how.

jlliagre 02-14-2006 03:00 PM

Quote:

Originally Posted by landuchi
I think there is a way to tell "for" with a special parameter, to read lines intead of words, i was reading that in the bash manual, but i couldn't understand how.

I think you are thinking about IFS:

Code:

IFS="$(echo)"                       
for i in $(<orders.txt)
do
  echo $i
done


schneidz 02-15-2006 01:36 PM

Quote:

Originally Posted by landuchi
Thanks for the replies, i tested both ideas,

schneidz's solution gives the same result i got...

one
two
three
this
is
a
word


...

yeah your right i tried it doesn't work,

glad you got it regardless.


All times are GMT -5. The time now is 09:49 PM.