cancel
Showing results for 
Search instead for 
Did you mean: 

DOS help

SOLVED
Go to solution
Steven Chen_1
Super Advisor

DOS help

This is a repost hoping to get some quick soul helps:

---------------------
Hi,

Somehow I switch from Unix scripting to DOS batch command and need help here:

how to replace a word in a text file, such as "DEVPLOP" to "PRODUCTION", multiple times and save the file, either with original name or different name.

Unix script makes it simple, but DOS command seems to be remote to me.


Any help is appreciated!

Steven
Steve
20 REPLIES
Robert-Jan Goossens
Honored Contributor
Solution

Re: DOS help

Hi Steven,

cmd edit "open file" / "search and replace"

Robert-Jan
spex
Honored Contributor

Re: DOS help

Hi,

sed for DOS is available here:

http://www.student.northpark.edu/pemente/sed/

PCS
Chan 007
Honored Contributor

Re: DOS help

Hi,

If you are using a windows system for doing your DOS edit stuff, open the file in "notepad"

then you find under the edit menu, "replace" type in what you wanted to. Also note if you need to enable/disable the match case.

Chan
Steven Chen_1
Super Advisor

Re: DOS help

I want a batch command, not doing it manually using notepad or ed utility, such as sed or awk doing text edition in an easy way.

Any help is appreciated!
Steve
spex
Honored Contributor

Re: DOS help

Steven,

1) Download sed for DOS:
http://www.student.northpark.edu/pemente/sed/ssed360s.zip

2) Extract sed-3.59.exe to a directory in %PATH%.

3) Rename sed-3.59.exe to sed.exe.

4) cd

5) sed 's/DEVELOP/PRODUCTION/g' < textfile > textfile.new

PCS
Yogeeraj_1
Honored Contributor

Re: DOS help

hi steven,

one workaround would be to send the text file to a unix machine, do the change and get it back.

of course, it will be easy if and only if it is a once off task.

hope this helps too!
kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
A. Clay Stephenson
Acclaimed Contributor

Re: DOS help

Let me suggest a Plan B that will work in either a UNIX or a Windows environment -- Perl. You can install one of the free Perl's for Windows (e.g. www.activestate.com) and then your scripts (if well written) will work in both worlds without changes.
If it ain't broke, I can fix that.
Steven Chen_1
Super Advisor

Re: DOS help

A. Clay,

I want to try Plan B. Do you have syntac handy so that I can jump right ahead?

Thanks,

Steven
Steve
A. Clay Stephenson
Acclaimed Contributor

Re: DOS help

Sure, and this will make an automatic backup file for you named filename.save where filename is the original source file.

perl -i.save -p -e 's/DEVPLOP/PRODUCTION/g' file1 file2 ...

It will process each file listed and create a backup file1.save, file2.save, ... the modified files will be file1, file2, ...

If you don't want the backup files then omit the -i option.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: DOS help

Hi Steven:

In Perl, you could substitute the string PRODUCTION for that of DEVELOP in your file "inplace" like this:

# perl -pi.old -e 's/\bDEVELOP\b/PRODUCTION/gi' file

This substitution would occur case-insensitvely. That is, the "i" following the string PRODUCTION means to match the string DEVELOP case-insensitively, and replace every occurance ((g)lobally) with "PRODUCTION".

The "\b" is a "boundry" so that DEVELOP is replaced by PRODUCTION but not DEVELOPMENT.

The switch '-pi.old' tells Perl to print and perform an inplace edit, first preserving a a copy of 'file' as 'file.old'.

As you can otherwise see, the substitution syntax is similar to 'sed'.

If you want to begin to learn Perl, a good start would be O'Reilly's "Learning Perl (3rd Ed)" by by Randal L. Schwartz and Tom Phoenix.

Regards!

...JRF...
Rory R Hammond
Trusted Contributor

Re: DOS help

Steven.

I am not recommending this solution. But for the "old" dos edlin fans this worked on
my Windows 2000 [Version 5.00.2195] box
I could not resist showing this:

