Comment by ectospheno
2 days ago
To be fair the example above is easier to remember than:
ls -l --sort=size | head -n 5 | tail -n 4 | awk '{print $5 " = " $9}' | numfmt --to iec | jq --raw-input --null-input 'inputs | gsub("\r$"; "") | split(" = "; "") | select(length == 2) | {"s": (.[0]), "n": .[1]}'
This example shows nicely how ugly text processing is: you have to use head and tail simply to trim out the first line of ls (the total).
I think it doesn't even work correctly. ls lists files and directories and then picks the first 4 (it should only select files).
And this also uses awk and jq, which are not just simple "one purpose" tools, but pretty much complete programming languages. jq is not even part of most standard installations, it has to be installed first.
I'd replace the first part with (which isn't any shorter, but in general if I want a list of files for a pipeline, find is usually more flexible than ls for anything but the most trivial):
For the latter part, I'd tend to think that if you're going to use awk and jq, you might as well use Ruby.
("-nae" effectively takes an expression on the command line (-e), wraps it in "while gets; ... end" (-a), and adds the equivalent to "$F = $_.split" before the first line of your expression (-n))
It's still ugly, so no competition for nushell still.
I'd be inclined to drop a little wrapper in my bin with a few lines of helpers (see my other comment) and do all Ruy if I wanted to get closer without having to change shells...
Ruby is a pretty natural fit for shell scripting.
https://lucasoshiro.github.io/posts-en/2024-06-17-ruby-shell...
1 reply →
> And this also uses awk and jq, which are not just simple "one purpose" tools, but pretty much complete programming languages
In a way that exactly illustrates the GGP's point: why learn a new language (nushell's) when you can learn awk or jq, which are arguably more generally- and widely-applicable than nushell. Or if awk and jq are too esoteric, you could even pipe the output of `find` into the python or ruby interpreters (one of which you may already know, and are much more generally applicable than nushell, awk, or jq), with a short in-line script on the command line.
> awk or jq, which are arguably more generally- and widely-applicable than nushell
That is backwards. I know I said "complete programming languages", but to be fair, awk only shines when it comes to "records processing", jq only shines for JSON processing. nushell is more like a general scripting language — much more flexible.
Yes, the point was to show that nushell is pretty awesome. I totally punted on the file only part.
I'm aware ;)