Imagen de rioport
+1
+1
-1

...
According to the POSIX standard, awk is supposed to behave as if each record is split into fields at the time that it is read. In particular, this means that you can change the value of FS after a record is read, but before any of the fields are referenced. The value of the fields (i.e. how they were split) should reflect the old value of FS, not the new one.

However, many implementations of awk do not do this. Instead, they defer splitting the fields until a field reference actually happens, using the current value of FS! This behavior can be difficult to diagnose. The following example illustrates the results of the two methods. (The sed command prints just the first line of `/etc/passwd'.)

sed 1q /etc/passwd | awk '{ FS = ":" ; print $1 }'

will usually print

root

on an incorrect implementation of awk, while gawk will print something like

root:nSijPlPhZZwgE:0:0:Root:/:

...

Fuente

+1
+1
-1

-- GC --

Imagen de rioport
+1
+1
-1

... ser:

gawk --field-separator="|" "{ print NF }" fich_prueba.txt
+1
+1
-1

-- GC --

Imagen de JAVIER NAVARRO ALTIMIRA
+1
+1
-1

cat a.txt | awk --field-separator="|" '
{
printf("%s\n", $1);
printf("%d\n", NF);
}
'

+1
+1
-1