RE: Using regular expressions to replace file text?

2004-12-22 Thread Arms, Mike
[Decided to top-post this time since Matt is.]

Under Win32, you need to set binmode() on binary files:

  my $file = 'myImage.jpg';
  # Opening binary file for read & write.
  open( IMAGE, "+<$file" ) or die "*** Trouble opening '$file' : $!\n";
  binmode( IMAGE );

I would caution you though that you are always safer opening
one file in Read mode and another file descriptor in Write mode.
Then after you are done writing to the new file, close the
file handles, then delete the old file, and rename the new
file to the old file's name. This prevents various nastiness
when in a multi-processing environment. And you don't clobber
your original file until you have completely finished writing
your replacement file which protects you from your own logic
errors. Just a suggestion.

  while ( @ARGV )
  {
my $out = "$_.NEW";
open( IN, $_ ) or die "*** Trouble opening '$_' : $!\n";
open( OUT, ">$out" ) or die "*** Trouble creating '$out' : $!\n";
binmode( IN );
binmode( OUT );
# ...
close( OUT );
close( IN );
unlink $_;
rename $out, $_;
  }

--
Mike Arms


> -Original Message-
> Matt Harp wrote:
> 
> Thanks much for the info.
> 
> One question though...
> 
> I'm trying Perl because using the ReplaceRegExp stuff from Ant script
> was corrupting my binary files. Now that I got Perl working, 
> I find out
> that it's corrupting my binary files too :(
> 
> I'm trying to do this stuff on a cvs ,v archive file that represents a
> binary file (a .jpg for my testing). Doing a search/replce from my Vim
> editor or from CodeWright works great and doesn't corrupt the 
> file. But
> running that Perl command on that ,v file ends up with a 
> corrupted image
> file when I do a Checkout from CVS.
> 
> Are there special flags to tell Perl to stop corrupting my 
> binary files?
> Am I hosed, and should just stick to do it from an editor?
> 
> Thanks,
> 
> -matt
> mharp AT seapine DOT com
> 
>  
> 
> > -Original Message-
> > Mike Arms [marms AT sandia DOT gov] wrote:
> > 
> > Matt Harp
> > > Thanks Mike!
> > > 
> > > I had tried single quotes but didn't think to try double quotes.
> > > 
> > > I should probably be banned from this mailing list for such 
> > stupidity.
> > > 
> > > but Ok, I'll push my luck... Any hints on how to make 
> this a script 
> > > that'll let me pass *.*?
> > > 
> > > I seem to remember a reference to a way to tell it to work 
> > on multiple 
> > > files from the cmd-line but can't seem to find the forum post any 
> > > longer.
> > > 
> > > Something like -file (name=*.*)? Or something...
> > > 
> > > -matt
> > > mharp AT seapine DOT com
> > > 
> > >
> > > > Mike Arms [marms AT sandia DOT gov]
> > > > 
> > > > Matt Harp wrote:
> > > > > This has to be an easy question to answer, but I've been
> > > > looking and
> > > > > hacking for a day now and can't figure it out.
> > > > >  
> > > > > I want to just do search/replace on a set of files 
> > using regular 
> > > > > expressions.
> > > > >  
> > > > > I have ActiveState 5.8.6 installed on WinXP, if that matters.
> > > > >  
> > > > > I am trying it like this...
> > > > >  
> > > > > perl -i.bak -pe s/^ext$/HARP/m fred.txt
> > > > >  
> > > > > Perl help says that using m should result in ^,$ matching 
> > > > > beginning of line, end of line, but that's not what 
> > this does. It 
> > > > > matches any 'ext' string and replaces it with HARP.
> > > > > I've tried \n instead also, but then that doesn't replace 
> > > > > anything.
> > > > >  
> > > > > My second question is of course, how to make this into 
> > a script so 
> > > > > I can pass *.txt. I've found a couple scripts but 
> none of them 
> > > > > will even run w/o giving me errors.
> > > > >  
> > > > > Any help on either issue would be immensley 
> > appreciated. I can't 
> > > > > take any more of this...
> > > > >  
> > > > > Regards,
> > > > >  
> > > > > -matt
> > > > > mharp AT seapine DOT com
> > > >  
> > > > I'm guessing that the issue is that you need to put the script 
> > > > portion in quotes:
> > > > 
> > > >   perl -i.bak -pe "s/^ext$/HARP/m" fred.txt
> > > > 
> > > > Try that and let us know.
> > > > 
> > > > --
> > > > Mike Arms
> > 
> > Single quote work great in Unix/Linux. In fact I prefer them 
> > there as they prevent the "$" for being interpreted as an 
> > Environment variable reference.
> > 
> > But under Win32, single quotes do not work. You have to use 
> > double quote. Fortunately, it does not try to do any command 
> > line substitutions. By the way, if you need to use double 
> > quotes in your script, I recommend using the qq() 
> construct. Example:
> > 
> >   perl -e "$i='foo'; print qq(This is a $i test\n);"
> > 
> > On to your multiple files question, you may want Jenda 
> > Krynicky's "G" module:
> > 
> >   http://jenda.krynicky.cz/#G
> > 
> > then try:
> > 
> >   perl -MG -i.bak -e "while (<>){ s/(^ext$/HARP/m; print; }" *.txt
> > 
> > or recursive even: 
> > 
> >   perl -MG=R -i.bak -e "while 

