aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2023-12-13 00:47:55 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2023-12-13 00:47:55 +0100
commit4ed29d46d45a751071d89be37c87de71204efda4 (patch)
treece7e71c1b7786a42c1aee91675654efa178f250b
parentea0192f7cb8a7e0f9ba9e8474a0e806ef6d94ded (diff)
downloadnotes-4ed29d46d45a751071d89be37c87de71204efda4.tar.gz
notes-4ed29d46d45a751071d89be37c87de71204efda4.zip
awk: range pattern example, regex cmp operator
-rw-r--r--src/tools/awk.md19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/tools/awk.md b/src/tools/awk.md
index 96212d5..a0c2470 100644
--- a/src/tools/awk.md
+++ b/src/tools/awk.md
@@ -45,6 +45,11 @@ fields ----> run associated action
Any valid awk `expr` can be a `pattern`.
+An example is the regex pattern `/abc/ { print $1 }` which prints the first
+field if the record matches the regex `/abc/`. This form is actually a short
+version for `$0 ~ /abc/ { print $1 }`, see the regex comparison operator
+below.
+
### Special pattern
awk provides two special patterns, `BEGIN` and `END`, which can be used
@@ -92,6 +97,15 @@ multiple times. Actions with those patterns are **executed exactly once**.
- `%S` second (00-59)
- `%T` alias for `%H:%M:%S`
+- `S ~ R`, `S !~ R`
+
+ The regex comparison operator, where the former returns true if the string
+ `S` matches the regex `R`, and the latter is the negated form.
+ The regex can be either a
+ [constant](https://www.gnu.org/software/gawk/manual/html_node/Regexp-Usage.html)
+ or [dynamic](
+ https://www.gnu.org/software/gawk/manual/html_node/Computed-Regexps.html)
+ regex.
## Examples
@@ -110,7 +124,8 @@ Matches records not starting with `#`.
### Range patterns
```bash
-echo -e "a\nFOO\nb\nc\nBAR\nd" | awk '/FOO/,/BAR/ { print }'
+echo -e "a\nFOO\nb\nc\nBAR\nd" | \
+ awk '/FOO/,/BAR/ { print }'
```
`/FOO/,/BAR/` define a range pattern of `begin_pattern, end_pattern`. When
`begin_pattern` is matched the range is **turned on** and when the
@@ -120,7 +135,7 @@ in the range _inclusive_.
An _exclusive_ range must be handled explicitly, for example as follows.
```bash
echo -e "a\nFOO\nb\nc\nBAR\nd" | \
- awk '/FOO/,/BAR/ { if (!($1 ~ "FOO") && !($1 ~ "BAR")) { print } }'
+ awk '/FOO/,/BAR/ { if (!($1 ~ "FOO") && !($1 ~ "BAR")) { print } }'
```
### Access last fields in records