From 44e8fe89023e512afdc48b02c54860d8de3cc23d Mon Sep 17 00:00:00 2001 From: johannst Date: Fri, 18 Dec 2020 17:19:04 +0100 Subject: awk: example how to access last fields in a record --- src/tools/awk.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tools/awk.md b/src/tools/awk.md index 38b0cb1..f8aa0a8 100644 --- a/src/tools/awk.md +++ b/src/tools/awk.md @@ -59,6 +59,7 @@ multiple times. Actions with those patterns are **executed exactly once**. - `FS` _field separator_: regex to split records into fields, by default - `NR` _number record_: number of current record +- `NF` _number fields_: number of fields in the current record ### Special statements & functions @@ -96,6 +97,12 @@ awk 'NR%2 == 0 { print $0 }' The pattern `NR%2 == 0` matches every second record and the action `{ print $0 }` prints the whole record. +### Access last fields in records +```bash +echo 'a b c d e f' | awk '{ print $NF $(NF-1) }' +``` +Access last fields with arithmetic on the `NR` number of fields variable. + ### Capture in variables ```bash # /proc//status -- cgit v1.2.3