LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2006, 10:25 AM   #1
landuchi
Member
 
Registered: Oct 2004
Distribution: Debian, Ubuntu
Posts: 74

Rep: Reputation: 15
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 ?
 
Old 02-14-2006, 11:34 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this does it for me:
Code:
for line in `cat ./orders.txt`
anybody got a better way?

hth
 
Old 02-14-2006, 12:06 PM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,501
Blog Entries: 2

Rep: Reputation: 68
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.

Last edited by marozsas; 02-14-2006 at 12:38 PM.
 
Old 02-14-2006, 01:00 PM   #4
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
Code:
echo $ORDERS | while read line; do
    echo $line
done
should do it
 
Old 02-14-2006, 01:46 PM   #5
landuchi
Member
 
Registered: Oct 2004
Distribution: Debian, Ubuntu
Posts: 74

Original Poster
Rep: Reputation: 15
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.
 
Old 02-14-2006, 01:53 PM   #6
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
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
 
Old 02-14-2006, 01:57 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Code:
while i=$(line)
do
  echo $i
done < orders.txt
 
Old 02-14-2006, 02:05 PM   #8
landuchi
Member
 
Registered: Oct 2004
Distribution: Debian, Ubuntu
Posts: 74

Original Poster
Rep: Reputation: 15
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.
 
Old 02-14-2006, 02:08 PM   #9
landuchi
Member
 
Registered: Oct 2004
Distribution: Debian, Ubuntu
Posts: 74

Original Poster
Rep: Reputation: 15
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.

Last edited by landuchi; 02-14-2006 at 02:13 PM.
 
Old 02-14-2006, 03:00 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
Old 02-15-2006, 01:36 PM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.

Last edited by schneidz; 02-15-2006 at 01:47 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 03:49 AM
bash: read lines from a configuration script ldp Programming 2 09-23-2005 11:58 AM
BASH: First words in a line JordanH Programming 7 10-24-2004 10:00 AM
Sorting a list of words in LISP! Hady Programming 1 05-01-2004 03:29 PM
This might sound strange, but I need a big list of dirty words. Travis86 Programming 12 07-17-2003 11:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration