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 ...
- 01-31-2012 #1Just 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.
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.Code:read variable STRING=$variable "Auth-Type := Local, User-Password == "$variable" Service-Type = Login-User" sed -i 's/SearchTerm/'$STRING'/g' userfile
Any help is greatly appreciated.
- 02-01-2012 #2Just 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:
BeforeCode:#!/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; } < userfileAfterCode:$ cat userfile This SearchTerm is a test
Used this as a reference: sed.. Add new line after finding text - The UNIX and Linux ForumsCode:$ ./theScript MyLogInInfo $ cat userfile This Auth-Type := Local, User-Password == MyLogInInfo Service-Type = Login-User is a test $
Last edited by histrungalot; 02-01-2012 at 04:46 AM.
- 02-01-2012 #3Linux 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.:
also, note that:Code:STRING="$variable \"Auth-Type := Local, User-Password == \"$variable\"\n Service-Type = Login-User" sed -i "s/SearchTerm/$STRING/" userfile
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
- 02-01-2012 #4Linux 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:
and an entered string ofCode:This SearchTerm is a test
Using the code:Code:VARIBALE VAUE FROM READ INPUT
and will result in userfile of: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
One should not edit a file in place with sed.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
- 02-01-2012 #5Just Joined!
- Join Date
- Aug 2011
- Posts
- 48
alf55: Gives an error?
atreyu: Gives an error?Code:./bb MyLogin sed: 1: /dev/fd/63: command a expects \ followed by text
I am using a Mac, if that makes a difference?Code:./bb MyTest sed: 1: "i\n": command i expects \ followed by text
- 02-01-2012 #6Just 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
- 02-01-2012 #7Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
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


Reply With Quote
