LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-05-2011, 04:44 PM   #1
odstderek
LQ Newbie
 
Registered: Nov 2011
Location: USA
Distribution: Backtrack
Posts: 9

Rep: Reputation: Disabled
Reading from file


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?
 
Old 11-05-2011, 05:03 PM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
read file where:
- BASH (shell) ?
- PHP ?
- Perl ?
...

be descriptive and you will get answers, describe the system you use (RHEL, Deb...) and what language you want to use (Perl, Python, PHP....)
 
Old 11-05-2011, 06:47 PM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
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?
 
Old 11-05-2011, 06:52 PM   #4
odstderek
LQ Newbie
 
Registered: Nov 2011
Location: USA
Distribution: Backtrack
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-05-2011, 08:50 PM   #5
odstderek
LQ Newbie
 
Registered: Nov 2011
Location: USA
Distribution: Backtrack
Posts: 9

Original Poster
Rep: Reputation: Disabled
say I have a loop like this:
Code:
while read word
do
# bunch of code
done
how can I do this, 1 word at a time:
Code:
while read word < $1
do
# bunch of code
done
can I just read $1? will that actually read the contents of the file?
 
Old 11-05-2011, 10:57 PM   #6
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by odstderek View Post
how can I do this, 1 word at a time:
Code:
while read word < $1
do
# bunch of code
done
can I just read $1? will that actually read the contents of the file?
What happens when you try that?
 
Old 11-06-2011, 03:40 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 11-06-2011, 03:52 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 11-06-2011, 03:54 AM   #9
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
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.
 
Old 11-06-2011, 11:17 AM   #10
odstderek
LQ Newbie
 
Registered: Nov 2011
Location: USA
Distribution: Backtrack
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading ext3 file system on Mac and overriding file permission moravveji Other *NIX 6 09-12-2011 02:25 AM
[SOLVED] Reading in a file in C, after file read null terminator at strings end gone josip76 Programming 2 05-14-2010 12:44 AM
Reading/Wirting file/parsing xml file using javascript fakhrul Programming 1 08-14-2007 05:08 PM
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration