substitute perl script

Discussion in 'NZ Computing' started by Shane, Nov 27, 2005.

  1. Shane

    Shane Guest

    Ive written a crappy(tm) perl script, that searches for a string and
    replaces it with another string
    No doubt theres a better way of doing it (please if you know of one let me
    know then I can use that instead of my script)
    but.. if anyones interested .. help yrself :)

    #!/usr/bin/perl -w
    use strict;

    my @files = </usr/local/www/data/shane/*.html>;
    print "Search string : ";
    my $search = <>;
    print "Replace with : ";
    my $replace = <>;
    my $line;
    my @lines;
    my $file;
    chomp $search;
    chomp $replace;

    foreach $file(@files) {
    open (READ, "$file");
    @lines = <READ>;
    close (READ);
    print "$file\n";
    open (DELETE, ">$file");
    print DELETE "";
    close(DELETE);
    foreach $line(@lines) {
    open (EDIT, ">>$file")|| die "couldnt open $file : $!";
    ($line =~ s/$search/$replace/);
    print EDIT $line;
    close (EDIT);
    }
    }
    print "Complete";


    :)

    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Nov 27, 2005
    #1
    1. Advertising

  2. Shane wrote:
    > Ive written a crappy(tm) perl script, that searches for a string and
    > replaces it with another string
    > No doubt theres a better way of doing it (please if you know of one let me
    > know then I can use that instead of my script)


    sed

    The Other Guy
    The Other Guy, Nov 27, 2005
    #2
    1. Advertising

  3. Shane

    Shane Guest

    On Mon, 28 Nov 2005 10:16:16 +1300, The Other Guy wrote:

    > Shane wrote:
    >> Ive written a crappy(tm) perl script, that searches for a string and
    >> replaces it with another string
    >> No doubt theres a better way of doing it (please if you know of one let me
    >> know then I can use that instead of my script)

    >
    > sed
    >
    > The Other Guy


    :)
    Never thought about sed..
    find it a bit tricky I do
    how would I find 000044 in each .html, and replace it with 00005a ?
    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Nov 27, 2005
    #3
  4. Shane

    Shane Guest

    On Mon, 28 Nov 2005 10:28:07 +1300, Fran wrote:

    > Change day to night...
    >
    > sed s/day/night/ <oldfile >newfile
    >
    > Fran
    > :):):)


    ta :)
    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Nov 27, 2005
    #4
  5. Shane

    Fran Guest

    Shane wrote:

    > Ive written a crappy(tm) perl script, that searches for a string and
    > replaces it with another string
    > No doubt theres a better way of doing it (please if you know of one let me
    > know then I can use that instead of my script)
    > but.. if anyones interested .. help yrself :)
    >
    > #!/usr/bin/perl -w
    > use strict;
    >
    > my @files = </usr/local/www/data/shane/*.html>;
    > print "Search string : ";
    > my $search = <>;
    > print "Replace with : ";
    > my $replace = <>;
    > my $line;
    > my @lines;
    > my $file;
    > chomp $search;
    > chomp $replace;
    >
    > foreach $file(@files) {
    > open (READ, "$file");
    > @lines = <READ>;
    > close (READ);
    > print "$file\n";
    > open (DELETE, ">$file");
    > print DELETE "";
    > close(DELETE);
    > foreach $line(@lines) {
    > open (EDIT, ">>$file")|| die "couldnt open $file : $!";
    > ($line =~ s/$search/$replace/);
    > print EDIT $line;
    > close (EDIT);
    > }
    > }
    > print "Complete";
    >
    >
    > :)
    >


    Change day to night...

    sed s/day/night/ <oldfile >newfile

    Fran
    :):):)
    Fran, Nov 27, 2005
    #5
  6. Shane

    Allistar Guest

    Shane wrote:

    > On Mon, 28 Nov 2005 10:16:16 +1300, The Other Guy wrote:
    >
    >> Shane wrote:
    >>> Ive written a crappy(tm) perl script, that searches for a string and
    >>> replaces it with another string
    >>> No doubt theres a better way of doing it (please if you know of one let
    >>> me know then I can use that instead of my script)

    >>
    >> sed
    >>
    >> The Other Guy

    >
    > :)
    > Never thought about sed..
    > find it a bit tricky I do
    > how would I find 000044 in each .html, and replace it with 00005a ?


    sed "s/000044/00005a/g" inputFile > outputFile

    Allistar.
    Allistar, Nov 28, 2005
    #6
  7. Shane

    Shane Guest

    On Mon, 28 Nov 2005 16:23:41 +1300, Allistar wrote:

    > Shane wrote:
    >
    >> On Mon, 28 Nov 2005 10:16:16 +1300, The Other Guy wrote:
    >>
    >>> Shane wrote:
    >>>> Ive written a crappy(tm) perl script, that searches for a string and
    >>>> replaces it with another string
    >>>> No doubt theres a better way of doing it (please if you know of one let
    >>>> me know then I can use that instead of my script)
    >>>
    >>> sed
    >>>
    >>> The Other Guy

    >>
    >> :)
    >> Never thought about sed..
    >> find it a bit tricky I do
    >> how would I find 000044 in each .html, and replace it with 00005a ?

    >
    > sed "s/000044/00005a/g" inputFile > outputFile
    >
    > Allistar.


    the problem Im having isnt with the regexp...
    ah nevermind
    sed "s/find/substitute/g" *.html > *.html

    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Nov 28, 2005
    #7
  8. Shane

    Shane Guest

    On Mon, 28 Nov 2005 16:41:02 +1300, Shane wrote:

    > On Mon, 28 Nov 2005 16:23:41 +1300, Allistar wrote:
    >
    >> Shane wrote:
    >>
    >>> On Mon, 28 Nov 2005 10:16:16 +1300, The Other Guy wrote:
    >>>
    >>>> Shane wrote:
    >>>>> Ive written a crappy(tm) perl script, that searches for a string and
    >>>>> replaces it with another string
    >>>>> No doubt theres a better way of doing it (please if you know of one let
    >>>>> me know then I can use that instead of my script)
    >>>>
    >>>> sed
    >>>>
    >>>> The Other Guy
    >>>
    >>> :)
    >>> Never thought about sed..
    >>> find it a bit tricky I do
    >>> how would I find 000044 in each .html, and replace it with 00005a ?

    >>
    >> sed "s/000044/00005a/g" inputFile > outputFile
    >>
    >> Allistar.

    >
    > the problem Im having isnt with the regexp...
    > ah nevermind
    > sed "s/find/substitute/g" *.html > *.html


    strike that.. no it didnt

    the point of my script wasnt to do a simple substitution, it was to do it
    in multiple files at once (in this case every .html the shane/ directory
    on my webserver)

    Its a PITA (for me) if I go through and make a series of pages to a
    'template' of my own making, only to decide I prefer a different colour ..
    I then have to go back through (in this case 8 files) and change each file
    appropriately


    I suppose I could css :\
    but for example if I discover a spelling mistake across all the pages....

    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Nov 28, 2005
    #8
  9. Shane

    Tim Guest

    On Mon, 28 Nov 2005 16:48:14 +1300, Shane wrote:

    > the point of my script wasnt to do a simple substitution, it was to do it
    > in multiple files at once (in this case every .html the shane/ directory
    > on my webserver)


    perl -pi.bak -e "s/000044/00005a/g" /var/www/html/shane/*.html


    Will edit all the HTML files in the directory (after saving the original
    with suffix '.bak').

    -p tells perl to loop on the files, printing each line.

    If you don't want backups, drop the 'i.bak', so the command is
    perl -p -e "s/000044/00005a/g" /var/www/html/shane/*.html
    Tim, Nov 28, 2005
    #9
  10. Shane

    Fran Guest

    Shane wrote:

    > On Mon, 28 Nov 2005 10:28:07 +1300, Fran wrote:
    >
    >> Change day to night...
    >>
    >> sed s/day/night/ <oldfile >newfile
    >>
    >> Fran
    >> :):):)

    >
    > ta :)


    With multiple files do a...

    for filename in *.html
    do
    sed s/day/night/ <filename >newfile
    mv newfile filename
    done

    Hmmm.
    Would this work for multiple instances of "day" in the file?

    Fran
    :):):)
    Fran, Nov 28, 2005
    #10
  11. Shane

    Tim Guest

    On Mon, 28 Nov 2005 18:33:30 +1300, Fran wrote:

    > Shane wrote:
    >
    >> On Mon, 28 Nov 2005 10:28:07 +1300, Fran wrote:
    >>
    >>> Change day to night...
    >>>
    >>> sed s/day/night/ <oldfile >newfile
    >>>
    >>> Fran
    >>> :):):)

    >>
    >> ta :)

    >
    > With multiple files do a...


    As posted, that will type to read a file called 'filename', and NOT the
    file which is named in that variable $filename. The sed and mv lines need
    to have '$' before 'filename':


    >
    > for filename in *.html
    > do
    > sed s/day/night/ <filename >newfile

    sed 's/day/night/' <$filename > newfile
    > mv newfile filename

    mv newfile $filename
    > done
    >
    > Hmmm.
    > Would this work for multiple instances of "day" in the file?


    Add 'g' to the end of the sed script, to tell sed to look for multiple
    occurrences in the same line:
    sed 's/day/night/g' <$filename > newfile


    >
    > Fran
    > :):):)
    Tim, Nov 28, 2005
    #11
  12. Shane

    Shane Guest

    On Mon, 28 Nov 2005 18:35:45 +1300, Tim wrote:

    > On Mon, 28 Nov 2005 18:33:30 +1300, Fran wrote:
    >
    >> Shane wrote:
    >>
    >>> On Mon, 28 Nov 2005 10:28:07 +1300, Fran wrote:
    >>>
    >>>> Change day to night...
    >>>>
    >>>> sed s/day/night/ <oldfile >newfile
    >>>>
    >>>> Fran
    >>>> :):):)
    >>>
    >>> ta :)

    >>
    >> With multiple files do a...

    >
    > As posted, that will type to read a file called 'filename', and NOT the
    > file which is named in that variable $filename. The sed and mv lines need
    > to have '$' before 'filename':
    >
    >
    >>
    >> for filename in *.html
    >> do
    >> sed s/day/night/ <filename >newfile

    > sed 's/day/night/' <$filename > newfile
    >> mv newfile filename

    > mv newfile $filename
    >> done
    >>
    >> Hmmm.
    >> Would this work for multiple instances of "day" in the file?

    >
    > Add 'g' to the end of the sed script, to tell sed to look for multiple
    > occurrences in the same line:
    > sed 's/day/night/g' <$filename > newfile
    >
    >
    >>
    >> Fran
    >> :):):)


    Yeah bash script
    I forgot to make the substitution global in my script as well.
    I like the perl oneliner tim posted


    Not bad for a days work :)

    --
    Hardware, n.: The parts of a computer system that can be kicked

    The best way to get the right answer on usenet is to post the wrong one.
    Shane, Nov 28, 2005
    #12
  13. Shane

    Fran Guest

    Tim wrote:
    > Add 'g' to the end of the sed script, to tell sed to look for multiple
    > occurrences in the same line:
    > sed 's/day/night/g' <$filename > newfile


    Cool, that /g will come in handy :)

    Fran
    :):):)
    Fran, Nov 28, 2005
    #13
  14. Shane

    Enkidu Guest

    Shane wrote:
    > On Mon, 28 Nov 2005 16:41:02 +1300, Shane wrote:
    >
    >
    >>On Mon, 28 Nov 2005 16:23:41 +1300, Allistar wrote:
    >>
    >>
    >>>Shane wrote:
    >>>
    >>>
    >>>>On Mon, 28 Nov 2005 10:16:16 +1300, The Other Guy wrote:
    >>>>
    >>>>
    >>>>>Shane wrote:
    >>>>>
    >>>>>>Ive written a crappy(tm) perl script, that searches for a string and
    >>>>>>replaces it with another string
    >>>>>>No doubt theres a better way of doing it (please if you know of one let
    >>>>>>me know then I can use that instead of my script)
    >>>>>
    >>>>>sed
    >>>>>
    >>>>>The Other Guy
    >>>>
    >>>>:)
    >>>>Never thought about sed..
    >>>>find it a bit tricky I do
    >>>>how would I find 000044 in each .html, and replace it with 00005a ?
    >>>
    >>>sed "s/000044/00005a/g" inputFile > outputFile
    >>>
    >>>Allistar.

    >>
    >>the problem Im having isnt with the regexp...
    >>ah nevermind
    >>sed "s/find/substitute/g" *.html > *.html

    >
    >
    > strike that.. no it didnt
    >
    > the point of my script wasnt to do a simple substitution, it was to do it
    > in multiple files at once (in this case every .html the shane/ directory
    > on my webserver)
    >
    > Its a PITA (for me) if I go through and make a series of pages to a
    > 'template' of my own making, only to decide I prefer a different colour ..
    > I then have to go back through (in this case 8 files) and change each file
    > appropriately
    >
    >
    > I suppose I could css :\
    > but for example if I discover a spelling mistake across all the pages....
    >

    Either - use Server Side Includes
    Or - write your template(s) and then write a script to generate the
    pages from the unique information and the templates. eg have a footer
    template and a header template and a unique file and

    cat header unique footer > myfirstpage.html

    CSS is definitely the way to go though long term.

    Cheers,

    Cliff
    Enkidu, Nov 28, 2005
    #14
  15. Shane

    steve Guest

    Shane wrote:

    > Its a PITA (for me) if I go through and make a series of pages to a
    > 'template' of my own making, only to decide I prefer a different colour ..
    > I then have to go back through (in this case 8 files) and change each file
    > appropriately
    >


    style sheets.

    One change in the *.css file affects every place with the same attribute in
    all files calling that style sheet.

    Great way to maintain a consistent appearance.
    steve, Nov 28, 2005
    #15
    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. mkinsman

    NAS Perl/CGI Script

    mkinsman, Nov 23, 2003, in forum: Cisco
    Replies:
    0
    Views:
    547
    mkinsman
    Nov 23, 2003
  2. tomviolin
    Replies:
    9
    Views:
    1,067
    Richard Graves
    Apr 26, 2005
  3. Runar Furenes

    Writing a simple cgi-script with perl

    Runar Furenes, Jul 22, 2003, in forum: Computer Support
    Replies:
    1
    Views:
    783
  4. baz

    absolute address for perl script

    baz, Mar 4, 2004, in forum: Computer Support
    Replies:
    1
    Views:
    431
    Runar Furenes
    Mar 4, 2004
  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:
    670
    business one way
    Jan 5, 2008
Loading...

Share This Page