diff options
author | Johannes Stoelp <johannes.stoelp@gmail.com> | 2023-12-13 00:23:02 +0100 |
---|---|---|
committer | Johannes Stoelp <johannes.stoelp@gmail.com> | 2023-12-13 00:23:02 +0100 |
commit | ea0192f7cb8a7e0f9ba9e8474a0e806ef6d94ded (patch) | |
tree | 4aab816600dd2130c4b2f3ecbfa78f887ceb0baf /src/tools | |
parent | 947ca7041270c1a0bf8ac39387e611931f606aa1 (diff) | |
download | notes-ea0192f7cb8a7e0f9ba9e8474a0e806ef6d94ded.tar.gz notes-ea0192f7cb8a7e0f9ba9e8474a0e806ef6d94ded.zip |
awk: add range pattern example inclusive/exclusive
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/awk.md | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/tools/awk.md b/src/tools/awk.md index 5fc3184..96212d5 100644 --- a/src/tools/awk.md +++ b/src/tools/awk.md @@ -108,6 +108,21 @@ awk '!/^#/ { print $1 }' <file> ``` Matches records not starting with `#`. +### Range patterns +```bash +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 +`end_pattern` is matched the range is **turned off**. This matches every record +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 } }' +``` + ### Access last fields in records ```bash echo 'a b c d e f' | awk '{ print $NF $(NF-1) }' |