substitution from command line

Discussion in 'Perl Misc' started by Geezer From The Freezer, Apr 23, 2004.

  1. trying to change a string in a file

    perl -e s/dogshit/catshit/g filename

    Seems to do nothing - what am I doing wrong?
     
    Geezer From The Freezer, Apr 23, 2004
    #1
    1. Advertising

  2. Geezer From The Freezer

    Guest

    Geezer From The Freezer <> writes:

    > perl -e s/dogshit/catshit/g filename
    > Seems to do nothing - what am I doing wrong?


    It's equivalent to writing a script like this:

    #!/usr/bin/perl
    s/dogshit/catshit/g;

    and then running that script like this:

    perl myscript filename

    You've never actually opened filename up for reading,
    nor have you looped through the contents of it, nor
    printed out anything (ie. back to the file or to a
    new file).

    Take a look at the -n and -p (and the -i) command
    line options for perl

    man perlrun



    --
    Plain Bread alone for e-mail, thanks. The rest gets trashed.
    No HTML in E-Mail! -- http://www.expita.com/nomime.html
    Are you posting responses that are easy for others to follow?
    http://www.greenend.org.uk/rjk/2000/06/14/quoting
     
    , Apr 23, 2004
    #2
    1. Advertising

  3. Geezer From The Freezer

    Mothra Guest

    "Geezer From The Freezer" <> wrote in message
    news:...
    > trying to change a string in a file
    >
    > perl -e s/dogshit/catshit/g filename
    >
    > Seems to do nothing - what am I doing wrong?


    You should be running:

    perl -pi -we 's/dogshit/catshit/g' filename
     
    Mothra, Apr 23, 2004
    #3
  4. Geezer From The Freezer <> wrote:

    > trying to change a string in a file
    >
    > perl -e s/dogshit/catshit/g filename

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

    That was uncalled for.

    It earns you a killfile entry. So long.


    > Seems to do nothing - what am I doing wrong?


    s/// operates on the string in $_, you do not have any string in $_.

    Maybe you want to add the -p switch?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 24, 2004
    #4
    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. Lucas Cowald
    Replies:
    4
    Views:
    1,112
    Tohid
    Oct 23, 2003
  2. Pranav Agarwal

    substitution fails for long line

    Pranav Agarwal, Feb 9, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    126
    Pranav Agarwal
    Feb 11, 2004
  3. Replies:
    4
    Views:
    134
  4. kp

    Command substitution in perl

    kp, Oct 7, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    818
  5. P B
    Replies:
    6
    Views:
    145
Loading...

Share This Page