Results 1 to 3 of 3
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 ...
- 06-10-2011 #1Just Joined!
- Join Date
- Dec 2010
- Posts
- 5
reading values from a file into a string variable
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?
- 06-11-2011 #2
- 06-11-2011 #3Just Joined!
- Join Date
- Jan 2010
- Posts
- 27
First of all, you should choose a tool to parse the file.
That could be C compiler, Bash shell, Awk, Sed, Perl, PHP interpreters, etc.
Having that done will let us answer your question.
Eg. using Awk that would sound like a:
Code:$ cat "list.txt" | awk '{s1=$1; print s1}'
That will print only first word from each line of "list.txt". All lines will be processed the same way. If you need only the first word of first line:
Code:$ cat "list.txt" | awk '{s1=$1; print s1}' | head -n 1
But if your lines are already one-word formatted, then it's very by simple using cat + head:
Code:$ cat "list.txt" | head -n 1
You should choose your tools : ), there are several of them.


Reply With Quote
