Tech Support > Operating Systems > MS-DOS > find and replace a string in a text file...possible with a batch file?
find and replace a string in a text file...possible with a batch file?
Posted by KILOWATT on July 8th, 2005


Hi...thanks to read. In a textfile that contains lets say the following:

D:\Images\PHOTOS\light trailing 01.JPG
D:\Images\PHOTOS\light trailing 02.JPG
D:\Images\PHOTOS\light trailing 03.JPG

It is possible by using a batch file to change each line to to the folowing:

D_\Images\PHOTOS\light trailing 01.JPG
D_\Images\PHOTOS\light trailing 02.JPG
D_\Images\PHOTOS\light trailing 03.JPG

I can do it manually (dit it with Wordpad) but i would like to automate the
process if possible. Maybe i need an external application, maybe not...? In
fact the text file i wish to modify automatically, contains thousands of
lines like those, weighting a little more than 700KB. I've joined three
lines for simplicity. ;-) If you ask why i need such modifications, the
textfile would be used as a listfile by Rar.exe to extract only the backed
files in the archive that match the ones in this listfile. Since filenames
can't contain the : character, Rar packs the files with _ instead of : when
choosing to include full patch including drive letter during archiving
operation.

--
Alain(alias:Kilowatt)
Montréal Québec
PS: 1000 excuses for grammatical errors or
omissions, i'm a "pure" french canadian! :-)
Come to visit me at: http://kilowatt.camarades.com
(If replying also by e-mail, remove
"no spam" from the adress.)


Posted by William Allen on July 8th, 2005


"KILOWATT" wrote in message
If you have a lot of string substitution to do, try SED (the Stream
Editor). This is a command-line tool that replaces text using
regular expressions if needed (not needed in your example).

This screen capture shows SED syntax to make the substitution
in your example:

============Screen capture Windows 95
C:\WORK>type input.txt
D:\Images\PHOTOS\light trailing 01.JPG
D:\Images\PHOTOS\light trailing 02.JPG
D:\Images\PHOTOS\light trailing 03.JPG

C:\WORK>sed s/:/_/g input.txt>OUTPUT.TXT

C:\WORK>type output.txt
D_\Images\PHOTOS\light trailing 01.JPG
D_\Images\PHOTOS\light trailing 02.JPG
D_\Images\PHOTOS\light trailing 03.JPG

C:\WORK>
============End screen capture

SED syntax and capability varies with version. You can download
the exact version of SED I used for the above from our Usual Suspects
page (go to the third-party utilities section):
http://www.allenware.com/mcsw/bus.htm#ThirdParty

Place the SED.EXE executable from the unzipped package in a folder
in your system path, say in: C:\WINDOWS\COMMAND for Windows 95/98/ME.

SED command lines can be used in Batch files.

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/



Posted by Ted Davis on July 8th, 2005


On Thu, 7 Jul 2005 21:47:48 -0400, "KILOWATT"
<kilowatt"nospam"@softhome.net> wrote:

Gsar is a general search and replace utility - the Win32 version is a
port of the standard Unix tool. Free and open source from
<http://gnuwin32.sourceforge.net/packages/gsar.htm>.


--
T.E.D. (tdavis@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.

Posted by KILOWATT on July 8th, 2005


Hi...thanks to both of you for your replies.
I'm studying now SED. I did a quick search about the program capabilities
and among what i've read,i found the following on this site:
http://www.student.northpark.edu/pemente/sed/
"GNU sed 3.02.80, sed 3.02, sed 2.05, sed 1.18 - These are older versions of
GNU sed are considered buggy and have superseded by v4.0.7". Then i asked
myself if it exist also a version newer than 4.0.7 ? Found version 4.1.4 at
his ftp:
ftp://ftp.delorie.com/pub/djgpp/current/v2gnu/
Program itself, documentation and sourcecode for it,is also there.

Thanks again... i'll post back when i get acquainted with SED. ;-)




Posted by Dr John Stockton on July 8th, 2005


JRS: In article <9Xkze.10244$Ud.1151316@news20.bellglobal.com>, dated
Thu, 7 Jul 2005 21:47:48, seen in news:alt.msdos.batch, KILOWATT
<kilowatt@?.net> posted :


type infile.dat | SUBS ':='_ > outfile.dat

SUBS via sig line 3; SUBS.ZIP < 5 kB.


Or


type infile.dat | COLS 1 '_ 2- > outfile.dat

COLS via aig line 3; COLS.ZIP < 11 kB.



You could process the file with SED; but MiniTrue is nicer : I find
MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> ir
Garbo.



You may be able to automate it, in WordPad or Word, with a Windows macro
or keypress recording.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)