1748202 Members
2872 Online
108759 Solutions
New Discussion юеВ

Re: DOS help

 
SOLVED
Go to solution
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.