Find the answer to your Linux question:
Results 1 to 4 of 4
I have been playing around with a script for a few hours and now I need to be able to output the lines in a text file one by one ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    1

    How to read a line from a txt file in a script?


    I have been playing around with a script for a few hours and now I need to be able to output the lines in a text file one by one to be used later in the script.


    What it gonna do is to read a log file and grep the usernames, then write them to a file, and then run one script for each user, to search for more information about them in the log.

    But I don't know how to output a single line from a file, and google does not return any solution.


    Thanks

  2. #2
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    Quote Originally Posted by Ravenholdt View Post
    I have been playing around with a script for a few hours and now I need to be able to output the lines in a text file one by one to be used later in the script.


    What it gonna do is to read a log file and grep the usernames, then write them to a file, and then run one script for each user, to search for more information about them in the log.

    But I don't know how to output a single line from a file, and google does not return any solution.


    Thanks
    Looking for something like this?
    Code:
    cat /some_path/some_file | while read line
    do
      <something with $line>
    done
    exit 0
    or

    Code:
    cat /some_path/some_file | while read
    do
      <something with $REPLY>
    done
    exit 0

  3. #3
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    117
    Another option, if you need to break out this loop for some reason, would be to open the file:

    Code:
    exec 5<$FILENAME
    
    # That's basic redirection syntax: $FILENAME is opened for reading and bound to file descriptor 5.  "exec" is a shell built-in which applies this redirection to the shell's session.
    
    #To read a line from the open file, just redirect the input of "read" to FD #5...
    read x <&5
    
    #To close the file, there's the following syntax.  You can think of it as "redirect 5 from nothing"...
    exec 5<&-
    You can use that "read" command in a while loop as in barriehie's examples.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85

    Thumbs up

    Here's a link to a BASH scripting guide. I've found it to be indispensable.
    Advanced Bash Scripting Guide Download - Softpedia

Posting Permissions

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