for loop or while loop to read the fields of a file..
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
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.
Here you use the -F parameter for define the field separator, so each data after a space will be a new field.
These fields are accessed by $1 $2 $3 ...
The newline is \n. I hope it helped.
Thank you for your reply. I am sorry to say that I know this how to do with awk, but i want to read the input from a file use bash. Acutally my awk script will do some calculations depending the month and year of the file(which i cating use for loop).
Thank you for your reply. I am sorry to say that I know this how to do with awk, but i want to read the input from a file use bash. Acutally my awk script will do some calculations depending the month and year of the file(which i cating use for loop).
Ok,
you should have mentioned that in your first post. Well, in this case you might want to try
Code:
while read line; do
echo "$line" | awk 'your awk commands ...'
done < infile
exec 4<"file"
while read -r a b<&4
do
echo "Month: $a , Year: $b"
done
exec 4<&-
you do not need the awk command inside the while loop if all you are going to do is just to print those 2 statements.
If you are going to do some further processing using awk, then using purely awk to do your processing is more efficient than a while read loop, especially if your files are big in size.
If you want to use a for loop (with cat for example), you have to set IFS=$'\n'.
Last edited by ghostdog74; 09-02-2010 at 09:09 PM.
exec 4<"file"
while read -r a b
do
echo "Month: $a , Year: $b"
done
exec 4<&-
If you want to use file descriptors then it should be done <&4
The OP also stated that this is just an example and that he needs awk for some calculations. Maybe it can all be done in bash; or with awk. The OP will have to provide more details on that matter.
So I do not wish to hijack this thread, but as I have recently doing more with descriptors, may I ask what the advantage is here of using the descriptor as
opposed to just redirecting the file directly into the loop?
So I do not wish to hijack this thread, but as I have recently doing more with descriptors, may I ask what the advantage is here of using the descriptor as
opposed to just redirecting the file directly into the loop?
with a file descriptor, you take control of how you are processing the lines..eg, To simulate grep -A 2 "string"
Code:
exec 4<"file"
while read -r line <&4
do
case "$line" in
*string*)
for((i=1;i<=2;i++))
do
read nextline<&4
echo $nextline
done
esac
done
exec 4<&-
Of course, you can also do that with some counters. But the idea is the same in other languages where you can seek/tell a file using a file handle. (don't think bash has seek/tell though)
Also, you can try benchmarking using file descriptors vs input redirection. you might get more insight (yes, i have done it, so i know which one runs faster )
Last edited by ghostdog74; 09-02-2010 at 08:52 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.