RE: Using regular expressions to replace file text?

2004-12-22 Thread Matt Harp
Thanks much for the info.

One question though...

I'm trying Perl because using the ReplaceRegExp stuff from Ant script
was corrupting my binary files. Now that I got Perl working, I find out
that it's corrupting my binary files too :(

I'm trying to do this stuff on a cvs ,v archive file that represents a
binary file (a .jpg for my testing). Doing a search/replce from my Vim
editor or from CodeWright works great and doesn't corrupt the file. But
running that Perl command on that ,v file ends up with a corrupted image
file when I do a Checkout from CVS.

Are there special flags to tell Perl to stop corrupting my binary files?
Am I hosed, and should just stick to do it from an editor?

Thanks,

-matt
[EMAIL PROTECTED]

 

> -Original Message-
> From: Arms, Mike [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, December 22, 2004 6:17 PM
> To: Matt Harp; perl-win32-users@listserv.ActiveState.com
> Subject: RE: Using regular expressions to replace file text?
> 
> Matt Harp
> > Thanks Mike!
> > 
> > I had tried single quotes but didn't think to try double quotes.
> > 
> > I should probably be banned from this mailing list for such 
> stupidity.
> > 
> > but Ok, I'll push my luck... Any hints on how to make this a script 
> > that'll let me pass *.*?
> > 
> > I seem to remember a reference to a way to tell it to work 
> on multiple 
> > files from the cmd-line but can't seem to find the forum post any 
> > longer.
> > 
> > Something like -file (name=*.*)? Or something...
> > 
> > -matt
> > mharp AT seapine DOT com
> > 
> >
> > >Arms, Mike [marms AT sandia DOT gov]
> > > Sent: Wednesday, December 22, 2004 5:40 PM
> > > To: Matt Harp; perl-win32-users@listserv.ActiveState.com
> > > Subject: RE: Using regular expressions to replace file text?
> > > 
> > > Matt Harp wrote:
> > > > This has to be an easy question to answer, but I've been
> > > looking and
> > > > hacking for a day now and can't figure it out.
> > > >  
> > > > I want to just do search/replace on a set of files 
> using regular 
> > > > expressions.
> > > >  
> > > > I have ActiveState 5.8.6 installed on WinXP, if that matters.
> > > >  
> > > > I am trying it like this...
> > > >  
> > > > perl -i.bak -pe s/^ext$/HARP/m fred.txt
> > > >  
> > > > Perl help says that using m should result in ^,$ matching 
> > > > beginning of line, end of line, but that's not what 
> this does. It 
> > > > matches any 'ext' string and replaces it with HARP.
> > > > I've tried \n instead also, but then that doesn't replace 
> > > > anything.
> > > >  
> > > > My second question is of course, how to make this into 
> a script so 
> > > > I can pass *.txt. I've found a couple scripts but none of them 
> > > > will even run w/o giving me errors.
> > > >  
> > > > Any help on either issue would be immensley 
> appreciated. I can't 
> > > > take any more of this...
> > > >  
> > > > Regards,
> > > >  
> > > > -matt
> > > > mharp AT seapine DOT com
> > >  
> > > I'm guessing that the issue is that you need to put the script 
> > > portion in quotes:
> > > 
> > >   perl -i.bak -pe "s/^ext$/HARP/m" fred.txt
> > > 
> > > Try that and let us know.
> > > 
> > > --
> > > Mike Arms
> 
> Single quote work great in Unix/Linux. In fact I prefer them 
> there as they prevent the "$" for being interpreted as an 
> Environment variable reference.
> 
> But under Win32, single quotes do not work. You have to use 
> double quote. Fortunately, it does not try to do any command 
> line substitutions. By the way, if you need to use double 
> quotes in your script, I recommend using the qq() construct. Example:
> 
>   perl -e "$i='foo'; print qq(This is a $i test\n);"
> 
> On to your multiple files question, you may want Jenda 
> Krynicky's "G" module:
> 
>   http://jenda.krynicky.cz/#G
> 
> then try:
> 
>   perl -MG -i.bak -e "while (<>){ s/(^ext$/HARP/m; print; }" *.txt
> 
> or recursive even: 
> 
>   perl -MG=R -i.bak -e "while (<>){ s/(^ext$/HARP/m; print; }" *.txt
> 
> The globbing wouldn't be an issue if you were on Linux/Unix. :-)
> 
> --
> Mike Arms
> 
> 
> 

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Using regular expressions to replace file text?

2004-12-22 Thread Arms, Mike
Matt Harp
> Thanks Mike!
> 
> I had tried single quotes but didn't think to try double quotes.
> 
> I should probably be banned from this mailing list for such stupidity.
> 
> but Ok, I'll push my luck... Any hints on how to make this a script
> that'll let me pass *.*?
> 
> I seem to remember a reference to a way to tell it to work on multiple
> files from the cmd-line but can't seem to find the forum post any
> longer.
> 
> Something like -file (name=*.*)? Or something...
> 
> -matt
> mharp AT seapine DOT com
> 
>
> >Arms, Mike [marms AT sandia DOT gov] 
> > Sent: Wednesday, December 22, 2004 5:40 PM
> > To: Matt Harp; perl-win32-users@listserv.ActiveState.com
> > Subject: RE: Using regular expressions to replace file text?
> > 
> > Matt Harp wrote:
> > > This has to be an easy question to answer, but I've been 
> > looking and 
> > > hacking for a day now and can't figure it out.
> > >  
> > > I want to just do search/replace on a set of files using regular 
> > > expressions.
> > >  
> > > I have ActiveState 5.8.6 installed on WinXP, if that matters.
> > >  
> > > I am trying it like this...
> > >  
> > > perl -i.bak -pe s/^ext$/HARP/m fred.txt
> > >  
> > > Perl help says that using m should result in ^,$ matching 
> > > beginning of line, end of line, but that's not what this
> > > does. It matches any 'ext' string and replaces it with HARP.
> > > I've tried \n instead also, but then that doesn't replace 
> > > anything.
> > >  
> > > My second question is of course, how to make this into a 
> > > script so I can pass *.txt. I've found a couple scripts but
> > > none of them will even run w/o giving me errors.
> > >  
> > > Any help on either issue would be immensley appreciated. I 
> > > can't take any more of this...
> > >  
> > > Regards,
> > >  
> > > -matt
> > > mharp AT seapine DOT com
> >  
> > I'm guessing that the issue is that you need to put the 
> > script portion in quotes:
> > 
> >   perl -i.bak -pe "s/^ext$/HARP/m" fred.txt
> > 
> > Try that and let us know.
> > 
> > --
> > Mike Arms

Single quote work great in Unix/Linux. In fact I prefer them there
as they prevent the "$" for being interpreted as an Environment
variable reference.

But under Win32, single quotes do not work. You have to use
double quote. Fortunately, it does not try to do any command
line substitutions. By the way, if you need to use double quotes
in your script, I recommend using the qq() construct. Example:

  perl -e "$i='foo'; print qq(This is a $i test\n);"

On to your multiple files question, you may want Jenda Krynicky's
"G" module:

  http://jenda.krynicky.cz/#G

then try:

  perl -MG -i.bak -e "while (<>){ s/(^ext$/HARP/m; print; }" *.txt

or recursive even: 

  perl -MG=R -i.bak -e "while (<>){ s/(^ext$/HARP/m; print; }" *.txt

The globbing wouldn't be an issue if you were on Linux/Unix. :-)

--
Mike Arms



___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Using regular expressions to replace file text?

2004-12-22 Thread $Bill Luebkert
Matt Harp wrote:

> Thanks Mike!
> 
> I had tried single quotes but didn't think to try double quotes.
> 
> I should probably be banned from this mailing list for such stupidity.
> 
> but Ok, I'll push my luck... Any hints on how to make this a script
> that'll let me pass *.*?
> 
> I seem to remember a reference to a way to tell it to work on multiple
> files from the cmd-line but can't seem to find the forum post any
> longer.
> 
> Something like -file (name=*.*)? Or something...

Just noticed a module that may help - it seems to work.

Win32-Autoglob  [1.01   ] expand globs in @ARGV when the shell
  doesn't

Otherwise you can use glob, File::Glob or File::DOS::Glob to handle the *.txt

>>From: Arms, Mike [mailto:[EMAIL PROTECTED] 

>>Matt Harp wrote:
>>
>>>This has to be an easy question to answer, but I've been 
>>
>>looking and 
>>
>>>hacking for a day now and can't figure it out.
>>> 
>>>I want to just do search/replace on a set of files using regular 
>>>expressions.
>>> 
>>>I have ActiveState 5.8.6 installed on WinXP, if that matters.
>>> 
>>>I am trying it like this...
>>> 
>>>perl -i.bak -pe s/^ext$/HARP/m fred.txt
>>> 
>>>Perl help says that using m should result in ^,$ matching 
>>
>>beginning of 
>>
>>>line, end of line, but that's not what this does. It 
>>
>>matches any 'ext' 
>>
>>>string and replaces it with HARP.
>>>I've tried \n instead also, but then that doesn't replace anything.
>>> 
>>>My second question is of course, how to make this into a 
>>
>>script so I 
>>
>>>can pass *.txt. I've found a couple scripts but none of 
>>
>>them will even 
>>
>>>run w/o giving me errors.
>>> 
>>>Any help on either issue would be immensley appreciated. I 
>>
>>can't take 
>>
>>>any more of this...
>>> 
>>> 
>>>Regards,
>>> 
>>>-matt <[EMAIL PROTECTED]>
>>>[EMAIL PROTECTED]
>>
>> 
>>I'm guessing that the issue is that you need to put the 
>>script portion in quotes:
>>
>>  perl -i.bak -pe "s/^ext$/HARP/m" fred.txt
>>
>>Try that and let us know.


-- 
  ,-/-  __  _  _ $Bill LuebkertMailto:[EMAIL PROTECTED]
 (_/   /  )// //   DBE CollectiblesMailto:[EMAIL PROTECTED]
  / ) /--<  o // //  Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Using regular expressions to replace file text?

2004-12-22 Thread Matt Harp
Thanks Mike!

I had tried single quotes but didn't think to try double quotes.

I should probably be banned from this mailing list for such stupidity.

but Ok, I'll push my luck... Any hints on how to make this a script
that'll let me pass *.*?

I seem to remember a reference to a way to tell it to work on multiple
files from the cmd-line but can't seem to find the forum post any
longer.

Something like -file (name=*.*)? Or something...

-matt
[EMAIL PROTECTED]

 

> -Original Message-
> From: Arms, Mike [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, December 22, 2004 5:40 PM
> To: Matt Harp; perl-win32-users@listserv.ActiveState.com
> Subject: RE: Using regular expressions to replace file text?
> 
> Matt Harp wrote:
> > This has to be an easy question to answer, but I've been 
> looking and 
> > hacking for a day now and can't figure it out.
> >  
> > I want to just do search/replace on a set of files using regular 
> > expressions.
> >  
> > I have ActiveState 5.8.6 installed on WinXP, if that matters.
> >  
> > I am trying it like this...
> >  
> > perl -i.bak -pe s/^ext$/HARP/m fred.txt
> >  
> > Perl help says that using m should result in ^,$ matching 
> beginning of 
> > line, end of line, but that's not what this does. It 
> matches any 'ext' 
> > string and replaces it with HARP.
> > I've tried \n instead also, but then that doesn't replace anything.
> >  
> > My second question is of course, how to make this into a 
> script so I 
> > can pass *.txt. I've found a couple scripts but none of 
> them will even 
> > run w/o giving me errors.
> >  
> > Any help on either issue would be immensley appreciated. I 
> can't take 
> > any more of this...
> >  
> >  
> > Regards,
> >  
> > -matt <[EMAIL PROTECTED]>
> > [EMAIL PROTECTED]
>  
> I'm guessing that the issue is that you need to put the 
> script portion in quotes:
> 
>   perl -i.bak -pe "s/^ext$/HARP/m" fred.txt
> 
> Try that and let us know.
> 
> --
> Mike Arms
> 
> 

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Using regular expressions to replace file text?

2004-12-22 Thread Arms, Mike
Matt Harp wrote:
> This has to be an easy question to answer, but I've been 
> looking and hacking for a day now and can't figure it out.
>  
> I want to just do search/replace on a set of files using 
> regular expressions.
>  
> I have ActiveState 5.8.6 installed on WinXP, if that matters.
>  
> I am trying it like this...
>  
> perl -i.bak -pe s/^ext$/HARP/m fred.txt
>  
> Perl help says that using m should result in ^,$ matching 
> beginning of line, end of line, but that's not what this 
> does. It matches any 'ext' string and replaces it with HARP. 
> I've tried \n instead also, but then that doesn't replace anything.
>  
> My second question is of course, how to make this into a 
> script so I can pass *.txt. I've found a couple scripts but 
> none of them will even run w/o giving me errors.
>  
> Any help on either issue would be immensley appreciated. I 
> can't take any more of this...
>  
>  
> Regards,
>  
> -matt <[EMAIL PROTECTED]> 
> [EMAIL PROTECTED]
 
I'm guessing that the issue is that you need to put the script 
portion in quotes:

  perl -i.bak -pe "s/^ext$/HARP/m" fred.txt

Try that and let us know.

--
Mike Arms


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs