Alex Hall writes:
Or perhaps https://xon.sh, and have Python be the native syntax of the shell. (Unfortunately if I read correctly xonsh is based on Python 3.5, so no walrus operator and no f-strings yet.)
On Thu, Nov 5, 2020 at 8:03 PM Hans Ginzel firstname.lastname@example.org wrote:
Is there a reason, why not to make python useful for practical one liners to replace perl and awk?
Why to learn Perl/awk/datamash/mlr/…, for “one line like” tasks?
I'm really not clear on the concept here, though. My problem with the whole idea of Python one-liners is its modular architecture and syntax. It doesn't lend itself to one-liners as far as I can see. I guess you deal with significant whitespace by defining "line" as "logical line" and passing python a string formatted as a program would be, shells can handle that.
But most work in Python involves function calls, which involve parentheses, so that on the command line you'll have to quote them, which means you have to keep track of Python and shell quoting semantics simultaneously if you use any literal strings. The perlrun manpage say "There is no general solution to all of this. It's just a mess." :-)
As for function calls, even a simple regex filter ("grep") using -p will require
if not re.search('some regexp', line): continue
Sure, you could provide a command-line option for that; I suspect it's common enough to justify one more option if you're already adding four. :-) But there are infinite variations on the theme.
Anyway, in most cases you have to call functions, and quite likely import modules that provide them. I guess that your initial list of os, re, sys covers most of what Perl provides natively, so maybe imports are not such a big problem. Although requiring the "os." (and "os.path."!) prefix is annoying -- I'm not sure it's a good idea to "from os import *", etc.
It would help if you provided a variety of "<-c code comes here>" examples for your proposed "python -n -c ..." invocations to show that these extensions would be frequently useful. Eg, I think I'd type
cat file | python -p "if not re.search('some regexp', line): continue"
at most once before asking a friend if there's a command-line utility that takes a regexp and filters linewise.
Also, given your example for -n, I wonder if you couldn't get most of the effect you want with modules, like this:
python -m perln "<-c code comes here>"
python -m oneline -n "<-c code comes here>"
Don't ask me how to implement those; I'm pretty sure it's quite possible, but it's over my head.
I second most of what Steve D'Aprano wrote, except that I think that "-m oneline -n" would be a good way to experiment (and I suspect something like this is what Alex had in mind when he wrote "DSL").