replace a string in a file and then save file with new name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goreilly
Posts: 2
Joined: 28 Aug 2011 16:20

replace a string in a file and then save file with new name

#1 Post by goreilly » 29 Aug 2011 07:33

Hello, I'm a newbee. Simple question.

How do I open a text file, find a particular string, replace that string with another string, and then save the file with a new name.
So far, I can find the string in a file using the find command, but then I don't know how to replace the string in that file.

Thank you,
Gerry :D

InterociterOperator
Posts: 11
Joined: 28 Feb 2011 21:08

Re: replace a string in a file and then save file with new n

#2 Post by InterociterOperator » 29 Aug 2011 19:50

What if we save the file with a new name and THEN replace a string? :)

I would cheat and program in Edlin, which has been part of DOS since the beginning.

Make a copy of your original file with a copy command and work from it.

Code: Select all

copy Members.txt Names.txt


Now say we want to find "Harry" in our new file called Names.txt, and change it to "Mary" ...

If Names.txt contains:
Tom
Dick
Harry

and NewName.txt contains
Mary

..we could do...

Edlin c:\Names.txt < c:\EdlinCommands.txt

where the EdlinCommands file contains...

Code: Select all

1sHarry
.d
.tNewNames.txt
e


"1sHarry" means start at the first line in the file and search for the word Harry
and ".d" means delete the line that contains "Harry"
and ".tNewName.txt" means transfer the contents of "NewName.txt" to this line location (.).
"e" means save and end.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: replace a string in a file and then save file with new n

#3 Post by Ocalabob » 29 Aug 2011 21:18

Greetings Gerry!

If SED is an optional then the solution is trivial. Here's a SED
routine within a batch file as an example for your consideration.

[code]
@echo off
::Create a test file
echo Now is the time for all good men to come to the aid of their Country.>$$$Gerry.txt

::Use SED to replace some text.
type $$$Gerry.txt |SED "s/men/men and women/g" |SED "s/Country./State./g">$$$Gerry_final.txt
echo Orginal Line is:
type $$$Gerry.txt
echo.
echo Replaced line is:
type $$$Gerry_final.txt
echo.
echo Press The Space Bar to Close this Window.
pause>nul
::Housekeeping
del $$$*.txt
[code]

Best wishes Gerry!

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: replace a string in a file and then save file with new n

#4 Post by ghostmachine4 » 29 Aug 2011 22:08

[quote="Ocalabob"]

Code: Select all

::Use SED to replace some text.
type $$$Gerry.txt |SED "s/men/men and women/g" |SED "s/Country./State./g">$$$Gerry_final.txt


No need to use type, since sed takes in a file as argument. And you can combine sed commands together

Code: Select all

sed "s/men/blah blah/g;s/Country/blah blah ../g" Gerry.txt

goreilly
Posts: 2
Joined: 28 Aug 2011 16:20

Re: replace a string in a file and then save file with new n

#5 Post by goreilly » 30 Aug 2011 12:09

Responders:
Thank you so much. I downloaded sed and ran the suggested programs, and they did the trick most effectively.
Save me a lot of time struggling with dos stuff.
Gerry :D :D

Post Reply