awk - multiple and nested if-then-else

Hello.

I would like to convert the following piece of code from bash to awk.

Here are bash variables in a bash script.

CUR_ROW_ID and ROW_ID_TO_SEARCH contains a string which represent a row id.

The string contain a valid row id.

  • CUR_ROW_ID sometimes may be null.
  • CUR_VALUE contains a string which represent the value of the relative CUR_ROW_ID.
  • CUR_VALUE sometimes may be null.
  • DO_IT is a flag to control the rest of the process.

Three variables a read from bash env to awk env : CUR_ROW_ID and ROW_ID_TO_SEARCH and CUR_VALUE
One variable is write from awk env to bash env.

Don't look for a meaning in this piece of code. It's just a question about nested if-then-else

ROW_ID_TO_SEARCH contains a string to search in the input file
If there is a match CUR_ROW_ID = ROW_ID_TO_SEARCH
If ROW_ID_TO_SEARCH is not in the input file CUR_ROW_ID is null.
I got this in bash with something like :

    L_FLAG=$( cat $L_INPUT_FILE | grep -w "$ROW_ID_TO_SEARCH" )
    if [[ -z $L_FLAG ]] ; then
        echo "ROW : $ROW_ID_TO_SEARCH not in use"
    else
        .................
        .................
        .................
    fi
 

So my question is : how to convert the following in awk :

    if [[  -z "$CUR_ROW_ID" ]] ; then

        printf "NEW ROW to insert" 

        DO_IT=1

    else

        if [[  "$CUR_ROW_ID" == "$ROW_ID_TO_SEARCH" ]] ; then 

                if [[ -z "$CUR_VALUE" ]] ; then
 
                    printf "NEW VALUE TO CREATE for ROW : %s\n" "$CUR_ROW_ID"
                    DO_IT=2 

            else 

                printf "CURRENT VALUE : %s TO UPDATE for ROW : %s\n" "$CUR_VALUE" "$CUR_ROW_ID"
                DO_IT=3 

            fi 

        fi 

    fi

 

AWK part

To convert

if [[  -z "$CUR_ROW_ID" ]] ; then

I have tried this which does not work ( print all the line ) :

awk -v a="$ROW_ID_TO_SEARCH" '{ if ($0 !~ a) {  printf ("NOT FOUND --> ROW_ID:%s\n",a)  }} '  $L_INPUT_FILE

For nested if-the-else I have tried this which should not print nothing because "ROW_ID_TO_SEARCH" (FORCE_IGNORE9) is not in the input file

awk -v a="$ROW_ID_TO_SEARCH" '{  if($1 == a)
    { if ($2 == "" ) printf ("ID_NULL --> ROW_ID:%s\tVALUE:%s\n",$1,$2) }
      else if ($2 != "" ) {  printf ("ID_NOLT_NULL --> ROW_ID:%s\tVALUE:%s\n",$1,$2) } ;
    }'  $L_INPUT_FILE
 

The result is :

ID_NOLT_NULL --> ROW_ID:#FORCE_IGNORE2 VALUE:1
ID_NOLT_NULL --> ROW_ID:FORCE_IGNORE4_1 VALUE:2
ID_NOLT_NULL --> ROW_ID:FORCE_IGNORE4_3 VALUE:3
ID_NOLT_NULL --> ROW_ID:FORCE_IGNORE4_5 VALUE:4

Test with :

ROW_ID_TO_SEARCH=FORCE_IGNORE9

the input file contains :

FORCE_IGNORE1
#FORCE_IGNORE2    1
FORCE_IGNORE3
FORCE_IGNORE4_1    2
FORCE_IGNORE4_2
FORCE_IGNORE4_3    3
FORCE_IGNORE4_4
FORCE_IGNORE4_5    4
FORCE_IGNORE4_6
 

Any help is welcome.

Not clear. The result you present is exactly what one would expect once your code has been rearranged like

awk -v a="$ROW_ID_TO_SEARCH" '
        {if ($1 == a)   if ($2 == "") printf ("ID_NULL --> ROW_ID:%s\tVALUE:%s\n",      $1, $2)
           else         if ($2 != "") printf ("ID_NOLT_NULL --> ROW_ID:%s\tVALUE:%s\n", $1, $2)
    }
' file

If your search pattern is not found AND $2 holds a value, print that "ID_NOLT_NULL" - line.

What is your desire? What am I missing here?

Than k you for your quick posting.3

I have found this rules when googleing :

if(conditional-expression1)
    action1;
else if(conditional-expression2)
    action2;
else if(conditional-expression3)
    action3;
    .
    .
else
    action n;

What I would like to do if possible is ( don't take care about the {} syntax; it is just to explain my thought) :

if(conditional-expression1)
    { if(conditional-expression4)        action4 ;   

    else if(conditional-expression5)        action5 ;

    else if(conditional-expression2)
        { if(conditional-expression6)             action6 ;   

         else if(conditional-expression7)             action7 ; } }


    else if(conditional-expression2)
      action2;  
    else if(conditional-expression3)
       action3 ;
    .
    . }
else
    action n;

I would like to add a sub level block of if-then-else any where.

--- Post updated at 17:52 ---

To explain what i want I am going to use pseudo code so forget missing ';' or missing {} :

awk -v a="$ROW_ID_TO_SEARCH" '
 {if ($1 == a)        # everything now depend of the fact that $1 == a if ($2 == "") printf ("ID_NULL --> ROW_ID:%s\tVALUE:%s\n",      $1, $2)      # because $1 == a  &&  $2 == ""
else
if ($2 != "") printf ("ID_NOLT_NULL --> ROW_ID:%s\tVALUE:%s\n", $1, $2)  # because $1 == a  &&  $2 != ""
} else {

# now here $1 != a and then nothing to do

}' INPUT_FILE



and the result should be now :


because the row id to search is not in the input file

1 Like

That's EXACTLY the point - you CAN'T "forget missing ... {} ". You HAVE to "take care about the {} syntax" as they support / convey the logics. action n in an if construct is a - one or multiple statement - block of code to be executed if the relevant condition is true. Inside the block, you can have other constructs, whatever the programming language provides.