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.
Hello! I want to read text from a file (the file is just a normal paragraph) word by word. To be honest I have no clue how to do this. I can't use input redirection, I need to read from a file given by parameter 1.
Here's the summary of the problem: I need to read from a file ($1) word by word (after which I apply a test to each word but I think I can do that). How can I do this? Can I use the read statement?
I'm guessing you want instructions for the shell because you mention input redirection and $1. Just guessing though.
I believe the traditional method is to use a read command enclosed within a while loop, with the input of the entire loop redirected from the file. So ... why can't you use input redirection?
I can't use input redirection because this is an assignment for a class. The program can only be invoked by the program name and its argument. This is a bash script with the text file in the same directory as the script, on a Ubuntu system.
If I can't figure anything else out I'll use input redirection but I will lose points for doing so.
If you can't use redirections*, have a look at file descriptors.
To work word-by-word, try using an array.
(hint: take a look at read's options)
*I get the sense that this rule only applies to launching the script itself, but it would be very strange indeed if it also referred the commands inside the script. You might want to get clarification on that.
To read word by word, in alternative to arrays, you can try the tr command to change space/tabs with newlines (so that you have a single word on each line) and feed the loop with the result.
When I want to read a file a word at a time I use for to iterate over the output of cat. Don't know if that's the most efficient way to do it, but it works and it's very simple.
Since you say it's an assignment and I've already found some alternatives to my usual method in less than 60 seconds via Google (all of which are a lot more complicated than mine and I'm not seeing how they're technically better), I'm a bit reluctant to give you actual code but if you look at the for and cat commands and also command substitution it should be easy to figure out.
I ended up solving the problem using the sed command inside a do loop.
Telengard: it causes a runtime error, something about the syntax being wrong.
David: yes I could use redirections in the script, just not when invoking it. sorry for the confusion. to get this to work however, I didn't need them.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.