Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 06-10-2011, 08:13 AM   #1
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:


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:

while (fscan(list,s1) !=EOF){
print (s1)

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:
the output of the above program would be:

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

Rep: Reputation: 121Reputation: 121
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
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:
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
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
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:
Read file into array in Bash

good luck


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 > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:36 PM.

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