Can't get command line perl to work right

Discussion in 'Perl Misc' started by Dave, Feb 24, 2011.

  1. Dave

    Dave Guest

    Hi,

    I'm using Perl 5.8.9 on Mac 10.6.3. I have a file and I want to have
    a new file where the uri_escape function is applied to each line of
    the file. However, this call

    perl -pi -e 'use URI::Escape; print uri_escape($_)' link_names.txt

    only partially works. The result is a line that contains the escaped
    string, and then the original string. How do I modify the code above
    to print out only the escaped string?

    Thanks, - Dave
     
    Dave, Feb 24, 2011
    #1
    1. Advertising

  2. Dave

    Guest

    On Thu, 24 Feb 2011 09:41:54 -0800 (PST), Dave <> wrote:

    >Hi,
    >
    >I'm using Perl 5.8.9 on Mac 10.6.3. I have a file and I want to have
    >a new file where the uri_escape function is applied to each line of
    >the file. However, this call
    >
    >perl -pi -e 'use URI::Escape; print uri_escape($_)' link_names.txt
    >
    >only partially works. The result is a line that contains the escaped
    >string, and then the original string. How do I modify the code above
    >to print out only the escaped string?
    >

    perl -ni -mURI::Escape -e 'print uri_escape($_)' link_names.txt

    maybe

    -sln
     
    , Feb 24, 2011
    #2
    1. Advertising

  3. On 24/02/11 17:41, Dave wrote:
    > Hi,
    >
    > I'm using Perl 5.8.9 on Mac 10.6.3. I have a file and I want to have
    > a new file where the uri_escape function is applied to each line of
    > the file. However, this call
    >
    > perl -pi -e 'use URI::Escape; print uri_escape($_)' link_names.txt
    >
    > only partially works. The result is a line that contains the escaped
    > string, and then the original string. How do I modify the code above
    > to print out only the escaped string?
    >
    > Thanks, - Dave


    Hi Dave,

    perl -n -E 'use URI::Escape; say uri_escape($_)' link_names.txt

    Cheers,
    Charlie
     
    Charlie Harvey, Feb 24, 2011
    #3
  4. Dave

    J. Gleixner Guest

    Dave wrote:
    > Hi,
    >
    > I'm using Perl 5.8.9 on Mac 10.6.3. I have a file and I want to have
    > a new file where the uri_escape function is applied to each line of
    > the file. However, this call
    >
    > perl -pi -e 'use URI::Escape; print uri_escape($_)' link_names.txt
    >
    > only partially works. The result is a line that contains the escaped
    > string, and then the original string. How do I modify the code above
    > to print out only the escaped string?



    Check the documentation to see what those flags actually do:

    perldoc perlrun

    Hint: using -p along with print might be redundant.

    If you want a new file, then you need to specify it. Hint: -i
     
    J. Gleixner, Feb 24, 2011
    #4
  5. Dave

    pleier Guest

    On 24.02.2011 18:41, Dave wrote:
    > Hi,
    >
    > I'm using Perl 5.8.9 on Mac 10.6.3. I have a file and I want to have
    > a new file where the uri_escape function is applied to each line of
    > the file. However, this call
    >
    > perl -pi -e 'use URI::Escape; print uri_escape($_)' link_names.txt
    >
    > only partially works. The result is a line that contains the escaped
    > string, and then the original string. How do I modify the code above
    > to print out only the escaped string?
    >
    > Thanks, - Dave


    The -p already prints the original line, just leave it and it should work.

    gerhard
     
    pleier, Feb 24, 2011
    #5
  6. Dave

    Uri Guttman Guest

    >>>>> "p" == pleier <> writes:

    >> perl -pi -e 'use URI::Escape; print uri_escape($_)' link_names.txt


    p> The -p already prints the original line, just leave it and it should work.

    leave what? uri_escape returns a value so it needs to either be printed
    explicitly or assigned to $_.

    these should be equivilent:

    perl -pi -e 'use URI::Escape; $_ = uri_escape($_)' link_names.txt
    perl -ni -e 'use URI::Escape; print uri_escape($_)' link_names.txt

    and using -M is a little cleaner too:

    perl -pi -MURI::Escape -e '$_ = uri_escape($_)' link_names.txt

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Feb 24, 2011
    #6
  7. Dave

    Dave Guest

    On Feb 24, 12:50 pm, "Uri Guttman" <> wrote:
    > >>>>> "p" == pleier  <> writes:

    >
    >   >> perl -pi -e 'use URI::Escape; print uri_escape($_)' link_names.txt
    >
    >   p> The -p already prints the original line, just leave it and it should work.
    >
    > leave what? uri_escape returns a value so it needs to either be printed
    > explicitly or assigned to $_.
    >
    > these should be equivilent:
    >
    > perl -pi -e 'use URI::Escape; $_ = uri_escape($_)' link_names.txt
    > perl -ni -e 'use URI::Escape; print uri_escape($_)' link_names.txt
    >
    > and using -M is a little cleaner too:
    >
    > perl -pi -MURI::Escape -e '$_ = uri_escape($_)' link_names.txt
    >
    > uri
    >
    > --
    > Uri Guttman  ------    --------  http://www.sysarch.com--
    > -----  Perl Code Review , Architecture, Development, Training, Support ------
    > ---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com---------


    Thanks for everyone's suggestions. Unfortunately, I got different
    errors from all of them except Uri's. Uri, you're solution did not
    result in any errors, but everything appeared on one long line (I
    noticed the carriage returns got escaped as "%0A"). Is there a way to
    make each line appear separately? - Dave
     
    Dave, Feb 24, 2011
    #7
  8. Dave

    Justin C Guest

    On 2011-02-24, Charlie Harvey <> wrote:
    > On 24/02/11 17:41, Dave wrote:
    >> Hi,
    >>
    >> I'm using Perl 5.8.9 on Mac 10.6.3. I have a file and I want to have
    >> a new file where the uri_escape function is applied to each line of
    >> the file. However, this call
    >>
    >> perl -pi -e 'use URI::Escape; print uri_escape($_)' link_names.txt
    >>
    >> only partially works. The result is a line that contains the escaped
    >> string, and then the original string. How do I modify the code above
    >> to print out only the escaped string?
    >>
    >> Thanks, - Dave

    >
    > Hi Dave,
    >
    > perl -n -E 'use URI::Escape; say uri_escape($_)' link_names.txt


    This is for 5.10, the OP doesn't have 5.10.

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Feb 25, 2011
    #8
    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. Xeno Campanoli
    Replies:
    1
    Views:
    345
    James Britt
    Jul 1, 2005
  2. P. A.
    Replies:
    0
    Views:
    146
    P. A.
    Sep 4, 2009
  3. lucy
    Replies:
    7
    Views:
    185
    Joe Smith
    Sep 3, 2004
  4. Replies:
    2
    Views:
    162
    Tad McClellan
    Mar 26, 2005
  5. Ting Wang
    Replies:
    3
    Views:
    197
    Paul Lalli
    Dec 13, 2005
Loading...

Share This Page