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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
01-20-2004, 02:06 PM
|
#1
|
LQ Newbie
Registered: Jan 2004
Posts: 1
Rep:
|
bash shell script read file line by line.
I am using the following code to read line by line from a file, however my problem is that $value=0 at the end of the loop possibly because bash has created a subshell for the loop or something similar. How can I solve this.
value=0;
while read line
do
value=`expr $value + 1`;
echo $value;
done < "myfile"
echo $value;
Note: This example just counts the number of lines, I actually desire to do more complex processing than this though, so 'wc' is not an alternative, nor is perl im afraid.
Thanks Darren.
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
01-20-2004, 02:27 PM
|
#2
|
Member
Registered: Feb 2003
Location: Atlanta, GA
Distribution: RHAS 2.1, RHEL3, RHEL4, SLES 8.3, SLES 9, SLES9_64, SuSE 9.3 Pro, Ubuntu, Gentoo
Posts: 335
Rep:
|
I think value may be a reserved word, maybe? I used a=$(($a+1)); instead of value=`expr $value + 1`; but it essentially the same. Replacing value with a seemed to work:
Code:
a=0
while read line
do a=$(($a+1));
echo $a;
done < "myfile"
echo "Final line count is: $a";
|
|
1 members found this post helpful.
|
01-20-2004, 03:43 PM
|
#3
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep:
|
Strange, I copied/pasted the first script (Darren's) into emacs. Put #!/bin/bash at the first line. chmod +x the script. And made a dummy "myfile" containing 23 lines of rubish.
And it worked!
(Debian testing/sarge w/ bash 2.05b.0(1) )
|
|
|
01-21-2004, 01:42 AM
|
#4
|
Member
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569
Rep:
|
Actually, if awk is available, that would be better
|
|
1 members found this post helpful.
|
01-21-2004, 08:06 AM
|
#5
|
Member
Registered: Feb 2003
Location: Atlanta, GA
Distribution: RHAS 2.1, RHEL3, RHEL4, SLES 8.3, SLES 9, SLES9_64, SuSE 9.3 Pro, Ubuntu, Gentoo
Posts: 335
Rep:
|
Perl is actually excellent for text maniplulation and has sed, awk and many other native utilities capablilites built in. With modules from CPAN you do pretty close to anything with perl.
|
|
1 members found this post helpful.
|
01-21-2004, 11:58 PM
|
#6
|
LQ Newbie
Registered: Sep 2003
Location: hyderabad
Posts: 12
Rep:
|
#!bin/bash
while read line
do
echo $line
done
|
|
1 members found this post helpful.
|
01-22-2004, 01:06 AM
|
#7
|
Member
Registered: Oct 2003
Posts: 47
Rep:
|
Code:
#!/bin/sh
echo enter file name
read fname
exec<$fname
value=0
while read line
do
value=`expr $value + 1`;
echo $value;
done
echo "****$value";
Above code worked fine in Redhat 9.0 . I don't know what is ur problem here ? If u have even some more doubts at this code , inform me. Bye.
|
|
1 members found this post helpful.
|
03-21-2004, 11:53 PM
|
#8
|
LQ Newbie
Registered: Mar 2004
Posts: 1
Rep:
|
Bash's sh emulation (that is if a script is called with /bin/sh instead of /bin/bash) does not spawn a subshell for that kind of while loop invloving IO redirection. Traditional sh as the one found in Solaris does.
A quick and dirty solution would be to save the result in a temporary file.
Code:
#!/bin/sh
TMPFILE="/tmp/test.$$.`whoami`"
n=0
while read curline; do
n=`expr $n + 1`
echo "$n" > $TMPFILE
done < "yourfile"
n=`cat $TMPFILE`
echo "Total: $n"
rm -f $TMPFILE
Still I'd like to know if there are more elegant solutions.
Cheers,
Ronny
|
|
3 members found this post helpful.
|
09-22-2005, 02:25 PM
|
#9
|
Member
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141
Rep:
|
Hi, I don't know if anyone still reads this but when I try to read a file line by line, I use something like "head -n $x filename | tail -n 1" where $x loops from 1 through the amount of lines in the file which can easily be found using "wc -l filename"
Does this make any sense or isn't it what you're looking for.
cheers!
|
|
|
09-22-2005, 03:20 PM
|
#10
|
Member
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141
Rep:
|
In fact, what's bothering me is that wc doesn't just return the number of lines in the file but also the name of the file which made it unusable in a conditional statement... (damn you! damn you, wc!)
I had to trick it like this:
**********************************
#!/bin/bash
# read a file line by line
file=/mnt/hdb/backupscripts/backup.conf
x=0
lns=`wc -l $file`
y=`expr "$lns" : '\([0-9]*\)'`
while [ "$x" -lt "$y" ]
do
let x=x+1
head -n $x $file | tail -n 1
done
exit 0
***********************************
If you see a more elegant way, please let me know.
thanks.
|
|
2 members found this post helpful.
|
09-22-2005, 03:46 PM
|
#11
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
|
Quote:
In fact, what's bothering me is that wc doesn't just return the number of lines in the file but also the name of the file which made it unusable in a conditional statement... (damn you! damn you, wc!)
|
what about :
Code:
while [ $x -lt $(cat $file | wc -l) ]
do
...
?
|
|
2 members found this post helpful.
|
09-25-2005, 10:32 AM
|
#12
|
Member
Registered: May 2005
Posts: 378
Rep:
|
Quote:
Originally posted by jlliagre
what about:
Code:
while [ $x -lt $(cat $file | wc -l) ]
do
...
?
|
The clever way to get rid of that pesky filename is not to give it!
Code:
while [ $x -lt $(wc -l <$file) ]
do
...
|
|
3 members found this post helpful.
|
09-25-2005, 10:41 AM
|
#13
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
|
Yep, that one is better !
|
|
0 members found this post helpful.
|
06-14-2006, 04:08 PM
|
#14
|
LQ Newbie
Registered: Jun 2006
Location: WA, USA
Distribution: Fedora Core 10
Posts: 16
Rep:
|
while read line giving giving the directory listing instead of just the file!?
I am having a lot of trouble with this form of the read line script.
Code:
oneLineFile=
for myFile in `dir`
do
while read myLine
do
oneLineFile="$oneLineFile$myLine"
done < $myFile
echo $oneLineFile
echo ------------
done
For some reason while read myLine spits out the directory listing after it has read the entire file!
For example, a file with contents
Code:
hello
from some
file
in the directory with `dir` listing:
Code:
file1.txt
file2.txt
...
file99.txt
yields output:
hello from some file file1.txt file2.txt ... file99.txt
------------
My guess is read myLine is not returning the correct value that would exit the while loop. And so it just keeps reading on up the stack where the `dir` output is stored.
I am using
GNU bash, version 3.1.17(6)-release (i686-pc-cygwin)
-J Tom Moon
Last edited by jtmoon; 06-14-2006 at 04:18 PM.
Reason: add an example
|
|
1 members found this post helpful.
|
06-14-2006, 04:38 PM
|
#15
|
LQ Newbie
Registered: Jun 2006
Location: WA, USA
Distribution: Fedora Core 10
Posts: 16
Rep:
|
read line use `cat $file` instead of just $file
Nevermind!
After many stoooopid problems (too many to repeat) I finally went with this solution:
File rmLines.sh
Code:
for myFile in `dir`
do
/helper.sh "$myFile"
done
File helper.sh
Code:
echo 1 is $1
cat $1 | while myLine=`line`
do
temp="$allFile"
allFile="$temp $myLine"
echo "$allFile" > ../edit/$1
done
Perhaps this has something to do with CR/CL line endings in Windows text files?
Or just a problem with cygwin bash?
I would *swear* that my first attempt would have worked under bash on my Fedore Core machine.
- J_Tom_Moon_79
UPDATE 2009/4/22 :
See my last post in this thread for a better solution:
http://www.linuxquestions.org/questi...ml#post3517917
Last edited by jtmoon; 04-22-2009 at 10:32 PM.
Reason: final result
|
|
2 members found this post helpful.
|
All times are GMT -5. The time now is 10:44 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|