cregox
cregox

Reputation: 18408

Use python in shell, as if it were awk

Say I want to print 1 + 1 on stdout (i.e. one-liner coding).

With awk I could do it simply with:

$ echo | awk '{print 1+1}'
2

How to do this with python?

Upvotes: 3

Views: 3148

Answers (5)

mbigras
mbigras

Reputation: 8055

Like SingleNegationElimination's answer recommend's, the -c flag is the right tool for the job.

Here are some examples using awk, ruby, and python:

echo foo bar baz | awk '{ split($0, arr, " "); print arr[NF] }'
baz
echo foo bar baz | ruby -e 'puts STDIN.read.chomp.split(" ")[-1]'
baz
echo foo bar baz | python -c 'import sys; print sys.stdin.read().rstrip().split(" ")[-1]'
baz

Upvotes: 0

abarnert
abarnert

Reputation: 365835

As ifLoop pointed out, what you're looking for here is -c.

But, as yo've discovered, python -c often isn't as useful as the corresponding awk (or sed or bash or perl or even ruby) for one-liners.

Python is explicitly designed to value readability over brevity and explicitness over implicitness (along with some correlated tradeoffs, like vocabulary over syntax, as little magic as possible, etc.). See the Zen of Python. There are intentional limits to what you can cram onto one line, and things like looping over stdin and/or command-line args have to be done explicitly with, e.g., sys.stdin and sys.argv, or fileinput.input().

That means that some very trivial scripts become less trivial to write in Python, but that's considered a good tradeoff for making even moderately non-trivial scripts easier to write, maintain, and understand.

The core developers understand this means you can't rewrite a lot of one-liners in Python. And if you asked them, most of them will ask why that's a problem at all.

If you know how to write something as a one-liner in a language like sed or awk, then you should be writing it as a one-liner in sed or awk. Those are perfectly good languages that are handy for all kinds of simple tasks, and there's no reason to avoid them just because Python is also a good language.

If you can't figure your way through the syntax to write that one-liner… well, it probably shouldn't be a one-liner. The only reason you want to try it in Python is that Python is generally easier to write and read, and the same reasons that's true are the same reasons Python won't let you write what you want without 3 lines. So just write the 3 lines. Python is great for that.

So, what you often really want is not -c, but a heredoc, or just a separate script that you run like any other program, or awk or perl instead of python.

Upvotes: 3

clt60
clt60

Reputation: 63932

if you're in the shell, the next would basic integer math

echo $((1+1))
echo $(( 100 / 5 ))

etc...

for floating point, yes, you should to use awk, or bc, or dc, or any other language what knows floating point math...

also, read this: https://stackoverflow.com/a/450853/632407

Upvotes: 0

Boris Gorelik
Boris Gorelik

Reputation: 31777

Inspired by the answer by IfLoop I wondered about the handy BEGIN and END blocks in awk. I have found the pawk module

    ls -l | awk 'BEGIN {c = 0} {c += $5} END {print c}'
    ls -l | pawk -s -B 'c = 0' -E 'c' 'c += int(f[4])'

Looks promising, but I have never tried this (yet)

Upvotes: 1

SingleNegationElimination
SingleNegationElimination

Reputation: 156208

you are looking for -c:

$ python -c 'print 1 + 1'
2

Upvotes: 7

Related Questions