Awk, with separator |

Friends have the following code that is correct.

BEGIN { num_reg = 0
        suma_iva=0        
}
{  
        num_reg++
        suma_iva=suma_iva+int(substr($0, 103,9))
 }
END{
         printf ("%011d",suma_iva)
}

I have the following problem, I have to do just that but this time is not fixed position (substr ($ 0, 103.9)) is now by a separator "|" as I can do ???

example:

manuel|141568|chile|201|glosa

I presume you are talking of an awk script.

In awk , you can define the field separator, in this case "|", and count the number of fields. Unfortunately your data sample doesn't match script, but in the sample print substr ($0, 21, 3) would be equivalent to print $4 .

1 Like

riend what happens is the following:

input file:

manuel|50|glosa1
car|30|glosa2
mariajuana|40|glosa3

with the above code should I add the numeric variable column and save the value in this case is 120

---------- Post updated at 03:01 PM ---------- Previous update was at 03:00 PM ----------

passes that code is oriented when it is fixed but in this case would be separated by |

suma_iva+=int($2)

1 Like

It was perfect !!! Thank you very much for your help

I leave the code if you're interested

cat $PATH_DAT/mr.txt | nawk -v ciclo=$var 'BEGIN  { COUNT = 0;
                suma_neto=0
                FECHA="`date +%Y%m%d`"
                FS="|"
              }
              {
                num_reg++
                suma_neto=suma_neto+int($2)
                printf ("salida:",$suma_neto)
              }
END{
        printf ("%011d",suma_neto)
}
' > cambiaporta.sh