Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Computing > Computer Support > using perl as a find/replace

Reply
Thread Tools

using perl as a find/replace

 
 
Ben Wylie
Guest
Posts: n/a
 
      04-15-2005
I'm having trouble with perl and making a find/replace script.

I have a text file of domain names, and would like to convert them to a text
file of perl regular expressions using a perl script, in windows.
eg
domain1.com
domain2.com
subdomain.domain3.com

I'd like to convert to:
(^|\.)domain1\.com$
(^|\.)domain2\.com$
(^|\.)subdomain\.domain3\.com$

I think that the perl script would include:
$string =~ s!\.!\\\.!g;
$string =~ s!^!!^\(\^\|\\\.\)!g;
$string =~ s!$!$\$!g;

I'd like to be able to run:
>c:\perl\bin perl findreplace.pl "c:\documents and settings\me\my

documents\domains.txt"
to do all the find/replaces on the file domains.txt
and also be able to do
>c:\perl\bin perl findreplace.pl "c:\documents and settings\me\my

documents\toconvert\"
to convert all the files in the folder "toconvert" with the find replace
rules.

I gather you use the @argv to use command line arguments, but am not sure
how to import the file or how to export the ammended file.

Thanks for your help

Ben


 
Reply With Quote
 
 
 
 
Ben Wylie
Guest
Posts: n/a
 
      04-15-2005
I've tried something like:

#!F:\Perl\bin\perl.exe -w
open INPUT, "< f:/squid/etc/squid.adverts" or die "can't read in.txt: $!";
open OUTPUT, "> f:/squid/etc/squid.adverts.txt" or die "can't write out.txt:
$!";
while (my $line = <INPUT>) {
$line =~ s!\.!\\\.!g;
$line =~ s!^!^\(\^\|\\\.\)!g;
$line =~ s!$!\$!g;
print OUTPUT $line;
}
close OUTPUT;
close INPUT;

but it doesn't seem able to open the file

Any ideas?
Ben

Ben Wylie wrote:
> I'm having trouble with perl and making a find/replace script.
>
> I have a text file of domain names, and would like to convert them to
> a text file of perl regular expressions using a perl script, in
> windows.
> eg
> domain1.com
> domain2.com
> subdomain.domain3.com
>
> I'd like to convert to:
> (^|\.)domain1\.com$
> (^|\.)domain2\.com$
> (^|\.)subdomain\.domain3\.com$
>
> I think that the perl script would include:
> $string =~ s!\.!\\\.!g;
> $string =~ s!^!!^\(\^\|\\\.\)!g;
> $string =~ s!$!$\$!g;
>
> I'd like to be able to run:
>> c:\perl\bin perl findreplace.pl "c:\documents and settings\me\my

> documents\domains.txt"
> to do all the find/replaces on the file domains.txt
> and also be able to do
>> c:\perl\bin perl findreplace.pl "c:\documents and settings\me\my

> documents\toconvert\"
> to convert all the files in the folder "toconvert" with the find
> replace rules.
>
> I gather you use the @argv to use command line arguments, but am not
> sure how to import the file or how to export the ammended file.
>
> Thanks for your help
>
> Ben



 
Reply With Quote
 
 
 
 
why?
Guest
Posts: n/a
 
      04-15-2005

On Fri, 15 Apr 2005 00:29:33 GMT, Ben Wylie wrote:

>I'm having trouble with perl and making a find/replace script.
>
>I have a text file of domain names, and would like to convert them to a text
>file of perl regular expressions using a perl script, in windows.
>eg
>domain1.com
>domain2.com
>subdomain.domain3.com


There are 100's of tutorial sites for Perl, regexes, try www.google.com

There are also many text manipulation modules / find and replsces
already written.

If you are going to start using rexexes for the fun they provide , a
must have book is
Mastering Regular Expressions
Jeffrey E.F. Friedl
publisher - O'Reilly
isbn - 1-56592-257-3

<snip>

>I gather you use the @argv to use command line arguments, but am not sure
>how to import the file or how to export the ammended file.


See the many tutorial sites, and back to the O'Reilly books,
The Lama, the Camel (picture on the front of the books) and The Perl
Cookbook.

All excellent references and guides.

>Thanks for your help
>
>Ben
>


Me
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 04-04-2011 10:00 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments