Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-02-2015, 11:03 AM   #1
LQ Newbie
Registered: Mar 2015
Posts: 1

Rep: Reputation: Disabled
Help modifying text with user input


I apologize in advance for my limited UNIX scripting knowledge. I am new to it and really want to learn.

I am trying to write a bash script that updates a config file based on user input.

What is the best method for accomplishing this? I need it to prompt the user for two variables, find the location in the config file, and insert new text with the two variables.

I was thinking I could use sed to find the text in the config file where the new text must be inserted before and replace it with the new text, two variables, and same ending text as before. Example:

echo "1st variable?"
read variable1

echo "2nd variable?"
read variable2

sed -e "s|<the spot in the config file that needs new config>|sometext...$variable2_somemoretext...$variable1\n<the spot in the config file that needs new config>|g" config > config2

This works except variable2 is not inserted into the replace string, only variable1 is. This is the result in config2:

<the spot in the config file that needs new config>

If anyone can tell me why variable2 isn't working in the sed replace string, or if there's a much better way for accomplishing what I'm after, I'd appreciate any help I can get.
Old 03-02-2015, 11:58 AM   #2
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,610
Blog Entries: 12

Rep: Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962
You need to use forward slashes, not the pipe with a sed search and replace command.
Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The
replacement may contain the special character & to refer to that portion of the pattern space which matched, and the
special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.
Stage that sed -e command by just typing it into the command line and when you get it working, that's how you code it within the script.
$ cat investment.txt 
$ sed -e s/INVEST/DIVEST/g investment.txt
And just me nitpicking, no technical reason except to make sure some alias doesn't cause problems, I always use the full path for the command, therefore /bin/sed when I call a system command.

Last edited by rtmistler; 03-02-2015 at 11:59 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
script help? modifying text files? saturndude Linux - General 11 01-28-2012 08:34 PM
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
Modifying text files with perl Tleilax Programming 8 02-17-2009 01:54 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Modifying text properties in Gimp pwabrahams Linux - Software 4 02-22-2008 05:07 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:09 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration