Can't get command line perl to work right 
Author Message
 Can't get command line perl to work right

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



Mon, 12 Aug 2013 18:41:54 GMT  
 Can't get command line perl to work right

Quote:

>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



Mon, 12 Aug 2013 19:10:48 GMT  
 Can't get command line perl to work right

Quote:
> 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



Mon, 12 Aug 2013 19:11:31 GMT  
 Can't get command line perl to work right

Quote:

> 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



Mon, 12 Aug 2013 19:18:27 GMT  
 Can't get command line perl to work right

Quote:
> 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



Mon, 12 Aug 2013 19:44:48 GMT  
 Can't get command line perl to work right

  >> 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

--

-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------



Mon, 12 Aug 2013 19:50:39 GMT  
 Can't get command line perl to work right

Quote:

> 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?

Use -n instead of -p. You get the same "loop over every line in the
input" behavior, but without the implicit "print $_" that -p adds.

See perlrun for details:

  <http://perldoc.perl.org/perlrun.html>

sherm--

--
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer



Mon, 12 Aug 2013 20:20:59 GMT  
 Can't get command line perl to work right

Quote:

> ? >> 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

> --

> ----- ?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


Mon, 12 Aug 2013 20:21:36 GMT  
 Can't get command line perl to work right

Quote:


>> 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

> 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

Chomp() the newline, then append it back onto $_ after escaping it:

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

sherm--

--
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer



Mon, 12 Aug 2013 20:29:34 GMT  
 Can't get command line perl to work right
Am 24.02.2011 20:21, schrieb Dave:

Quote:
>  [...]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?

"perldoc perlrun" is again your friend. Use the "-l" option to
remove line breaks when reading the file and add them back when
printing so they don't get passed to uri_escape().

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

-Chris



Mon, 12 Aug 2013 20:35:57 GMT  
 Can't get command line perl to work right

Quote:

> Am 24.02.2011 20:21, schrieb Dave:
>>  [...]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?

> "perldoc perlrun" is again your friend. Use the "-l" option to
> remove line breaks when reading the file and add them back when
> printing so they don't get passed to uri_escape().

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

Good answer - better than mine. I obviously need to play more golf. :-)

sherm--

--
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer



Mon, 12 Aug 2013 20:43:18 GMT  
 Can't get command line perl to work right

Quote:

>> 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.



Tue, 13 Aug 2013 09:58:41 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Q: Getting 'cp' command to work

2. Command line Perl works but IIS Perl doesn't

3. -F works on command line but not on #!perl -paF line

4. IIS: Perl CGI's execing command line progs, can't read from pipe

5. RE : DIFFICULT: Can't execute system command from FVWM, but can from the command line

6. If/ Else doesn't work right....

7. -s and -z filetests don't work right

8. Why doesn't this work right?

9. System call doesn't work right!

10. perl program works on command line, but not when run through www server

11. tk installed---only works from command line ($perl...)

12. Newbie ?: @ARGV not getting command line arguments

 

 
Powered by phpBB® Forum Software