Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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?

  2. #2
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    Quote Originally Posted by AndrewJS View Post
    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?
    Hard to make much sense out of this. "cat list.txt" would do what you're asking, as stated.
    Last edited by Mudgen; 06-11-2011 at 03:33 AM. Reason: quote, filename

  3. #3
    Just Joined!
    Join Date
    Jan 2010
    Posts
    40
    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.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •