Command line pipe question

Discussion in 'Perl Misc' started by RyanY, Oct 20, 2005.

  1. RyanY

    RyanY Guest

    Hello,

    I am wondering if it is possible to pipe input to the -i command line
    switch. For example:

    perl -i -pe'orig_*' 's/string/newString/g' file1

    will change all occurances of "string" with "newString" and create a
    file called "orig_file1" that will be the original file. How do I pipe
    the output of ls to this?

    The following doesn't work.

    ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'

    I have read the perlfaq5 and understand how to do this within a perl
    script. However, I came across the -i command line switch and wanted
    to know if I could just pipe the filenames to it.

    Thanks,
    Ryan
     
    RyanY, Oct 20, 2005
    #1
    1. Advertising

  2. RyanY

    John Bokma Guest

    "RyanY" <> wrote:

    > Hello,
    >
    > I am wondering if it is possible to pipe input to the -i command line
    > switch. For example:
    >
    > perl -i -pe'orig_*' 's/string/newString/g' file1
    >
    > will change all occurances of "string" with "newString" and create a
    > file called "orig_file1" that will be the original file. How do I pipe
    > the output of ls to this?
    >
    > The following doesn't work.
    >
    > ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'


    No, because -i means edit in place, so how do you want to edit the output
    of ls in place :)

    dir | perl -pe "s/a/b/g"

    works here. (BTW, I don't understand your perl -i pe'orig_*' syntax, since
    to me that would run 'orig_*' as perl code)

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Oct 21, 2005
    #2
    1. Advertising

  3. RyanY

    Guest

    "RyanY" <> wrote:
    > Hello,
    >
    > I am wondering if it is possible to pipe input to the -i command line
    > switch. For example:
    >
    > perl -i -pe'orig_*' 's/string/newString/g' file1
    >
    > will change all occurances of "string" with "newString" and create a
    > file called "orig_file1" that will be the original file.


    I'm pretty sure it won't. The argument to -i has to be immediately
    after -i, with no intervening whitespace and certainly no intervening
    switches.


    > How do I pipe
    > the output of ls to this?
    >
    > The following doesn't work.
    >
    > ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'


    Use the shell.

    perl -i'orig_*' -pe 's/string/newString/g' `ls *.txt`

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Oct 21, 2005
    #3
  4. RyanY wrote:
    >
    > I am wondering if it is possible to pipe input to the -i command line
    > switch. For example:
    >
    > perl -i -pe'orig_*' 's/string/newString/g' file1
    >
    > will change all occurances of "string" with "newString" and create a
    > file called "orig_file1" that will be the original file.


    No. That will run the expression 'orig_*' for every line in the two files
    s/string/newString/g and file1 and edit them in-place without creating a
    backup file.


    > How do I pipe the output of ls to this?


    You don't really want to do that.


    > The following doesn't work.
    >
    > ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'


    perl -i'orig_*' -pe's/string/newString/g' *.txt



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Oct 21, 2005
    #4
  5. RyanY

    Tintin Guest

    <> wrote in message
    news:20051020202550.828$...
    > "RyanY" <> wrote:
    >> Hello,
    >>
    >> I am wondering if it is possible to pipe input to the -i command line
    >> switch. For example:
    >>
    >> perl -i -pe'orig_*' 's/string/newString/g' file1
    >>
    >> will change all occurances of "string" with "newString" and create a
    >> file called "orig_file1" that will be the original file.

    >
    > I'm pretty sure it won't. The argument to -i has to be immediately
    > after -i, with no intervening whitespace and certainly no intervening
    > switches.
    >
    >
    >> How do I pipe
    >> the output of ls to this?
    >>
    >> The following doesn't work.
    >>
    >> ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'

    >
    > Use the shell.
    >
    > perl -i'orig_*' -pe 's/string/newString/g' `ls *.txt`


    UUOLS

    perl -i'orig_*' -pe 's/string/newString/g' *.txt
     
    Tintin, Oct 21, 2005
    #5
  6. RyanY

    Guest

    "Tintin" <> wrote:
    > <> wrote in message
    > news:20051020202550.828$...
    > > "RyanY" <> wrote:
    > >> Hello,
    > >>
    > >> I am wondering if it is possible to pipe input to the -i command line
    > >> switch. For example:
    > >>
    > >> perl -i -pe'orig_*' 's/string/newString/g' file1
    > >>
    > >> will change all occurances of "string" with "newString" and create a
    > >> file called "orig_file1" that will be the original file.

    > >
    > > I'm pretty sure it won't. The argument to -i has to be immediately
    > > after -i, with no intervening whitespace and certainly no intervening
    > > switches.
    > >
    > >
    > >> How do I pipe
    > >> the output of ls to this?
    > >>
    > >> The following doesn't work.
    > >>
    > >> ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'

    > >
    > > Use the shell.
    > >
    > > perl -i'orig_*' -pe 's/string/newString/g' `ls *.txt`

    >
    > UUOLS
    >
    > perl -i'orig_*' -pe 's/string/newString/g' *.txt


    I assumed the OP was following the guidelines by reducing his problem
    to the simplest case that demonstrates what he wanted to demonstrate.
    Doing that often results in constructs which are useless if you consider
    the program to be an end-in-itself, but are not useless if you consider it
    as what it is, a simplified demonstration.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Oct 21, 2005
    #6
  7. RyanY

    Guest

    Tintin wrote:
    > UUOLS


    What is UUOLS?
     
    , Oct 21, 2005
    #7
  8. RyanY

    Guest

    wrote:
    > Tintin wrote:
    > > UUOLS

    >
    > What is UUOLS?


    Useless Use of ls.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Oct 21, 2005
    #8
  9. RyanY

    RyanY Guest

    wrote:
    > wrote:
    > > Tintin wrote:
    > > > UUOLS

    > >
    > > What is UUOLS?

    >
    > Useless Use of ls.
    >
    > Xho
    >
    > --
    > -------------------- http://NewsReader.Com/ --------------------
    > Usenet Newsgroup Service $9.95/Month 30GB


    Thanks everyone. Yes, I mistyped the original post. The 'orig-*'
    should have been with the i switch.
     
    RyanY, Oct 21, 2005
    #9
  10. RyanY

    Guest

    RyanY wrote:

    This is why the clpm posting guidelines recommend:
    >>> Do not re-type Perl code
    >>> Use copy/paste or your editor's ``import'' function rather than
    >>> attempting to type in your code. If you make a typo you will get
    >>> followups about your typos instead of about the question you are
    >>> trying to get answered.


    Good advice:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    , Oct 21, 2005
    #10
  11. RyanY

    Peter Scott Guest

    On Thu, 20 Oct 2005 15:37:51 -0700, RyanY wrote:
    > I am wondering if it is possible to pipe input to the -i command line
    > switch. For example:
    >
    > perl -i -pe'orig_*' 's/string/newString/g' file1
    >
    > will change all occurances of "string" with "newString" and create a
    > file called "orig_file1" that will be the original file.


    No it won't; that's a syntax error. And the optional argument to the -i
    flag is a suffix, not a prefix. perldoc perlrun.

    > How do I pipe the output of ls to this?
    >
    > The following doesn't work.
    >
    > ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'


    Use the for / foreach builtin of your shell, e.g.

    foreach i (*.txt)
    perl -i.orig -pe 's/string/newString/g' $i
    end

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
     
    Peter Scott, Oct 23, 2005
    #11
  12. Peter Scott wrote:
    > On Thu, 20 Oct 2005 15:37:51 -0700, RyanY wrote:
    >>I am wondering if it is possible to pipe input to the -i command line
    >>switch. For example:
    >>
    >>perl -i -pe'orig_*' 's/string/newString/g' file1
    >>
    >>will change all occurances of "string" with "newString" and create a
    >>file called "orig_file1" that will be the original file.

    >
    > No it won't; that's a syntax error. And the optional argument to the -i
    > flag is a suffix, not a prefix. perldoc perlrun.


    Please read perlrun.pod more carefully:

    perldoc perlrun
    [snip]
    This allows you to add a prefix to the backup file, instead of (or in
    addition to) a suffix:

    $ perl -pi'orig_*' -e 's/bar/baz/' fileA # backup to 'orig_fileA'


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Oct 23, 2005
    #12
  13. RyanY

    Peter Scott Guest

    On Sun, 23 Oct 2005 17:06:42 +0000, John W. Krahn wrote:
    > Peter Scott wrote:
    >> On Thu, 20 Oct 2005 15:37:51 -0700, RyanY wrote:
    >>>I am wondering if it is possible to pipe input to the -i command line
    >>>switch. For example:
    >>>
    >>>perl -i -pe'orig_*' 's/string/newString/g' file1

    >>
    >> No it won't; that's a syntax error. And the optional argument to the -i
    >> flag is a suffix, not a prefix. perldoc perlrun.

    >
    > Please read perlrun.pod more carefully:
    >
    > perldoc perlrun
    > [snip]
    > This allows you to add a prefix to the backup file, instead of (or in
    > addition to) a suffix:
    >
    > $ perl -pi'orig_*' -e 's/bar/baz/' fileA # backup to 'orig_fileA'


    Good Lord, how did I miss that all this time? The OP's command line is
    still a syntax error, though, because the argument to -i is in the wrong
    place. Thanks for the eye-opener.

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
     
    Peter Scott, Oct 25, 2005
    #13
    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,073
    Tohid
    Oct 23, 2003
  2. Bernd Oninger
    Replies:
    1
    Views:
    793
    Martin Honnen
    Jul 7, 2004
  3. lee, wonsun
    Replies:
    1
    Views:
    490
    Jack Klein
    Nov 2, 2004
  4. GerShar
    Replies:
    1
    Views:
    723
    Dennis Lee Bieber
    Oct 31, 2005
  5. Replies:
    1
    Views:
    226
    Ben Morrow
    Jun 2, 2004
Loading...

Share This Page