Find the answer to your Linux question:
Results 1 to 7 of 7
Basically, I just need to search a text file for a line, then add a couple lines, with user input, to the file. (Radius server user file) I've tried using ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Location
    Illinois
    Posts
    7

    BASH - Add lines to file after text is found

    Basically, I just need to search a text file for a line, then add a couple lines, with user input, to the file. (Radius server user file)
    I've tried using sed, but continue to run into problems.

    I am using something similar to the code below.
    It is not working at all. I'm new to sed.


    Code:
    read variable
    STRING=$variable "Auth-Type := Local, User-Password == "$variable"
     Service-Type = Login-User"
    
    sed -i 's/SearchTerm/'$STRING'/g' userfile
    As you can see, the string that needs to replace the line, not only has a break in it, but it also contains the entered variable.

    Any help is greatly appreciated.

  2. #2
    Just Joined!
    Join Date
    Aug 2011
    Posts
    48
    I have looked for this before and still can't seem to find the answer for sed. That said, I did find this:
    Code:
    #!/bin/bash
    read input
    { rm userfile; awk '{ if ( $0 ~ /SearchTerm/ ) {
             printf("Auth-Type := Local, User-Password == %s\nService-Type = Login-User\n","'$input'");
         } else {
             print $0;
         }
    }' > userfile; } < userfile
    Before
    Code:
    $ cat userfile
    This
    SearchTerm
    is
    a 
    test
    After
    Code:
    $ ./theScript
    MyLogInInfo
    $ cat userfile
    This
    Auth-Type := Local, User-Password == MyLogInInfo
    Service-Type = Login-User
    is
    a 
    test
    $
    Used this as a reference: sed.. Add new line after finding text - The UNIX and Linux Forums
    Last edited by histrungalot; 02-01-2012 at 04:46 AM.

  3. #3
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    the above solution works good. for a sed solution, you can use the newline character (\n) instead of actual new lines, e.g.:

    Code:
    STRING="$variable \"Auth-Type := Local, User-Password == \"$variable\"\n Service-Type = Login-User"
    sed -i "s/SearchTerm/$STRING/" userfile
    also, note that:
    1. double-quotes in the variable have to be escaped
    2. the entire variable needs to be enclosed in double-quotes
    3. the sed line needs double-quotes around the entire expression, and the single-quotes around the variable name are not needed.
    Last edited by atreyu; 02-01-2012 at 05:21 AM. Reason: typo

  4. #4
    Linux User
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    262

    Using only bash and sed ...

    I am assuming that you do not wish to remove the "search string" line.

    This will take the userfile of:
    Code:
    This
    SearchTerm
    is
    a 
    test
    and an entered string of
    Code:
    VARIBALE VAUE FROM READ INPUT
    Using the code:
    Code:
    read variable
    mv userfile userfile.old
    sed -f <(echo $"/SearchTerm/ a\
    $variable \"Auth-Type := Local, User-Password == \"$variable\" \
     Service-Type = Login-User/") < userfile.old >userfile
    rm userfile.old
    and will result in userfile of:
    Code:
    This
    SearchTerm
    VARIBALE VAUE FROM READ INPUT "Auth-Type := Local, User-Password == "VARIBALE VAUE FROM READ INPUT"  Service-Type = Login-User/
    is
    a 
    test
    One should not edit a file in place with sed.

  5. #5
    Just Joined!
    Join Date
    Aug 2011
    Posts
    48
    alf55: Gives an error?
    Code:
    ./bb
    MyLogin 
    sed: 1: /dev/fd/63: command a expects \ followed by text
    atreyu: Gives an error?
    Code:
    ./bb
    MyTest
    sed: 1: "i\n": command i expects \ followed by text
    I am using a Mac, if that makes a difference?

  6. #6
    Just Joined!
    Join Date
    May 2009
    Location
    Illinois
    Posts
    7
    I found an easy solution using awk:

    Code:
    #!/bin/bash
    var1="New line 1"
    var2="New line 2"
    search="Search term"
    awk -v S="$search" -v V1="$var1" -v V2="$var2" '{if ($0~S) print $0 "\n" V1 "\n" V2; else print $0}' users > users.tmp
    mv users.tmp users

  7. #7
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    Quote Originally Posted by histrungalot View Post
    atreyu: Gives an error?
    Code:
    ./bb
    MyTest
    sed: 1: "i\n": command i expects \ followed by text
    oh, my bad. i just copied that part of the code from the OP, then modified the regex parts. i didn't notice that the -i was not followed by the required argument, that is, a file extension. you could try sed -i.bak ... , which would make a back up of the file first (named "original_filename.bak"), and the changes would be made inline to the existing file. Or you could leave off -i altogether and not make the backup copy.
    Last edited by atreyu; 02-01-2012 at 10:42 PM. Reason: typo

Posting Permissions

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