LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-10-2011, 08:13 AM   #1
AndrewJS
LQ Newbie
 
Registered: Dec 2010
Posts: 16

Rep: Reputation: 0
how to read values from a file (as part of a script)


I want to read values from a file...these are basically one word values, that is to say that the text file I want to read from contains lines of word length 1, as in:

word1
word2
word3
etc.

I actually only need the first word of the file and I want to assign it to a variable.
I am not very familiar with UNIX but I have practice with a UNIX-based software, and in this software, the method of sequentially reading values from a text file is the following:

list="list.txt"
while (fscan(list,s1) !=EOF){
print (s1)
}

where:
s1 is string variable
list is a list variable that goes through the text file sequentially
fscan is a task the sets the value of s1 to the current value of list
The above program would read through to the end of file and print the value of each word.
so say if I had a file list.txt with entries:
word1
word2
word3
the output of the above program would be:
word1
word2
word3

My question is, what tasks and variables can be used to get the same result in UNIX?
And also regarding the list variable...how does one declare and assign values to such variables in UNIX?
 
Old 06-10-2011, 08:51 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
May I ask what you are trying to accomplish? Sounds a lot like an assignment for a class.
 
Old 06-10-2011, 09:21 AM   #3
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
well let's hope this is not a homework assignment.... if you just need to get the first word into a variable, you could simply use:
Code:
s1=`head -1 list.txt`
for more info: 'man bash', 'man head'

if you need to read each word and process it, look at the for loop in the bash man page and also 'man cat'
 
Old 06-11-2011, 09:05 AM   #4
AndrewJS
LQ Newbie
 
Registered: Dec 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks to both comments...and no, this isn't a class assignment
like I said I often use a unix based software (an astronomy package) that has a programming language like that shown in my first post, however I needed a unix variable for a tiny part of a script (that I wrote for the astronomy package).
 
Old 06-13-2011, 11:40 AM   #5
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
I can not think of how many ways to do it is possible, but see something like these:
bash-script-read-file-into-array
Read file into array in Bash
reading-a-text-file-line-by-line-with-bash

good luck
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 01:19 PM
bash script 'read' with default values m4rtin Programming 1 03-05-2010 09:06 AM
need to read values in middle of reading value from a file vasireddy.jaipal Linux - Newbie 4 11-27-2009 02:32 PM
need to read values in middle of reading value from a file vasireddy.jaipal Linux - General 2 11-27-2009 04:17 AM
read config file parameter values sharathkv25 Programming 2 02-15-2007 01:55 AM


All times are GMT -5. The time now is 08:09 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration