LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: Disabled
Help modifying text with user input


Hello,

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:

sometext..._somemoretext...$variable1
<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
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,834
Blog Entries: 10

Rep: Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729Reputation: 1729
You need to use forward slashes, not the pipe with a sed search and replace command.
Quote:
s/regexp/replacement/
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.
Code:
$ cat investment.txt 
INVESTMENT2|0002450|000-245606|000J34434|2450003334
INVESTMENT2|0432450|000-323234|000HFF434|2150003334
INVESTMENT4|0012342|000-245606|000JT4434|3423433334
$ sed -e s/INVEST/DIVEST/g investment.txt
DIVESTMENT2|0002450|000-245606|000J34434|2450003334
DIVESTMENT2|0432450|000-323234|000HFF434|2150003334
DIVESTMENT4|0012342|000-245606|000JT4434|3423433334
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.
 
  


Reply


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


All times are GMT -5. The time now is 04:21 PM.

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