Earlier quoted context omitted.
You don’t even need to know awk these days. Just say “how to do x munging task” in ChatGPT and you’ll get a one liner that will be just as good as if you’d say there squinting at man pages for 30 minutes
this is exactly the sort of case where you get non-portable bullshit you don't understand out of it! It spits out something that works on BSD but not on GNU, you put it in your script and _boom_ wonder why the thing blew up in prod, and oh btw you also lack the ability to debug it because you never understood it in the first place
CLI text processing with GNU awk
121–130 of 136 posts
Re: CLI text processing with GNU awk
#122Earlier quoted context omitted.
One of these days I need to get around to learning awk. In the meantime, I've learned some of the deeper, stateful, features of sed. For instance, you mentioned wanting to only output a line if it was preceded by another. Here's a sed command that does so: sed -ne 'x' -e '/PREV/ {x; /CURR/ p; x}' > echo -e "PREV\nCURR\nCURR\nCURR\nPREV\nRED" | sed -ne 'x' -e '/PREV/ {x; /CURR/ p; x}' CURR This uses sed's hold buffer.…
> One of these days I need to get around to learning awk Plan9's awk(1)[0] man page provides a precise and concise (a few paragraphs) presentation of the core features of all awk implementations. Tutorials bring practical knowledge, but often lack complete and self-contained descriptions of those nifty little tools. [0]: https://man.cat-v.org/plan_9/1/awk
It's short and to the point, has good examples, and cuts most of the usual fluff like "what is a variable?". Its base assumptions are: You know how to program, and you're here to learn AWK. Let's get to it.
I dearly wish there'd be more books like it for other languages.
Re: CLI text processing with GNU awk
#123Earlier quoted context omitted.
Do all systems still come with Perl baked in these days? If so I could see reaching for that over awk/sed. If I have to install a runtime I may as well just reach for Python
>> Do all systems still come with Perl baked in these days? If you use Git for Windows ( https://gitforwindows.org/ ), it includes Perl.
Re: CLI text processing with GNU awk
#124Perhaps my old sysadmin hat is showing through, but I don’t quite see what the advantage of awk is over just writing the same thing in perl. I’ve seen my fair share of horrendous shell scripts from junior sysadmins, and every time I think to myself “the text processing portion would be so much cleaner in Perl”.
There aren't any technical advantages, no. Perl's features are a proper superset of awk (by design!). What's happened is that Kids Today (tm) never learned perl. So they're discovering awk as someone new to the idea of stream processing. And awk was a great idea for that, and it represented a genuine innovation worth emulating. In the late 1970's. Then of course perl did emulate and surpass it. But then got forgotten…
Now, if I would get hired as a full-time Perl developer and spent 2 years developing Perl: it would perhaps be different. But that's not the case, and isn't for most people.
For better or worse, Perl sees a lot less usage than it once did; I rarely encounter it "in the wild" and don't even have it on my laptop because nothing needs it.
Re: CLI text processing with GNU awk
#125Earlier quoted context omitted.
>> Do all systems still come with Perl baked in these days? If you use Git for Windows ( https://gitforwindows.org/ ), it includes Perl.
…and gawk :)
It is nice that Git for Windows includes bash and all these tools.
Re: CLI text processing with GNU awk
#126I love awk, and I find myself reaching for it a fair bit. One of the main things I use it for is “sed with state,” so for things like matching on a line, but only if it was preceded by some other line. I find this to be really useful for creating one-off linters, for example I made one recently to check all our migration files for CREATE INDEX without CONCURRENTLY on a particular set of very large tables where it wou…
One of these days I need to get around to learning awk. In the meantime, I've learned some of the deeper, stateful, features of sed. For instance, you mentioned wanting to only output a line if it was preceded by another. Here's a sed command that does so: sed -ne 'x' -e '/PREV/ {x; /CURR/ p; x}' > echo -e "PREV\nCURR\nCURR\nCURR\nPREV\nRED" | sed -ne 'x' -e '/PREV/ {x; /CURR/ p; x}' CURR This uses sed's hold buffer.…
Re: CLI text processing with GNU awk
#127Earlier quoted context omitted.
> One of these days I need to get around to learning awk Plan9's awk(1)[0] man page provides a precise and concise (a few paragraphs) presentation of the core features of all awk implementations. Tutorials bring practical knowledge, but often lack complete and self-contained descriptions of those nifty little tools. [0]: https://man.cat-v.org/plan_9/1/awk
I still maintain that "The AWK Programming Language" [1] is one of, if not the best programming language book I've read so far. It's short and to the point, has good examples, and cuts most of the usual fluff like "what is a variable?". Its base assumptions are: You know how to program, and you're here to learn AWK. Let's get to it. I dearly wish there'd be more books like it for other languages. [1]: https://archive…
It's always delightful to see competent authors demonstrate how much sophistication is achievable in about 100 lines of simple code, by comparison with the "the Dog class inherits from the Animal class" type of examples, or "real-life" codebases. This book is definitely in the former category.
> I dearly wish there'd be more books like it for other languages.
This all reminds me of a well-known regular expression matcher[0], in about 30 lines of C, featured in "The Practice of Programming"[1].
More generally, even without dedicated books, there are common simple-but-sophisticated type of programs that are great to get to know a language, once you have basic programming skills: standard UNIX tools (cat(1), grep(1), etc.), λ-calculus interpreter, LISP interpreter, raytracer, etc. One can often find online versions serving as "solutions."
[0]: https://www.cs.princeton.edu/courses/archive/spr09/cos333/be...
[1]: https://en.wikipedia.org/wiki/The_Practice_of_Programming
Re: CLI text processing with GNU awk
#128Earlier quoted context omitted.
There aren't any technical advantages, no. Perl's features are a proper superset of awk (by design!). What's happened is that Kids Today (tm) never learned perl. So they're discovering awk as someone new to the idea of stream processing. And awk was a great idea for that, and it represented a genuine innovation worth emulating. In the late 1970's. Then of course perl did emulate and surpass it. But then got forgotten…
I'm one of those "kids these days" but did actually learn to program Perl at some point, and I generally prefer AWK. Perl is a large and complex language, I don't need it that often, and I'm not smart enough to keep remembering all of it. Now, if I would get hired as a full-time Perl developer and spent 2 years developing Perl: it would perhaps be different. But that's not the case, and isn't for most people. For bet…
Re: CLI text processing with GNU awk
#129Perhaps my old sysadmin hat is showing through, but I don’t quite see what the advantage of awk is over just writing the same thing in perl. I’ve seen my fair share of horrendous shell scripts from junior sysadmins, and every time I think to myself “the text processing portion would be so much cleaner in Perl”.
Not being snarky, why not python over perl? what makes perl better for scripts?
Re: CLI text processing with GNU awk
#130Earlier quoted context omitted.
More importantly it's just mega slow. The whole point of bash one liners is not to write short bash, it's to type it in your shell to get a result quickly. Just typing the url to chatgpt in my browser I would have had the time to write my one liner in she'll XD
I sincerely doubt unless you are writing shell all day every day that you will get a decently complex working one liner out faster than GPT4.
What is a "decently complex one liner"?
If it's decently complex, then it's probably not a one liner, so indeed chatgpt may be faster.
If it's a one liner, then it's probably not complex, so I would be quite confident in being faster than chatgpt.
The reality is, 99.9% of one liners are just series of pipes and filters to extract specific fields from an output, and act on it. I'm quite confident I would be faster than chatgpt for any of these. And no, I don't write bash "all day, every day".
I tend to see "every day bash" as very similar to SQL. Once you know cut, grep, find, sed and awk, even at a basic level, then you can combine them and extract pretty much anything.