m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <m@m-chrzan.xyz>2025-05-08 00:25:14 +0200
committerMarcin Chrzanowski <m@m-chrzan.xyz>2025-05-13 16:49:47 +0200
commit77dcf718a05d7a9c0657612bff413f4d7ce0cda9 (patch)
tree4cb30ce38037d6ba7cc85a31c9e746066e9b3da2
parent34dd60e937f59e851b1be07d963321bb018115af (diff)
Add awk details
-rw-r--r--awk.md52
1 files changed, 51 insertions, 1 deletions
diff --git a/awk.md b/awk.md
index 2c0e1fa..ea1bb92 100644
--- a/awk.md
+++ b/awk.md
@@ -13,4 +13,54 @@ Program is ;-separated pattern-action statements.
`$0` is whole line, `$1`, `$2`, ... are fields separated by `FS` (by default,
whitespace).
-Set `FS` with `-F <sepstring>`
+Set `FS` with `-F <sepstring>`. This can be a regular expression.
+
+Actually, the first part is any *condition*, and a /grep/ pattern is just one
+possible one. Another useful example: `awk '$2 > 100 { print $3 }'`: print the
+third column, if the line's second column is greater than 100.
+
+## Flags
+
+* `-F`: set the field separator, `FS`
+* `-v`: set any variable
+
+## Conditions
+
+* Numerical comparisons: `==`, `!=`, `>`, `>=`, `<`, `<=`
+* Regex matches: `~`, `!~`
+* Logical operators: `&&`, `||`, `!`
+
+Special conditions:
+
+* `BEGIN`: triggered before processing any lines
+* `END`: triggered after processing all lines
+
+If multiple conditions match, each action will be executed for it. In
+particular, if multiple actions print, each will print. `next` can come in
+useful, skipping all other conditions.
+
+## Built in Variables
+
+* `NR`: number of lines processed so far
+* `NF`: number of fields (columns) in current line
+* `FS`: input field separator, used to split each line into fields.
+* `OFS`: output field separator, i.e. what gets printed between comma-separated
+ items in a `print` statement. Default: ' ' (space). Typical pattern: set it to
+ something else in a BEGIN block or with the `-v` flag.
+
+Useful when processing multiple files:
+
+* `FNR`: like `NR`, resets to 0 on new file
+* `FILENAME`: name of currently processed file (`-` when STDIN)
+
+## Actions
+
+* `next`: skips processing of following conditions
+* `exit`: finish processing (`END` will be executed)
+* `printf`: formatted printing
+
+## Arrays
+
+* They're really more like hashmaps.
+* Index with `[]`.
+* Iterate over keys: `for(x in arr) print x, arr[x]`