Live data from Hacker News

CLI text processing with GNU awk

learnbyexample.github.io

131–136 of 136 posts

Re: CLI text processing with GNU awk

#131
post #91

Earlier quoted context omitted.

Not being snarky, why not python over perl? what makes perl better for scripts?

By the time you figure out which env you need to be using with python, you’ll forget why you needed it.

If all you're doing is text processing that Perl can do out of the box, you probably only need the Python stdlib.

Re: CLI text processing with GNU awk

#132
post #41

Earlier quoted context omitted.

If you are comparing Awk vs Perl for scripts, I'd prefer Perl (or Python). This post is about short one-liners for ad hoc use cases. I prefer sed/awk over Perl for such cases. Though, if you already know Perl, you could continue using it instead of having to learn more tools.

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

What are "all systems"? Most mainstream Debian or Fedora based systems install Perl by default (but not necessarily in specialized settings such as embedded/boot/rescue systems). Alpine linux does not include it in standard images. FreeBSD (and probably Net/OpenBSD) don't install Perl by default. The current macOS still includes it, but Apple has notified that it will be removed at some point. Windows does not include Perl or awk by default.

Sed and Awk are part of POSIX, and maybe more importantly also part of Busybox. They're almost always available when Perl is available, while the reverse is not true.

Re: CLI text processing with GNU awk

#133
post #93

I 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.…

Not a waste of time, IMHO. In general, sed is faster than awk. It's smaller and is found in more places than awk, e.g., build toolchains. The grymoire site is one of the best, IMHO. He also has a tutorial on awk that is good, too. Nice to see people still discovering these tutorials.

Saddens me to see people selling crappy "e-books" or whatetver on text processing on HN. Compared to the older generations that used UNIX, the level of knowledge is lacking. IMHO.

This book from Tim Oreilly is an old favourite and has one of the nuttiest explanations of the hold space. See page 375.

https://www.oreilly.com/openbook/utp/UnixTextProcessing.pdf

https://web.archive.org/web/20230514225639if_/https://www.or...

As a NetBSD user, I found this books useful; all the utilities explained in it are still in the NetBSD userland.

Re: CLI text processing with GNU awk

#134

Earlier quoted context omitted.

Why is that cringe? They genuinely probably came across awk before perl (I know I did, I read "The AWK Programming Language" and then went on to "The C Programming Language"). Having that said, awk is great and it's been the same for decades and available on every system (the same can't really be said about perl).

>> awk is great and it's been the same for decades and available on every system (the same can't really be said about perl). The only issue with AWK is that there are many implementations and they are not always compatible with one another: https://www.gnu.org/software/gawk/manual/html_node/Other-Ver... I have ported AWK scripts from legacy Unix systems to Linux and ran into incompatibilities that required some adjus…

In practice, on actual systems, you're likely to encounter nawk (the One True Awk), gawk (GNU Awk), mawk ("Mike's AWK", a fast awk), and Busybox's AWK.

There are other variants, yes, but in virtually every case these are fully POSIX compliant and/or have a POSIX mode.

(And in truth, gawk is the only non-fully-POSIX awk I've encountered --- it extends standard AWK with asort and the "'" formatting modifier (which prints localised htousands separators in numeric data).

Programmes written for any one awk, if using POSIX features only, will run on any awk.

Many small / embedded systems (think routers, stock Android, or any POSIX-only Unix variant) must have awk, but often don't include Perl.

You'll also find variants of Perl, though the relative stasis of that language make this less an issue now than in the '90s and aughts.

Re: CLI text processing with GNU awk

#135
post #7

What is better? Starting with awk or sed?

Each has its uses, and there are things which are more easily achieved in one than the other.

(I have a set of scripts I use to parse NOAA's weather web page to plain text, and ended up resorting to both sed and awk in the process, and haven't yet tried to simplify that to a single script.)

Sed is usually used for simple text substitutions and manipulations.

Awk has built-in record and array concepts, as well as more standard programming constructs (loops, if/then, case/switch, printf, and external system interfaces (launching and/or reading from external programmes).

My view is that the tools overlap considerably, but also complement one another strongly.

Re: CLI text processing with GNU awk

#136

Earlier quoted context omitted.

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.

Right, but you are bending the argument to your will. 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, an…

One example of what I call “decently complex one liner” that I had ChatGPT write the code for the other day is a command to find top n files of a given pattern in a certain directory, sorted by n in terms of most recently modified. Sure that is reasonably fast to write yourself if you know shell pretty well but why bother when I can just input my requirements and get something that immediately works without squinting at man pages and checking my patterns on regex101? In the time it takes to write out that two sectioned pipe command I’ve already solved it by pasting my requirements into the AI.

SQL is a great example too where I will use an AI even though it’s not necessary. I’ve probably written many tens or hundred thousands of lines of SQL in my life and I would still prefer to just toss my requirements into an AI and have it write the query for me so I don’t have to cross reference things and look up syntax. Easier to do that and iterate on it once or twice than comb through some bigquery or Postgres docs because I can’t remember that particular flavor of sql today

Post reply on HN