Explain awk code

Paste awk, one-liners included, and Vibe explains which lines each pattern picks, which column each $ number reads and what an array is adding up.

totals.awk

Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Anthropic to write the explanation. Don't paste keys or passwords. Common key formats are refused.

Vibe24 lines selected

Press Explain it for the sample, or paste your own code first.

What this awk sample shows

fields
FS = "," splits every line at its commas, so $2 is the region and $4 is the amount. awk turns $4 into a number when it adds it: 12.50 adds 12.5, and text that doesn't start with a number adds 0.
patterns and actions
Each pattern { action } runs on every line its pattern matches, in order from the top. NR == 1 { next } matches the first line, and next moves straight on to the line after, so the header never reaches the totals. $4 == "" catches a sale with no amount, and the last block has no pattern, so it runs on every line still left.
associative arrays
total[$2] += $4 uses the region as the key. The first time a region turns up its entry is empty, which counts as 0, so nothing has to be set up first. rows[$2]++ counts each region's sales the same way.
BEGIN and END
BEGIN runs before the first line is read, which is why FS is set there, and END runs after the last one. for (region in total) visits the keys in no set order, so each line goes through sort -k2 -rn, which sorts on the second column as a number, largest first.

Good to know

  • Run on six sales with one missing amount, the script prints west 32.50 from 2 sales, east 28.00 from 2 and north 12.50 from 1, then 1 rows had no amount.
  • close("sort -k2 -rn") waits for sort to finish. Without it, 1 rows had no amount came out above the totals in this run, because sort can't print anything until its input is closed, and awk only closes it on exit.
  • Given two files, NR keeps counting across both, so only the first header is skipped: the second file's header adds a region called region with 0.00. FNR == 1 starts again for each file and skips both headers.
  • The sample was run with the awk that comes with macOS, awk version 20200816, and uses only standard awk. Sorting inside awk with asort or PROCINFO["sorted_in"] is gawk-only, which is why the sample hands its lines to sort.

Watch out: awk splits at every comma, even one inside quotes. A product written "poster, large" becomes two fields, so $4 is the text large" with a space in front, which counts as 0: that sale's 30 is lost and west shows 12.50 from 2 sales, with no error. gawk 5.3 and later have a --csv option that reads quoted fields, while this macOS awk prints unknown option --csv ignored.

Explain awk code beside your editor

Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).

Install on Mac

curl -fsSL https://unvibe.site/install.sh | bash

Paste into Terminal. Unvibe isn't notarized by Apple yet, so this is the smooth path.

Opening the .dmg the first time
  1. Open the .dmg and drag Unvibe to Applications.
  2. Open it once; macOS says it can't verify it. Click Done.
    macOS dialog: Unvibe Not Opened, with Done and Move to Trash
    Real screen, macOS 15
  3. System Settings › Privacy & Security › Open Anyway. Confirm, then enter your Mac password. The button shows for about an hour after you try to open Unvibe.
    macOS Privacy & Security, Security section: Unvibe was blocked to protect your Mac, with the Open Anyway button
    Real screen, macOS 15

The Terminal line checks the download against the published SHA-256 and Unvibe's self-signed signature before it installs anything. More help with the first open