using perl as a find/replace

Discussion in 'Computer Support' started by Ben Wylie, Apr 15, 2005.

  1. Ben Wylie

    Ben Wylie Guest

    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
     
    Ben Wylie, Apr 15, 2005
    #1
    1. Advertising

  2. Ben Wylie

    Ben Wylie Guest

    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
     
    Ben Wylie, Apr 15, 2005
    #2
    1. Advertising

  3. Ben Wylie

    why? Guest

    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
     
    why?, Apr 15, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    DW Crashing Constantly When Using Find & Replace?

    John, May 23, 2004, in forum: Computer Information
    Replies:
    16
    Views:
    634
  2. Hendrik Maryns

    Find and replace in forms

    Hendrik Maryns, Nov 1, 2006, in forum: Firefox
    Replies:
    0
    Views:
    424
    Hendrik Maryns
    Nov 1, 2006
  3. "find and replace" a graphic in an image?

    , Sep 18, 2005, in forum: Digital Photography
    Replies:
    1
    Views:
    320
    Paul H.
    Sep 20, 2005
  4. PeperG
    Replies:
    3
    Views:
    1,867
    PeperG
    Feb 8, 2007
  5. business one way

    Is it a Perl program or a Perl script?

    business one way, Jan 5, 2008, in forum: Digital Photography
    Replies:
    0
    Views:
    776
    business one way
    Jan 5, 2008
Loading...

Share This Page