I am trying to write a shell script that will allow the typing of a value, then using that value to replace data in a text file.
I suspect I need sed.
The format of the file is:
Variable1:Value1
Variable2:Value2
The interaction would be something like:
Shell Prompt: "Please enter the value for Variable1: "
User enters: "NewValue"
at which point I'd need to search the text file for Variable1:<ANYTHING> and replace it with Variable1:NewValue
I am barely knowledgeable in sed and RegEx. I tried looking through Sed - An Introduction and Tutorial but I think I'm not quite at that level yet.
Any help is welcome!
Thanks,
Alex
the logic you can use in your script is as folloes :
%s/oldword/newword/g
You can take the newword and oldword as input from screen and pass them into ur script.
hope I have made myself clear
please revert back in case you need any more help ..
Thanks and Regards
Ultimatix.
for getting the new variable use code
====================
echo "Please enter the value for Variable1: \c"
read newvar
if [ -z "$newvar" ];then
echo " NO INPUT DETECTED"
else
echo " going to replace $variable1 with $newvar"
# here you can have your code to replace the old variable with $newvar
Wow, thank you both for such fast responses.
That is a great help, but my only remaining problem is that I don't know what will come after Variable1:, Variable2:, etc
They may have already run this previously so now rather than:
Variable1:Value1
Variable2:Value2
it could read:
Variable1:NewValue
Variable1:SomeOtherValue
How could I search for a known beginning and replace the whole line with that same beginning followed by a new value?
Thanks again,
Alex
Something like this?
read newvar1
read newvar2
sed -e "s/Variable1:/Variable1:$newvar1/" -e "s/Variable2:/Variable2:$newvar2/" file > newfile
mv newfile > file
Regards
That's not working for me. Here are some specifics:
Existing line:
smtpDestination:=125
Command Run:
sed -e "s/smtpDestination:=/smtpDestination:=localhost:25/" alex.cfg > alex2.cfg
New Line:
smtpDestination:=localhost:25125
It's inserting the localhost:25, rather than replacing the line.
Thanks,
Alex
sed -e "s/smtpDestination:=.*/smtpDestination:=localhost:25/" alex.cfg > alex2.cfg
Perfect! Thanks everyone!
Last question I promise. How can I read and extract the existing data so I can show it to the user before they change it?
So for example, how would I extract
localhost:25
from
smtpDestination:=localhost:25
Thanks for all your help everyone!
Alex
sed -e "s/smtpDestination:=\(.*\)/\1/" alex.cfg
It's not quite right I think - it's printing out the whole alex.cfg.
My script looks like this:
#!/bin/bash
function outputMenu {
cat > /dev/stdout <<DELIM
Change SMTP Destination
Enter the IP Address and Port Number of your Mail Server
Example: 192.168.0.1:25
{ENTER} = No Change
DELIM
sed -e "s/smtpDestination:=\(.*\)/\1/" alex.cfg
read USERCHOICE
case $USERCHOICE in
"") exit 0;;
) sed -e "s/smtpDestination:=. /smtpDestination:=$USERCHOICE/" alex.cfg > alex2.cfg && mv alex2.cfg alex.cfg;;
esac
}
clear
outputMenu
Am I doing something wrong?
Thanks,
Alex