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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
10-03-2007, 05:15 AM
|
#16
|
|
LQ Newbie
Registered: Oct 2007
Posts: 1
Rep:
|
I am not getting value outside the loop
Hi This is my script like..
while read line
do
myval="DAKS"
done
echo $myval
i am not getting the value output outside the while loop
help me out if u havr some others
|
|
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
10-03-2007, 06:34 AM
|
#17
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by Darren[UoW]
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.
|
most of the problems i need to solve doesn't involve the use of loops at all. sometimes, using while loops sometime could be slower than just using tools like awk.
if you want to do more complex processing, why not try awk.
Code:
awk '{
# some processing with file
}
END { print "number of lines is " NR }' file
|
|
|
|
04-28-2008, 02:05 PM
|
#18
|
|
LQ Newbie
Registered: Mar 2008
Posts: 3
Rep:
|
loop
hi,
I am trying to do a loop, the main puporse of the loop is that it will search in a multiple text files stored on tmp2 a pattern /remote-host so it would output the pattern into tmp3.
The code that I have is the following:
for i in $( tmp2 ) ; do
sed -n '/remote-host/p' tmp2 >> tmp3
done
and obviously is not working, I would appreciate any help.
regards,
mike
|
|
|
|
08-10-2009, 06:39 AM
|
#19
|
|
LQ Newbie
Registered: Mar 2004
Location: UK
Distribution: Debian, Ubuntu, Mer
Posts: 3
Rep:
|
A simple solution to the subject line:
cat file |
(
while read line
do
echo process $line
done
)
|
|
|
|
08-10-2009, 07:47 AM
|
#20
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
you are digging up an old thread. check the date of post next time. also, no need to use cat. its useless
|
|
|
|
08-10-2009, 08:14 AM
|
#21
|
|
LQ Newbie
Registered: Mar 2004
Location: UK
Distribution: Debian, Ubuntu, Mer
Posts: 3
Rep:
|
Quote:
Originally Posted by ghostdog74
you are digging up an old thread. check the date of post next time. also, no need to use cat. its useless
|
The only reason I posted is because such an old thread was the top search result on google and had no answers that a new user could, IMHO, understand.
Using cat rather than a redirect here (again IMO) makes the answer crystal clear and hopefully will educate rather than confuse
Code:
(
while read line
do
echo process $line
done
) < file
does work but it isn't clear as you start to read the code without realising that, potentially many lines later, a file will be directed into the sub shell.
If you are after an optimised solution feel free to use exec with a fd numeric redirect and read -u too :
Code:
exec 9<file
while read -u9 line
do
echo process $line
done
Explanation left as an exercise for the reader...

|
|
|
1 members found this post helpful.
|
02-03-2010, 02:10 PM
|
#22
|
|
LQ Newbie
Registered: Feb 2010
Posts: 1
Rep:
|
Code:
#!/bin/bash
IFS=$'\12'
for i in $(cat somefile.txt); do
echo "line: $i";
done
Last edited by thehog; 02-03-2010 at 02:12 PM.
|
|
|
|
05-15-2010, 02:18 AM
|
#23
|
|
LQ Newbie
Registered: Aug 2009
Posts: 15
Rep:
|
Check bellow link, there I posted 5 methods to parse a file line by line
Process a file line by line
Last edited by kvmreddy; 05-17-2010 at 07:21 AM.
|
|
|
|
05-15-2010, 03:02 AM
|
#24
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by kvmreddy
|
The link is slightly mangled. Here's unmangled: Process a file line by line.
Nice blog
Regards request for correction ...
Regards " Bash can sometimes start a subshell in a PIPED "while-read" loop", what are the circumstances under which bash does not do so?
Regards
Code:
FILENAME=$1
...
done < $FILENAME
That will fail if $1 includes embedded IFS charcters; safer to use double quotes: done < "$FILENAME".
Regards " exec 3<&0 Now all of the keyboard and mouse input is going to our new file descriptor 3" it is only the keyboard input, not the mouse input.
Regards "while read LINE Using File Descriptor" an alternative, that does not require saving fd 0 and restoring it is to assign the file to, say, fd 3 and use read's -u option to read from fd3.
Regards typos, " The file descriptors for stdin,stdout, and stderr are 0,1, and 2, respectively" it is more correctly "The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively", that is with a space after the fist commas in the list.
The test timings are very useful showing how much faster awk is at processing a large file but it would be nice to see the other case -- the case when processing a single line. You would have to process the same line many times to get the timing and of course this would buffer both the input file and awk in RAM but it would still be interesting. I understand that the shell's fork-exec to run awk uses a lot of resource which will more than offset awk's much greater file IO and string handling efficiency.
Last edited by catkin; 05-15-2010 at 03:04 AM.
Reason: typodynamics
|
|
|
|
05-17-2010, 11:13 AM
|
#26
|
|
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,006
Rep: 
|
Besides the quoting issue that catkin mentioned, you should also use read -r in all the examples. You almost never want to not use -r. Also, use "printf" instead of "echo -e". The exact behavior of "echo -e" can change a lot from system to system.
|
|
|
|
05-17-2010, 12:41 PM
|
#27
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Why is everyone responding to this >6 year old thread?
|
|
|
|
10-16-2010, 09:46 AM
|
#28
|
|
LQ Newbie
Registered: Sep 2010
Posts: 25
Rep:
|
array within function
hello everyone!
i am passing an array in function. I want to read the first content again and again until it is empty so
function calltoarray
{
read $1[0]
while [ ! -n "$1[0]" ]
do
echo empty
read $1[0]
done
}
but the code is not working. please help me out.
|
|
|
|
10-20-2010, 06:04 AM
|
#29
|
|
Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,078
|
howto read file shell
Okay first off I realize this thread is old... very old. However the complexity that these guys are going about it is driving me nuts so I have to post this to enlighten them (and anyone who would be searching for this solution).
First off reading a file in shell is extremely simple.
lets make a simple file called mycat and let it's contents be...
Code:
#!/bin/bash
while read line;do
echo $line
done
That will read from stdin so you can read a file by simply...
Code:
chmod 755 ./mycat
./mycat < somefiletoread
The other thing is this guy is basically going about it the hard way because this can easily be done with a one liner.
Quote:
Originally Posted by dpoper1
hi,
I am trying to do a loop, the main puporse of the loop is that it will search in a multiple text files stored on tmp2 a pattern /remote-host so it would output the pattern into tmp3.
The code that I have is the following:
for i in $( tmp2 ) ; do
sed -n '/remote-host/p' tmp2 >> tmp3
done
and obviously is not working, I would appreciate any help.
regards,
mike
|
And here it is...
Code:
find /path/to/tmp2 -type f -not -name 'tmp3' -print0 | xargs -0 grep -ih 'remote-host' > tmp3
Just thought I'd post in case someone else was dealing with a similar issue...
SAM
Last edited by sag47; 10-20-2010 at 06:17 AM.
|
|
|
1 members found this post helpful.
|
03-14-2011, 02:20 PM
|
#30
|
|
LQ Newbie
Registered: Jan 2010
Posts: 1
Rep:
|
Hi, i test my script with a file and is excellent!!
for (( i = 0; i < `awk '{x++}END{ print x}' test.out`; i++))
do
echo `head -n $i test.out | tail -n 1` #return the line
done
where test.out is the file to scann!!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:04 AM.
|
|
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
|
|