ProgrammingThis 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.
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.
Hi:
I'm trying do write a shell script which must operate on each line of an ASCII text file. So, all the code must be inside a loop, and inside the loop, the first thing should be to read the next line from the file. I have the bash read command. But it reads from stdin. Any way to make read from a file? Thanks.
It certainly does. I now have the skeleton of my program. This can seem as a easy attitude on my part, but I can assure you I learn from it. Because I'm about to ask one more question.
I have now the line in my power. It is in ONELINE. The line will be of the form <blank space>INDEX NN mm:ss.ss
How could I capture the part mm:ss.ss?
I want to do this in a shell script language, because I'm learning bash scripting and this would further my knowledge. Regards.
Note that you can also pipe the output of a command into a loop:
Code:
command | while read
do
# stuff
done
The problem is that this will run the loop in a subshell, so that if it modifies any variables, the changes will not stay once the loop exits. If that's a problem, you can use this instead:
Code:
while read
do
# stuff
done < <(command)
The <(command) syntax creates a named pipe, connects the command's output to it, and evaluates to the path to the named pipe.
You can use more then one variable after the while read statement:
Code:
#!/bin/bash
while read FIRST SECOND THIRD
do
echo "-----------------"
echo "$FIRST"
echo "$SECOND"
echo "$THIRD"
done < infile
$ cat infile
INDEX1 XX mm1:ss.ss
INDEX2 YY mm2:ss.ss
INDEX3 ZZ mm3:ss.ss
$ ./foo
-----------------
INDEX1
XX
mm1:ss.ss
-----------------
INDEX2
YY
mm2:ss.ss
-----------------
INDEX3
ZZ
mm3:ss.ss
Hope this helps.[/Really this stuff is awesome.. i was just posting a thread for my requirement like the same, but i got this one from Druuna...thanks alot]
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.