How to replace text in a file with text entered


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace text in a file with text entered
# 1  
Old 04-11-2009
How to replace text in a file with text entered

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
# 2  
Old 04-11-2009
Bug

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.
# 3  
Old 04-11-2009
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
======
# 4  
Old 04-11-2009
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
# 5  
Old 04-11-2009
Something like this?

Code:
read newvar1
read newvar2

sed -e "s/Variable1:/Variable1:$newvar1/" -e "s/Variable2:/Variable2:$newvar2/" file > newfile
mv newfile > file

Regards
# 6  
Old 04-11-2009
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
# 7  
Old 04-11-2009
Code:
sed -e "s/smtpDestination:=.*/smtpDestination:=localhost:25/" alex.cfg > alex2.cfg

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Replace text in a file

I wrote a program using Perl to find and replace a text within a file. The text that needs to be replaced in the file is 'sql7.0.1' with 'sqls715'. When I execute my program I get an error message: Here is my code: #!/usr/bin/perl use strict; use warnings; my $filename =... (6 Replies)
Discussion started by: dellanicholson
6 Replies

3. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

4. Shell Programming and Scripting

How to accept command line argument as character or text if number is entered?

Hello Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex (6 Replies)
Discussion started by: aadityapatel198
6 Replies

5. Shell Programming and Scripting

Checking whether the entered text is file or not

how to check that the "file path" entered by the user has the valid file. (6 Replies)
Discussion started by: Rashid Khan
6 Replies

6. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

7. Solaris

Question marks appearing instead of text entered

I have a netscape 4.79 browser for our GUI which connects to a Solaris5.8 box. During peek hours, we see question marks appearing in the screen instead of the text we enter. This results in query failure. This problem does not happen always, and is quite irritating because, we have to close the... (6 Replies)
Discussion started by: vanz
6 Replies

8. Shell Programming and Scripting

find text but replace a text beside it

I have an html file that looks like this (this is just a part of the html file): <td colspan="3" rowspan="1" style="text-align: center; background-color: rgb(<!-- IDENTIFIER1 -->51, 255, 51);"><small><!-- IDENTIFIER2 -->UP</small></td> This is to automatically update the status of the... (4 Replies)
Discussion started by: The One
4 Replies

9. UNIX for Advanced & Expert Users

Replace the text between two lines with different text

I have a file which contains the following data. Parameters "CParameters" BEGIN DSSUBRECORD Name "TgtDB" Prompt "TgtDB" Default "edwdev" ParamType "0" ParamLength "0" ParamScale "0" END DSSUBRECORD MetaBag... (6 Replies)
Discussion started by: ukatru
6 Replies

10. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies
Login or Register to Ask a Question