Help converting sed script

Discussion in 'Perl Misc' started by lists@lastisfirst.org, Jun 25, 2005.

  1. Guest

    Howdy --
    What I'm looking for is a perl cript that goes step threw a directoy
    tree and do a sed style global string replacement on every file( with
    say an extension of *.php) here is the sed script I've been using:


    s/HTTP_GET_VARS/_GET/g
    s/HTTP_POST_VARS/_POST/g
    s/HTTP_SERVER_VARS/_SERVER/g
    s/HTTP_COOKIE_VARS/_COOKIE/g
    s/HTTP_SESSION_VARS/_SESSION/g


    TiA,
    David Jackson
    , Jun 25, 2005
    #1
    1. Advertising

  2. <> wrote:

    > Subject: Help converting sed script



    Run your sed program through the "s2p" sed-to-perl translator that
    comes with the perl distribution.


    > here is the sed script I've been using:


    [snip sed]


    But where is the Perl version that you've been working on that
    you need help with?

    If you show us your broken Perl code, we can probably help you fix it...



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 25, 2005
    #2
    1. Advertising

  3. Joe Smith Guest

    wrote:
    > Howdy --
    > What I'm looking for is a perl cript that goes step threw a directoy
    > tree and do a sed style global string replacement on every file


    1) Write a simple perl script that takes the name of a file as a
    command line argument, opens the file, opens a suitable output
    file, reads the input a line at a time and makes the string
    substitution before writing $_ to the output file.

    2) Make that first script handle multiple files on the command line.

    3) Learn about glob() and/or readdir() if you will be providing
    the names of directories on the command line.

    4) Use File::Find if you are intending to process all subdirectories.

    If you want help from us, you need to show good faith, by actually
    attempting that first step on your own. Good luck.

    -Joe

    P.S. The equivalent of sed's
    s/HTTP_GET_VARS/_GET/g
    in perl is
    s/HTTP_GET_VARS/_GET/g;
    Joe Smith, Jun 26, 2005
    #3
  4. Guest

    Thanks for you replies.
    Perl Version 5.8.0
    >
    > Run your sed program through the "s2p" sed-to-perl translator that
    > comes with the perl distribution.
    >

    I actually had already run s2p on sed script, does it really take a 139
    line Perl script to replace a 4 line sed script?

    Thanks again,
    David




    ------- Here's the s2p script -------------



    #!/usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if 0;
    $0 =~ s/^.*?(\w+)[\.\w+]*$/$1/;

    use strict;
    use Symbol;
    use vars qw{ $isEOF $Hold %wFiles @Q $CondReg
    $doAutoPrint $doOpenWrite $doPrint };
    $doAutoPrint = 1;
    $doOpenWrite = 1;
    # prototypes
    sub openARGV();
    sub getsARGV(;\$);
    sub eofARGV();
    sub printQ();

    # Run: the sed loop reading input and applying the script
    #
    sub Run(){
    my( $h, $icnt, $s, $n );
    # hack (not unbreakable :-/) to avoid // matching an empty string
    my $z = "\000"; $z =~ /$z/;
    # Initialize.
    openARGV();
    $Hold = '';
    $CondReg = 0;
    $doPrint = $doAutoPrint;
    CYCLE:
    while( getsARGV() ){
    chomp();
    $CondReg = 0; # cleared on t
    BOS:;
    # #!/usr/bin/sed -i -e
    # s/HTTP_GET_VARS/_GET/g
    { $s = s /HTTP_GET_VARS/_GET/sg;
    $CondReg ||= $s;
    }
    # s/HTTP_POST_VARS/_POST/g
    { $s = s /HTTP_POST_VARS/_POST/sg;
    $CondReg ||= $s;
    }
    # s/HTTP_SERVER_VARS/_SERVER/g
    { $s = s /HTTP_SERVER_VARS/_SERVER/sg;
    $CondReg ||= $s;
    }
    # s/HTTP_COOKIE_VARS/_COOKIE/g
    { $s = s /HTTP_COOKIE_VARS/_COOKIE/sg;
    $CondReg ||= $s;
    }
    # s/HTTP_SESSION_VARS/_SESSION/g
    { $s = s /HTTP_SESSION_VARS/_SESSION/sg;
    $CondReg ||= $s;
    }
    EOS: if( $doPrint ){
    print $_, "\n";
    } else {
    $doPrint = $doAutoPrint;
    }
    printQ() if @Q;
    }

    exit( 0 );
    }
    Run();

    # openARGV: open 1st input file
    #
    sub openARGV(){
    unshift( @ARGV, '-' ) unless @ARGV;
    my $file = shift( @ARGV );
    open( ARG, "<$file" )
    || die( "$0: can't open $file for reading ($!)\n" );
    $isEOF = 0;
    }

    # getsARGV: Read another input line into argument (default: $_).
    # Move on to next input file, and reset EOF flag $isEOF.
    sub getsARGV(;\$){
    my $argref = @_ ? shift() : \$_;
    while( $isEOF || ! defined( $$argref = <ARG> ) ){
    close( ARG );
    return 0 unless @ARGV;
    my $file = shift( @ARGV );
    open( ARG, "<$file" )
    || die( "$0: can't open $file for reading ($!)\n" );
    $isEOF = 0;
    }
    1;
    }

    # eofARGV: end-of-file test
    #
    sub eofARGV(){
    return @ARGV == 0 && ( $isEOF = eof( ARG ) );
    }

    # makeHandle: Generates another file handle for some file (given by its
    path)
    # to be written due to a w command or an s command's w
    flag.
    sub makeHandle($){
    my( $path ) = @_;
    my $handle;
    if( ! exists( $wFiles{$path} ) || $wFiles{$path} eq '' ){
    $handle = $wFiles{$path} = gensym();
    if( $doOpenWrite ){
    if( ! open( $handle, ">$path" ) ){
    die( "$0: can't open $path for writing: ($!)\n" );
    }
    }
    } else {
    $handle = $wFiles{$path};
    }
    return $handle;
    }

    # printQ: Print queued output which is either a string or a reference
    # to a pathname.
    sub printQ(){
    for my $q ( @Q ){
    if( ref( $q ) ){
    # flush open w files so that reading this file gets it all
    if( exists( $wFiles{$$q} ) && $wFiles{$$q} ne '' ){
    open( $wFiles{$$q}, ">>$$q" );
    }
    # copy file to stdout: slow, but safe
    if( open( RF, "<$$q" ) ){
    while( defined( my $line = <RF> ) ){
    print $line;
    }
    close( RF );
    }
    } else {
    print $q;
    }
    }
    undef( @Q );
    }

    
    , Jun 26, 2005
    #4
  5. Guest

    > If you want help from us, you need to show good faith, by actually
    > attempting that first step on your own. Good luck.


    My faith is "good".
    And my perl version is 5.8.0

    The problem my perl skill our 2 years out of date. And the script has
    to run on Mac/Windows and Unix which leave out running a simple:

    find . -name "*.php|sed -i -f sed_script?

    While looking at the perldoc page for File I noticed find2perl which
    produced this 54 line script:

    #! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if 0; #$running_under_some_shell

    use strict;
    use File::Find ();

    # Set the variable $File::Find::dont_use_nlink if you're using AFS,
    # since AFS cheats.

    # for the convenience of &wanted calls, including -eval statements:
    use vars qw/*name *dir *prune/;
    *name = *File::Find::name;
    *dir = *File::Find::dir;
    *prune = *File::Find::prune;

    sub wanted;
    sub doexec ($@);



    # Traverse desired filesystems
    File::Find::find({wanted => \&wanted}, '.');
    exit;


    sub wanted {
    /^.*\.php\z/s &&
    doexec(0, 'sed','-i','-e','s/HTTP_GET_VAR/_GET/g','-e');
    }


    use Cwd ();
    my $cwd = Cwd::cwd();

    sub doexec ($@) {
    my $ok = shift;
    my @command = @_; # copy so we don't try to s/// aliases to
    constants
    for my $word (@command)
    { $word =~ s#{}#$name#g }
    if ($ok) {
    my $old = select(STDOUT);
    $| = 1;
    print "@command";
    select($old);
    return 0 unless <STDIN> =~ /^y/;
    }
    chdir $cwd; #sigh
    system @command;
    chdir $File::Find::dir;
    return !$?;
    }

    
    , Jun 26, 2005
    #5
  6. <> wrote:
    >> If you want help from us, you need to show good faith, by actually
    >> attempting that first step on your own. Good luck.

    >
    > My faith is "good".



    We are not asking for you to *say* you are asking in good faith[1],
    we are asking for you to *show* us your good faith by actually
    trying to write your own Perl code that addresses your problem.


    > The problem my perl skill our 2 years out of date.



    Then you will have an even easier time of it than many who post
    here who have never used Perl before.


    > And the script has
    > to run on Mac/Windows and Unix which leave out running a simple:
    >
    > find . -name "*.php|sed -i -f sed_script?



    To do the recursive filesystem searching part, see:

    perldoc File::Find


    For help with the string replacing part, see:

    perldoc -q file

    How do I change one line in a file/delete a line in a file/insert a
    line in the middle of a file/append to the beginning of a file?

    perldoc -f s

    perldoc perlop



    [1] "good faith" here means that you are trying to get help with writing
    *your own* Perl code, rather than trying to get us to write
    your program for you.

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 26, 2005
    #6
  7. <> wrote:

    > Thanks for you replies.



    You can pay me back by reading the Posting Guidelines that
    are posted here frequently.


    [ Please provide an attribution when you quote someone ]


    >> Run your sed program through the "s2p" sed-to-perl translator that
    >> comes with the perl distribution.
    >>

    > I actually had already run s2p on sed script, does it really take a 139
    > line Perl script to replace a 4 line sed script?

    ^^^^^^
    ^^^^^^

    (I thought you had shown us a _5_ line sed script...)


    If you want a machine to do it at zero personal cost, then yes.
    That is typical of machine-generated code.

    If you want to learn enough Perl to write a program to replace it,
    then is should be only about 20 lines long.

    If you want to learn enough Perl to make use of perl's command line
    switches, then you can replace it with a single *one* liner that can
    be run directly from the command line.

    My one-liner to replace your 5 sed statements it about 65 characters
    long, including the 4 "perl" characters at the beginning.


    I'll try and remember to post it here later (if warranted).


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 26, 2005
    #7
    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 K. Humkey

    sed regexp mystery

    John K. Humkey, Jul 8, 2003, in forum: Perl
    Replies:
    0
    Views:
    2,033
    John K. Humkey
    Jul 8, 2003
  2. NNTP

    sed awk or perl for this?

    NNTP, Sep 11, 2003, in forum: Perl
    Replies:
    13
    Views:
    3,461
    Alan Connor
    Sep 30, 2003
  3. hofer
    Replies:
    11
    Views:
    2,610
  4. Jack Penarth

    using sed from with a perl script

    Jack Penarth, Feb 27, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    134
    Michele Dondi
    Mar 2, 2004
  5. Replies:
    2
    Views:
    142
    Tad McClellan
    Jan 16, 2005
Loading...

Share This Page