mercredi 23 septembre 2020

if-else statement issue in awk

I want to make a condition for processing my file but it doesn't work. Here is my dataset:

CPOODT      #   DODS Date KPI               #DECIMAL    #    7.0        #1      #   Date
CPLWRB      #   DODS Code KPI               #CHAR       #    30         #2      #0
CPF1TE      #   DODS Valeur  KPI            #CHAR       #    30         #0      #0
CPF2TE      #   DODS Description KPI        #CHAR       #    50         #0      #0
CPEJZB      #   Date Created                #DECIMAL    #    7.0        #0      #   Date
CPAVZI      #   Time Created                #DECIMAL    #    6.0        #0      #   Heure

I try to run a condition so I can replace "char" with "string" and save the result in new file. Here is my code:

BEGIN {                                                                         
  FS="#"
}
{
if ($3=="CHAR") {$3=="string"} else {$3="decimal"}
printf($3   "(\"${SEPARATEUR_ABINITIO}\")"       "\t"   $1"= NULL(\"\")""\t""/*"     "\n")}
END {
printf("end")}

Unfortunately I get this as a result:

decimal("${SEPARATEUR_ABINITIO}")   CPOODT= NULL("")    /*
decimal("${SEPARATEUR_ABINITIO}")   CPLWRB= NULL("")    /*
decimal("${SEPARATEUR_ABINITIO}")   CPF1TE= NULL("")    /*
decimal("${SEPARATEUR_ABINITIO}")   CPF2TE= NULL("")    /*
decimal("${SEPARATEUR_ABINITIO}")   CPEJZB= NULL("")    /*
decimal("${SEPARATEUR_ABINITIO}")   CPAVZI= NULL("")    /*

This is the expected output:

decimal("${SEPARATEUR_ABINITIO}")   CPOODT= NULL("")    /*
string("${SEPARATEUR_ABINITIO}")    CPLWRB= NULL("")    /*
string("${SEPARATEUR_ABINITIO}")    CPF1TE= NULL("")    /*
string("${SEPARATEUR_ABINITIO}")    CPF2TE= NULL("")    /*
decimal("${SEPARATEUR_ABINITIO}")   CPEJZB= NULL("")    /*
decimal("${SEPARATEUR_ABINITIO}")   CPAVZI= NULL("")    /*

I don't want to use the gsub function and even if I replace the if-else with : #{ $3 = ($3 == "%CHAR%" ? -1 : $3) } I still get the same result.

My command line to run the function:

awk -f function1.awk input.txt>output.txt

Aucun commentaire:

Enregistrer un commentaire