3

GNU awk 4.2 was released and it contains many interesting features. One of them is:

  1. The FIELDWIDTHS parsing syntax has been enhanced to allow specifying how many characters to skip before a field starts. It also allows specifying '*' as the last character to mean "the rest of the record". Field splitting with FIELDWIDTHS now sets NF correctly. The documentation for FIELDWIDTHS in the manual has been considerably reorganized and improved as well.

I tested the * thingie and works great to catch the last block into $NF:

# "*" catches in $NF from the 2+2+1=5th character and until the end
$ awk 'BEGIN {FIELDWIDTHS="2 2 *"} {print $NF}' <<< "1234567890"
567890

However, I cannot see how to use the first part of the feature, which is also described in the GNU Awk's Users Guide → A.6 History of gawk Features → Version 4.2 of gawk introduced the following changes:

FIELDWIDTHS was enhanced to allow skipping characters before assigning a value to a field (see Splitting By Content).

I cannot find an example in the linked section either. Thus, what is this feature exactly doing and how does it work?

fedorqui
  • 275,237
  • 103
  • 548
  • 598

1 Answers1

5

The GNU Awk documentation has a section where FIELDWIDTHS id defined.
That section/paragraph also has some notation which sheds some light on the new feature "skipping characters before assigning a value to a field".

Here it is (highlighted), from 7.5.1 Built-in Variables That Control awk:

FIELDWIDTHS #

A space-separated list of columns that tells gawk how to split input with fixed columnar boundaries. Starting in version 4.2, each field width may optionally be preceded by a colon-separated value specifying the number of characters to skip before the field starts. Assigning a value to FIELDWIDTHS overrides the use of FS and FPAT for field splitting.


How it goes in practice:

Let's say we want to skip 3 characters before the 1st field and 1 character before the 2nd field.

awk 'BEGIN {FIELDWIDTHS="3:2 1:2 *"} {print $1, $2}' <<< "1234567890"

The output:

45 78

So 3:2 skips 123 and set 45 and 1:2 skips 6 and set 78.

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105