Mohamed Lamine Allal
2 min readSep 14, 2020

--

Hi again Fahima! Thank you again for your response! FPAT look too cool!

https://www.gnu.org/software/gawk/manual/gawk.html

I can see this example from the link above!

BEGIN {

FPAT = "([^,]+)|(\"[^\"]+\")"

}

{

print "NF = ", NF

for (i = 1; i <= NF; i++) {

printf("$%d = <%s>\n", i, $i)

}

}

which will render in case of an csv file something like

$ gawk -f simple-csv.awk addresses.csv

NF = 7

$1 = <Robbins>

$2 = <Arnold>

$3 = <"1234 A Pretty Street, NE">

$4 = <MyTown>

$5 = <MyState>

$6 = <12345-6789>

$7 = <USA>

The FPAT allowed to make a pattern for what the fields should be!

At first glimpse i questioned if the regex groups matter ! And that precising multiple groups will determine the fields to be taken! $1, $2, $3 will refer respectively to the first, second, and third group! But unfortunately and after testing directly with an example! I found it is not! groups doesn't matter at all! FPAT take a pattern and with that pattern it take all the matched elements! They make the fields on the order that they take! And that's it! Still FPAT is too interesting!

For the test here it is

text: "Awk is Mega Amazing ok my friend"

awk -v FPAT='(awk).*?(amazing).*?(friend)' '{for(i = 1; i <= NF; i++){ printf("$%d = <%s>\n", i, $i) }}' text.txt

give nothing.

awk -v FPAT='([A-Za-z]*?) ' '{for(i = 1; i <= NF; i++){ printf("$%d = <%s>\n", i, $i) }}' text.txt

give :

$1 = <Awk >

$2 = <is >

$3 = <Mega >

$4 = <Amazing >

$5 = <ok >

$6 = <my >

We can notice too that the space is taken even though it's not in the group! Which will make us conclude that the catching group have no effect on when it comes to determining the fields!

And a last test:

awk -v FPAT='(Awk) (is)' '{for(i = 1; i <= NF; i++){ printf("$%d = <%s>\n", i, $i) }}' text.txt

result:

$1 = <Awk is>

And that make it completely clear.

Questions ???

Well is there more to the FPAT! Can we use the catching goups to make the $1, $2, ... As with a normal regex engine! Or not!

Any other good options ?

Otherwise FPAT seems too nice!

--

--

Mohamed Lamine Allal
Mohamed Lamine Allal

Written by Mohamed Lamine Allal

Developper, Entrepreneur, CTO, Writer! A magic chaser! And A passionate thinker! Obsessed with Performance! And magic for life! And deep thinking!

Responses (1)