Python is not very suitable for shell one-liners. You could play with The Pyed Piper:
$ ls | pyp "p[0] | pp.sort() | p + ' first letter, sorted!'"
# it gives sorted list of first letters of every line
It uses standard Python string and list methods as well as custom functions. There is also pyline:
$ ls | pyline -m os 'line and os.path.abspath(line.strip())'
$ ls | pyline -r '\(.*\)' 'rgx and (rgx.group(0), rgx.group(1)) or line'
$ ls | pyline -p 'p and p.abspath() or ("# ".format(line))'
Another alternative is to use ipython
as a shell or a browser-based notebook (recommended). Or if you want more BASHwards-looking syntax and tab completion for subprocess commands; try xonsh
as your shell:
xonsh$ [i*i for i in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
xonsh$ date -u
Tue Oct 6 04:25:27 UTC 2015
subprocess
and its alternatives (plumbum
, pexpect
, sarge
, sh
(module), fabric
) allow you to create arbitrary complex commands by exploiting the best of python and bash.
There are also several nice python -m
one-liners e.g.:
$ python3 -m http.server # serve current directory over http
$ python -m zipfile # work with zipfiles
$ python -m calendar # show calendar
$ python -m telnetlib towel.blinkenlights.nl # Star Wars