4 ms·
I always thought we should make a short of Python for one liners inspired from awk, where the loop over the lines would be implied. the line, lineno and fields
by make3 3y ago
I always thought we should make a short of Python for one liners inspired from awk, where the loop over the lines would be implied.
the line, lineno and fields would be predifined, and I guess re, os, shutil, pathlib and sys are pre imported. maybe the whole stdlib acts as if it's preimported, while only being imported lazyly
here it would be something like
```
if re.match(r'[A-Za-z]', line):
fields = line.split()
print(inp.lineno(), fields[1])
```
so
```
cat makefile | pyawk 'if re.match(r"[A-Za-z]", line): print(lineno, fields[1])'
```
I don't see a way out of multiple if statements requiring multiple lines though, otherwise you would have to introduce brackets to Python lol
- benhoyt 3y agoAlec Thomas wrote a script like this called pawk.py (https://github.com/alecthomas/pawk https://github.com/alecthomas/pawk). It reads input automatically, and for each line, defines "n" and "f" to the line number and fields list (among other things). It even supports /regex/ patterns. Even the print is implicit. So the example above would be: pawk '/^[A-Za-z]/ (n, f[1])' By the way, triple backticks don't work on HN. You have to indent by 2 spaces to get a code block.
- make3 3y agothanks a lot for mentioning pawk, it really looks like what I had in mind
- deleted 3y ago[deleted]
- networked 3y agoA sibling comment already mentions PAWK. You can do cat makefile | pyawk 'if re.match(r"[A-Za-z]", line): print(lineno, fields[1])' in a Python one-liner without PAWK by abusing list comprehensions: python -c 'import fileinput, re; [print(re.split(r"\s+", line)[0], fileinput.lineno()) for line in fileinput.input() if re.match(r"[A-Za-z]", line)]' makefile Edit: Removed a list of other awk replacements to post in a separate comment (https://news.ycombinator.com/item?id=37465164 https://news.ycombinator.com/item?id=37465164).
- cb321 3y agoOften whole program generation in a prog.lang (& ecosystem!) that you already know can substitute for a new prog.lang. Python even has eval. You may be interested in: https://github.com/c-blake/bu/blob/main/doc/rp.md https://github.com/c-blake/bu/blob/main/doc/rp.md You can actually get pretty far depending upon boundaries with the always implicit command-option language (when launched from the shell language, anyway). For example, Ben's example can be adapted to: rp -m^\[A-Za-z\] 'echo nr," ",s[1]' which is only 5 more characters and only 3 more key downs (less SHIFT-ing) than the space-optimized version of his `awk`. { key downs are, of course, just a start to a deep rabbit hole on HCI ergonometrics ending in heatmaps, finger reach/strain/keyboard layouts, left-right hand switching dynamics, etc., but they seem the most portable idea. } Nim is not Python - it is actually a bit more concise while also being statically typed and can be compiled to code which runs as fast as the best C/C++ (at more expense than one usually wants for 1-liner interactive iteration, though unless you need to test on very large data). That said, I find it roughly "as easy" to enter `rp` commands as `awk`. If doing this in Python tickles your fancy, Ben actually has an interesting on these ideas: https://benhoyt.com/writings/prig/ https://benhoyt.com/writings/prig/ you might also find interesting. EDIT: and while I was typing in a sibling @networked mentions a bunch more examples, but I think my comment here remains non-redundant. I'm not sure even one of those examples has some simple `-m` for auto-match mode (although many would say a grep pre-filter is enough for this).
- networked 3y agoSorry, I have removed the list of awk replacements for other languages from that comment because I thought it wasn't the right place for it in the thread. I'll just post it here. - Common Lisp: https://github.com/sharplispers/clawk https://github.com/sharplispers/clawk - Haskell: https://github.com/gelisam/hawk https://github.com/gelisam/hawk - Racket: https://gitlab.com/xgqt/racket-rawk https://gitlab.com/xgqt/racket-rawk - Tcl: https://wiki.tcl-lang.org/page/owh+%2D+a+fileless+tclsh https://wiki.tcl-lang.org/page/owh+%2D+a+fileless+tclsh (disclosure: the page links to my fork) One use for an awk replacement is emitting more structured data. I have used my fork of owh a few times to emit JSON after awk-style parsing. I know GNU Awk can generate JSON with https://www.gnu.org/software/gawk/manual/html_node/gawkextlib.html https://www.gnu.org/software/gawk/manual/html_node/gawkextli..., but I haven't tried it.