Contents of text file to fix called test.txt:
C:\>type test.txt
This is a DEVPLOP system
This is a DEVPLOP system
This is a DEVPLOP system
THIS is a DEVPLOP system

C:\>
The above file is CRLF terminated..

I then create a file called doit, with edlin using the following line commands:
C:\>edlin doit
New file
*i
1:*1RDEVPLOP^ZPRODUCTION
2:*E
3:*^Z
e

C:\>

^Z is "Ctrl"Z
I then ran the following command to change the test.txt "text":

C:\>edlin test.txt < doit
End of input file
*1RDEVPLOP^ZPRODUCTION
1:*This is a PRODUCTION system
2: This is a PRODUCTION system
3: This is a PRODUCTION system
4: THIS is a PRODUCTION system
*E

C:\>

edlin renamed lower case test.txt to upper case TEST.TXT (and left a TEST.BAK copy)

Thanks for letting me use edlin for the first time in many years...

Rory

On my box edlin can be found at
C:\WINNT\system32\edlin.exe
C:\WINNT\system32\dllcache\edlin.exe
There are a 100 ways to do things and 97 of them are right
dirk dierickx
Honored Contributor

Re: DOS help

EDLIN! Nooooooo, the memories, the horror, the end is near! save your souls!

;)
Rory R Hammond
Trusted Contributor

Re: DOS help

Dirk,

I couldn't really believe it myself. I even found edlin on my XP machine.. I truly have not used it for many years... (I do admit that I have recently used UNIX ed ...)

Do you think this revelation might be a MS plot to still our souls?


:-}

Rory


There are a 100 ways to do things and 97 of them are right
Steven Chen_1
Super Advisor

Re: DOS help

Thanks for all help!

But I cannot make words replace, either using dos sed, or perl command.

All DEVELOP words remains. Execution went through without error. Syntac is corret. Would that be the windows/dos character issue?

Steve
Peter Nikitka
Honored Contributor

Re: DOS help

Hi,

the perl will work for shure.
- Did you get any error messages?
- You can create a small file as a test case and try.
- You can give as one matching line of that file for inspection.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
A. Clay Stephenson
Acclaimed Contributor

Re: DOS help

While the PERL syntax does not change, how quotes are interpreted by the host's shell (COMMAND.COM) are different.

This should work under Windows:

perl -i.save -p -e "s/DEVPLOP/PRODUCTION/g" file1 file2 ...

Note the double vs. single quotes.

If it ain't broke, I can fix that.
Steven Chen_1
Super Advisor

Re: DOS help

A. Clay,

Thanks for the help. It works, and it happens when I try "perl -pi.old -e 's/\bDEVELOP\b/PRODUCTION/gi' file
", which failed to replace any.

Yours is simpler and working.

But everybody's efforts are very appreciated.
Steve
James R. Ferguson
Acclaimed Contributor

Re: DOS help

Hi Steven:

Consider a file with these lines:

this is DEVELOPMENT DEVELOP
DEVELOP
DEVELOPMENT

# perl -ple 's/\bDEVELOP\b/PRODUCTION/gi' file

...produces:

this is DEVELOPMENT PRODUCTION
PRODUCTION
DEVELOPMENT

...That is, "words" matching DEVELOP are replaced, not any string containing this sequence. You were not very clear defining your requiremnents.

Regards!

...JRF...


A. Clay Stephenson
Acclaimed Contributor

Re: DOS help

You should note that your problems don't stem from Perl itself but rather from the command interpreter underwhich Perl is actually running. What you should really consider is a pure Perl script that would with prompt you for target/replacement pairs and the filename(s) OR parse the command line for those values. That way the underlying command interpreter would not interfere and you would have a truly portable solution. I am not going to craft such a script (and I hope no one else does either); that should be your task and you could then post your finished solution. Your goal is not to do these dumb string replacements but rather to learn enough so that you never have to ask how to do it again.
If it ain't broke, I can fix that.
rmueller58
Valued Contributor

Re: DOS help

I'd install CYGWIN, posix utilities, active perl on the DOS/Win box and use
perl -pi -e s/DEVPLOP/PRODUCTION/g