Mailing List Archive

perl "-n" option in python?
Hi everyone!

I switched from perl to python some months ago.
One think i liked in perl was the "-n" option to assume a 'while (<>) { ... } loop
around the script. I used this option to do some quick jobs on
the command line. For example:

perl -n -e "/regex/ && print"

So i was never forced to use awk/sed.

Why isn't there something equivalent in python?

Juergen
perl "-n" option in python? [ In reply to ]
On 03 May 1999 16:38:53 +0000, Juergen Hoetzel wrote:
>Hi everyone!

Hi, Juergen.

>I switched from perl to python some months ago.

We understand.

>One think i liked in perl was the "-n" option to assume a 'while (<>) {
>... } loop around the script. I used this option to do some quick jobs on
>the command line. For example:

>perl -n -e "/regex/ && print"
>So i was never forced to use awk/sed.

>Why isn't there something equivalent in python?

I could give a LOT of reasons -- for example, Python doesn't have the
notion of a default argument, so such a thing would be counterproductive
-- but I'll just note that Python isn't made for one-liners.

If you need a one-liner, use Perl instead.

Or, in the above example, grep. :-)

>Juergen

but-then-APL-beats-Perl-at-that-too-ly yr's

--
-William "Billy" Tanksley
"But you shall not escape my iambics."
-- Gaius Valerius Catullus
perl "-n" option in python? [ In reply to ]
[Juergen Hoetzel]
> I switched from perl to python some months ago.

And the world is a better place for it <wink>.

> One think i liked in perl was the "-n" option to assume a 'while
> (<>) { ... } loop around the script. I used this option to do some
> quick jobs on the command line. For example:
>
> perl -n -e "/regex/ && print"
>
> So i was never forced to use awk/sed.
>
> Why isn't there something equivalent in python?

There's something exactly equivalent:

python -c "import os;os.system('perl -ne \"/regex/ && print\"')"

That even works under Windows <wink>.

python's-the-right-tool-for-every-job-ly y'rs - tim
perl "-n" option in python? [ In reply to ]
> [Juergen Hoetzel]
> I switched from perl to python some months ago.
> One think i liked in perl was the "-n" option to assume a 'while
> (<>) { ... } loop around the script. I used this option to do some
> quick jobs on the command line. For example:
>
> perl -n -e "/regex/ && print"
>
> So i was never forced to use awk/sed.
>
> Why isn't there something equivalent in python?

Look at fileinput (standard library module), and look at the pp.py script
in the Demo/ subdirectory in the *source* distribution of Python. Not
quite the same, but gets you partway there.

--david
perl "-n" option in python? [ In reply to ]
wtanksle@dolphin.openprojects.net (William Tanksley) writes:

> On 03 May 1999 16:38:53 +0000, Juergen Hoetzel wrote:
[...]
> I could give a LOT of reasons -- for example, Python doesn't have the
> notion of a default argument, so such a thing would be counterproductive

What do you mean by this? Python has the notion of default arguments...

[...]

--

Magnus
Lie
Hetland http://arcadia.laiv.org <arcadia@laiv.org>
perl "-n" option in python? [ In reply to ]
On 03 May 1999 14:32:37 +0200, Magnus L. Hetland wrote:
>wtanksle@dolphin.openprojects.net (William Tanksley) writes:

>> On 03 May 1999 16:38:53 +0000, Juergen Hoetzel wrote:

>> I could give a LOT of reasons -- for example, Python doesn't have the
>> notion of a default argument, so such a thing would be counterproductive

>What do you mean by this? Python has the notion of default arguments...

Certainly. Python has @default arguments, but it doesn't have the $default
argument. You snipped the context.

He was saying that he wanted to use $_. I was saying he couldn't.

> Magnus

--
-William "Billy" Tanksley
"But you shall not escape my iambics."
-- Gaius Valerius Catullus
perl "-n" option in python? [ In reply to ]
wtanksle@dolphin.openprojects.net (William Tanksley) writes:

> On 03 May 1999 14:32:37 +0200, Magnus L. Hetland wrote:
[...]
> >What do you mean by this? Python has the notion of default arguments...
>
> Certainly. Python has @default arguments, but it doesn't have the $default
> argument. You snipped the context.

OK. Sorry for not reading your posting properly. (Being lazy, I'd much
rather have you explain it ;)

>
> He was saying that he wanted to use $_. I was saying he couldn't.

Right.

>
> > Magnus

--

Magnus
Lie
Hetland http://arcadia.laiv.org <arcadia@laiv.org>