Search-replace in multiple files?

Discussion in 'Perl Misc' started by Roger Johansson, Oct 9, 2003.

  1. I have been trying to learn more about regular expressions. I have
    tried many programs and read a lot of help files.

    I finally managed to use regexp in BK-Replace-em to change all the
    <body background...> tags in a number of html files into a plain
    <body>, for example. My first success with regexpressions!

    Search expression, replace expression:
    <[Bb][Oo][Dd][Yy][^>]*> <body>

    I learned how to use the Tcl/Tk script visual_regexp.tcl to test
    regular expressions.

    Now I want to learn how to use perl to perform the same type of tasks,
    replacing one expression with another in multiple files, for example
    in all files in a certain directory.

    I have installed ActivePerl 5.8 and have read pod files for many
    hours. But I still cannot write a program for this kind of task in
    perl. I am a total beginner in using perl.

    Could somebody please show me how to write a perl script to change a
    number of files with a regexp search and replace, like I did in
    BK-Replacem?

    I have read perlrequick and perlretut and I understand how to use
    regular expressions, but how do I handle the directory and files, and
    maybe I have to create loops to read in each file into a string,
    etc..?

    A practical example script would help a lot.

    --
    Roger J.

    (My email address is a spam trap, do not use it)
    (Use my real email address, if you know it.)
    Roger Johansson, Oct 9, 2003
    #1
    1. Advertising

  2. Roger Johansson wrote:
    > Now I want to learn how to use perl to perform the same type of
    > tasks, replacing one expression with another in multiple files, for
    > example in all files in a certain directory.
    >
    > I have installed ActivePerl 5.8 and have read pod files for many
    > hours. But I still cannot write a program for this kind of task in
    > perl. I am a total beginner in using perl.


    http://learn.perl.org/

    > but how do I handle the directory and files, and maybe I have to
    > create loops to read in each file into a string, etc..?


    Something like that.

    http://learn.perl.org/

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Oct 9, 2003
    #2
    1. Advertising

  3. Roger Johansson

    ko Guest

    Roger Johansson wrote:

    [snip]
    > Now I want to learn how to use perl to perform the same type of tasks,
    > replacing one expression with another in multiple files, for example
    > in all files in a certain directory.


    If its a simple search/replace, you can do it with a one-liner from your
    shell:

    *nix:
    perl -p -i.orig -e 's/SEARCH/REPLACE/g' *.html

    WINDOWS (wrapped, but should be on one line):
    perl -p -i.orig -e "BEGIN {@ARGV = map { glob } @ARGV }
    s/SEARCH/REPLACE/g" *.html

    These commands will do replacement on HTML files in the current
    directory. In the second example, you need the BEGIN block because the
    Windows shell doesn't do wildcard expansion. 'perlrun' will give you a
    better idea of what you can do with the -i, -p, and -e command line
    switches.

    [snip]

    > I have read perlrequick and perlretut and I understand how to use
    > regular expressions, but how do I handle the directory and files, and
    > maybe I have to create loops to read in each file into a string,
    > etc..?


    'perlsyn' will help you understand loops, among other things. The
    perlfaqs are all good if you haven't read them already - 'perlfaq5' will
    help with reading files, etc. No offense, but it usually takes a while
    (messing around with them) to *really* understand regular expressions.
    I've read 'perlrequick' and 'perlretut' a few times, and still have a
    way to go...

    HTH - keith
    ko, Oct 9, 2003
    #3
  4. ko <> wrote:

    >If its a simple search/replace, you can do it with a one-liner from your
    >shell:


    >WINDOWS (wrapped, but should be on one line):
    >perl -p -i.orig -e "BEGIN {@ARGV = map { glob } @ARGV }
    >s/SEARCH/REPLACE/g" *.html
    >
    >These commands will do replacement on HTML files in the current
    >directory. In the second example, you need the BEGIN block because the
    >Windows shell doesn't do wildcard expansion. 'perlrun' will give you a
    >better idea of what you can do with the -i, -p, and -e command line
    >switches.


    Thanks. It works.

    I had some practical problems because a DOS Prompt window does not
    allow copy and paste from the windows clipboard, so I would have to
    write everything manually in the DOS window.
    I had to change autoexec.bat to get my swedish keyboard to work in DOS
    windows.

    I finally found a way to do it in my file handler Total Commander, and
    that worked, but it would be nice to have a DOS console program which
    was more useful than the DOS Prompt window. How do you perl
    programmers solve that problem? Is there a DOS console program you can
    use to work in which allows copying and pasting from the windows
    environment?
    A window which doesn't close every time a script has ended, and with
    better background color, better font, etc..

    I tried to put your line of code into a .pl script, like this:

    #! ./perl
    #
    # search and replace in all html files in current directory

    use strict;
    use warnings;

    {
    perl -p -i.orig -e "BEGIN {@ARGV = map { glob } @ARGV }
    s/SEARCH/REPLACE/g" *.html
    }

    {
    print "Press [ ENTER ] to continue\n";
    <STDIN>;
    }

    But it didn't work. I probably made some stupid mistake.
    (I unwrapped the long line, so it is not that problem)

    I found several online books on learn.perl.org but I haven't had time
    to read them yet. I will tonight :)


    --
    Roger J.

    (My email address is a spam trap, do not use it)
    Roger Johansson, Oct 9, 2003
    #4
  5. Roger Johansson <> wrote:
    > ko <> wrote:
    >
    >>If its a simple search/replace, you can do it with a one-liner from your
    >>shell:

    >
    >>WINDOWS (wrapped, but should be on one line):
    >>perl -p -i.orig -e "BEGIN {@ARGV = map { glob } @ARGV }
    >>s/SEARCH/REPLACE/g" *.html


    > I tried to put your line of code into a .pl script, like this:
    >
    > #! ./perl
    > #
    > # search and replace in all html files in current directory
    >
    > use strict;
    > use warnings;
    >
    > {
    > perl -p -i.orig -e "BEGIN {@ARGV = map { glob } @ARGV }
    > s/SEARCH/REPLACE/g" *.html
    > }
    >
    > {
    > print "Press [ ENTER ] to continue\n";
    > <STDIN>;
    > }
    >
    > But it didn't work.



    The switches go on the shebang (#!) line.

    #!perl -p -i.orig
    BEGIN {@ARGV = map { glob } @ARGV }
    s/SEARCH/REPLACE/g;

    or, you read perlrun.pod to see what the switches do for you,
    then write code that does it yourself:

    #!perl
    BEGIN {@ARGV = map { glob } @ARGV }
    $^I = '.orig'; # turn on in-place editing
    while ( <> ) {
    s/SEARCH/REPLACE/g;
    print;
    }


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 9, 2003
    #5
  6. Roger Johansson

    Bob Walton Guest

    Roger Johansson wrote:

    > ko <> wrote:
    >

    ....
    > I had some practical problems because a DOS Prompt window does not
    > allow copy and paste from the windows clipboard, so I would have to



    <offtopic>
    Sure it does. Right-click the DOS window's title bar and select edit,
    then paste. To go the other way easily, modify the properties of the
    DOS window, checking the box "quick edit" on the "misc" tab. Then
    highlight a rectangular region with your mouse and push the enter key.
    Some of that might be dependent upon what version of Windoze you have --
    that stuff works for Windoze 98SE and NT 4 sp 6A at least; I don't know
    about the rest.

    </offtopic>


    > write everything manually in the DOS window.
    > I had to change autoexec.bat to get my swedish keyboard to work in DOS
    > windows.



    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Oct 10, 2003
    #6
    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. François Robert
    Replies:
    3
    Views:
    678
    Toni Uusitalo
    Jul 19, 2005
  2. Replies:
    4
    Views:
    939
    M.E.Farmer
    Feb 13, 2005
  3. RajaniKumarV
    Replies:
    0
    Views:
    453
    RajaniKumarV
    Feb 22, 2010
  4. Abby Lee
    Replies:
    5
    Views:
    382
    Abby Lee
    Aug 2, 2004
  5. Trans
    Replies:
    2
    Views:
    108
    William James
    Apr 22, 2006
Loading...

Share This Page