diff options
author | johannst <johannes.stoelp@gmail.com> | 2020-12-18 17:19:04 +0100 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2020-12-18 17:19:04 +0100 |
commit | 44e8fe89023e512afdc48b02c54860d8de3cc23d (patch) | |
tree | c609a29382a01e5f7a39aed7f3ca22fb804810fd /src/tools | |
parent | 1b493919365ce70152643da3336c685b640105f2 (diff) | |
download | notes-44e8fe89023e512afdc48b02c54860d8de3cc23d.tar.gz notes-44e8fe89023e512afdc48b02c54860d8de3cc23d.zip |
awk: example how to access last fields in a record
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/awk.md | 7 |
1 files changed, 7 insertions, 0 deletions
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 <space> - `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 }' <file> 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/<pid>/status |