LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 10-07-2004, 08:44 PM   #1
clinton
Member
 
Registered: Oct 2003
Location: Vancouver
Distribution: RH Enterprise AS 3
Posts: 47

Rep: Reputation: 15
Question BASH: Read entire file line in for loop


Hi there,

Does anyone know of a way to read an entire file line as an argument to a for loop?

For example: we have my.file:
My name is boo.
I like to foo.
Isn't that coo?

Then, the BASH script:
[code]
for entry in $(cat my.file)
do
....
done
[end code]

What I want is for each _line_ of the file to be $entry. That is:
$entry = My name is boo.
$entry = I like to foo.
$entry = Isn't that coo?

But, in the code above, $entry will be each word. That is:
$entry = My
$entry = name
$entry = is
...
etc.


I have tried using double quotes aroung the my.file or the entire command substitution, but nothing seems to work.

Any ideas?

Thanks very much for your help
 
Old 10-07-2004, 09:04 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
IFS=$'\n'



Cheers,
Tink
 
1 members found this post helpful.
Old 10-08-2004, 05:07 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Re: BASH: Read entire file line in for loop

Quote:
Originally posted by clinton
Does anyone know of a way to read an entire file line as an argument to a for loop?
Instead of the "IFS-way" above, I think it's better to use a while loop. Because changing IFS has side-effects you may have to take care of (depending on the rest of your script).
Code:
#!/bin/bash

N=0
cat my.file | while read LINE ; do
	N=$((N+1))
	echo "Line $N = $LINE"
done
or:

Code:
#!/bin/bash

N=0
while read LINE ; do
	N=$((N+1))
	echo "Line $N = $LINE"
done < my.file
P.S. Instead of [ end code ] use [ /code ]
(without the spaces)

Last edited by Hko; 10-08-2004 at 05:11 AM.
 
1 members found this post helpful.
Old 10-08-2004, 12:32 PM   #4
clinton
Member
 
Registered: Oct 2003
Location: Vancouver
Distribution: RH Enterprise AS 3
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks you two, for your help.

I used Hko's suggestion: works like a charm!

As to Tinkster's, this may be a foolish question, but were you suggesting I change $IFS for cat? I wasn't aware that cat had such a variable.

If $IFS is an environment variable, I don't have that set in my environment. Perhaps it is known as something else.....?

I'm using RH Enterprise 3, btw.

Thanks again for your help!
 
Old 10-08-2004, 01:16 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by clinton
If $IFS is an environment variable, I don't have that set in my environment. Perhaps it is known as something else.....?
$IFS is a special environment variable, and you do have it set, because it alwasy exists. You only (almost) don't see it, because by default is only contains a line-feed.

It think Tinkster means something like this:
Code:
#!/bin/bash

IFS=$'\n'
set $(cat my.file)

# Now the lines are stored in $1, $2, $3, ...

echo $1
echo $2
echo $3
echo $4
 
Old 10-08-2004, 01:48 PM   #6
clinton
Member
 
Registered: Oct 2003
Location: Vancouver
Distribution: RH Enterprise AS 3
Posts: 47

Original Poster
Rep: Reputation: 15
Of course I couldn't see a newline.... yeesh

Brain fart on my part there.

Thanks
 
Old 07-22-2006, 04:31 PM   #7
Dragineez
Member
 
Registered: Oct 2005
Location: Annapolis
Distribution: Ubuntu
Posts: 275

Rep: Reputation: 32
Thanx!

Yet another example of how a thread from the dead can help with a current problem.
 
Old 07-27-2006, 02:58 AM   #8
SeekingWisdom
LQ Newbie
 
Registered: Feb 2006
Distribution: Slackware 10.2
Posts: 8

Rep: Reputation: 0
I would've done it Tkinsters way:

OLDIFS="$IFS"
IFS=
for entry in `cat foo`; do [ code here ]; done
 
Old 07-27-2006, 03:01 AM   #9
SeekingWisdom
LQ Newbie
 
Registered: Feb 2006
Distribution: Slackware 10.2
Posts: 8

Rep: Reputation: 0
I would've done it Tkinsters way:
Code:
OLDIFS="$IFS"
IFS=
for entry in `cat foo`; do [ code here ]; done
IFS="$OLDIFS"
You can save IFS, and later change it back if necessary.
Quote:
Originally Posted by Hko
You only (almost) don't see it, because by default is only contains a line-feed.
And a space and a tab, if memory serves.
 
Old 05-25-2010, 12:13 PM   #10
ifeatu
Member
 
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68

Rep: Reputation: 15
Syntax Help

I'm trying to read a file in using the syntax above to read in a list of users and create a user.bat for them for some reason this doesn't seem to work:

Code:
#!/bin/bash

FS=$'\n'
set $(cat users.txt)

for i in $FS; do

`cp logon.bat ./$1.bat`


# Now the lines are stored in $1, $2, $3, ...

echo $1
echo $2
echo $3
echo $4
done
Can anyone help?
 
Old 05-25-2010, 12:29 PM   #11
ifeatu
Member
 
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68

Rep: Reputation: 15
Update...

Okay I've made some changes to the file and now it only prints the last line of the file, its not iterating thru the loop:

Code:
#!/bin/bash
i=0;
FS=$'\n'
set $(cat users.txt)

for i in $_; do

#cp ~/scripts/logon.bat ./$i.bat


# Now the lines are stored in $1, $2, $3, ...

echo $i
done
 
Old 05-25-2010, 01:33 PM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by Hko View Post
$IFS is a special environment variable, and you do have it set, because it alwasy exists.
It is so important that if you do unset it, bash behaves in all ways as if it had its default value. This means you never need to save the existing value (unless it has been set to something different from the default) as suggested in replies above. It is simpler and works just as well to do
Code:
IFS=whatever_you_need_now
<do stuff using the special $IFS>
unset IFS
 
Old 05-25-2010, 01:39 PM   #13
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Also, strongly prefer the while-read loop over IFS tricks. It's far more intuitive and reliable. See 'Why you don't use "for" for this' in http://mywiki.wooledge.org/BashFAQ/001
 
Old 05-25-2010, 03:24 PM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
ifeatu;

Next time, please do not jump into an old thread like this. It's almost always better to start a new thread, especially when an existing one is 4 years old.
 
Old 06-01-2010, 03:51 AM   #15
cioraneanu
LQ Newbie
 
Registered: Jun 2010
Posts: 1

Rep: Reputation: 0
Problems

Having problems with reading from file. This is my script.

Code:
nr=0
cat lista_ip.txt | while read a
do

if [ xxx]
then

echo "yyyyyyyy"
nr=$(($nr+1))
echo "NR=$nr"
fi

done

echo "NR=$nr"
It does what i needed, counts the number of lines but after it exits the while no values were saved. My $nr variable is back to 0 in the end. Any ideas?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 09:16 PM
bash: read a line from file zokik Linux - General 6 12-10-2008 10:24 AM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 08:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 05:16 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM


All times are GMT -5. The time now is 04:38 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration