Find the answer to your Linux question:
Results 1 to 7 of 7
Hello there. I have a script which reads lines from a file. I do this by setting variables in a for loop using cat. Now cat stops reading at a ...
  1. #1
    Just Joined!
    Join Date
    Oct 2006
    Posts
    8

    Ksh/bash: read file including delimiters ???

    Hello there.

    I have a script which reads lines from a file. I do this by setting variables in a for loop using cat. Now cat stops reading at a delimiting character, so the line:

    This is my example

    will set the variable four times. I would really like to put the whole line into one variable. This can probably be done with either awk or sed but I cannot seem to find how to do this.

    Hope some of you can help me out.

    Greetings ... Danny

  2. #2
    Linux Newbie
    Join Date
    Mar 2008
    Location
    Hyderabad
    Posts
    109
    You can use an array and later concatenate them

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    The way I get a whole line in a variable is use the 'read' command as follows:

    Code:
    while read LNE
        do
             echo $LNE
        done < filename
    Use the -r option if lines will contain backslashes (\).

  4. #4
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by vsemaska View Post
    The way I get a whole line in a variable is use the 'read' command as follows:

    Code:
    while read LNE
        do
             echo $LNE
        done < filename
    Use the -r option if lines will contain backslashes (\).
    That will not get a whole line of there is leading or trailing
    whitespace.
    Code:
    while IFS= read -r LINE
    do
      printf "%s\n" "$LINE"
    done < filename

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    cfa is correct.

    IFS stands for "Internal Field Separator". It contains all of the characters on which input will be split. By default it contains spaces, tabs, and newlines (at least, there may be more).

    If you want it to split on newlines, you might do:
    Code:
    IFS=$'\012'
    ...
    Where 012 is the octal value for a newline.
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by DvdPluijm View Post
    Hello there.

    I have a script which reads lines from a file. I do this by setting variables in a for loop using cat. Now cat stops reading at a delimiting character, so the line:

    This is my example

    will set the variable four times. I would really like to put the whole line into one variable. This can probably be done with either awk or sed but I cannot seem to find how to do this.

    Hope some of you can help me out.

    Greetings ... Danny
    using for loop with cat to iterate file contents has its "quirks". It best to use while loop with read as others have suggested, or one of the other way is to use awk. Depending on what you want to do
    Code:
    awk '
     {
       # the variable $0 stores the line value.
     }
    ' file

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Not relevant to the OP but the easiest way to get a whole file into a variable is:
    Code:
    VAR=$(<filename)
    Whitespace and newlines will be preserved, but lost if you echo the variable unquoted. To put a specific line in you could use sed:
    Code:
    VAR=$(sed -n ${LINE}p file)
    where LINE holds the number of the line you wish to extract.

Posting Permissions

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