awk if else problem

Hi,

I am trying to put if else on some field of a file but not getting it where to put it.

find . -name "*" -ctime -6 | xargs cat | grep -E -v ^fileName\|^\([0-9]\) | awk -v DATE="${CURR_DATE}" -v DATE_LOG="$DATE_SYS" -v IN="" '
        BEGIN {
                FS = ";"
                OFS = ";"
                CONVFMT = "%.9g"
                OFMT = "%.9g"
        }
         {
           if(substr($0,5,6)=='NAN' {IN=1}
           else if {substr($0,5,6)=='NON' {IN=2}
         }

        {
                clef = "2-PARSING_ERROR;" DATE ";" DATE_LOG ";"
                substr ( $0, 12, 8) ";" IN ";NS;NS;" $3
                tab[clef]++
        }
        END {
                for (clef in tab)
                {
                        print tab[clef],clef
                }
        }
'

I am trying to put the red color block but not getting where to put and the right way.

Please help.

Thanks and Regards

Abhinav

  1. Use double quotes.
  2. Parenthesis after if keyword does not close (same with else-if)
if (substr($0, 5, 6) == "NAN")

Thanks for the reply but didnot help..

find . -name "*" -ctime -6 | xargs cat | grep -E -v ^fileName\|^\([0-9]\) | awk -v DATE="${CURR_DATE}" -v DATE_LOG="$DATE_SYS" -v IN="" '
        BEGIN {
                FS = ";"
                OFS = ";"
                CONVFMT = "%.9g"
                OFMT = "%.9g"
        }
        {
          if(substr($0,5,6)=="NAN") {IN=1}
          else {IN=2}
       }
   ..

It didnot work syntax error

awk: syntax error Context is:

>>>     GIN{FS=";";OFS=";";CONVFMT="%.9g";OFMT="%.9g"}(if       <<<

Am I putting it in a wrong way?

What OS are you using?

The code you included shows a an open brace before the if that isn't working; but the awk syntax error diagnostic shows an open parenthesis there. If you don't show us code that matches the diagnostics, we can't help much.

Also note that a 6 character substring ( substr($0,5,6) ) is never going to match a three character string ( "NAN" ) unless $0 only contains 7 characters ending with "NAN".

SunOS 5.10

But does it have to do with OS? I think I am putting it in a wrong way.

When using Solaris systems and you are writing new awk scripts, you should use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk , instead of /bin/awk . The default /bin/awk on Solaris systems is there for compatibility with ancient awk scripts; not for new code.

Sorry, totally my bad..

find . -name "*" -ctime -6 | xargs cat | grep -E -v ^fileName\|^\([0-9]\) | awk -v DATE="${CURR_DATE}" -v DATE_LOG="$DATE_SYS" -v IN="" '
        BEGIN {
                FS = ";"
                OFS = ";"
                CONVFMT = "%.9g"
                OFMT = "%.9g"
        }
        {
          if(substr($0,5,6)=="NAN") {IN=1}
          else {IN=2}
       }

and the syntax error:

awk: syntax error  Context is:
>>>     IN{FS=";";OFS=";";CONVFMT="%.9g";OFMT="%.9g"}{(if       <<<

I tried removing the "{}" of if-else but still didnot work.
May be the logic is incorrect but syntax error should not be there.

Please help me with this..

Can you post the actual command you are running?
the command you posted doesnt have ( before if however your error message shows it is there

1 Like

sorry my bad..

The code is:

find . -name "*" -ctime -6 | xargs cat | grep -E -v ^fileName\|^\([0-9]\) | awk -v DATE="${CURR_DATE}" -v DATE_LOG="$DATE_SYS" -v IN="" '
        BEGIN {
                FS = ";"
                OFS = ";"
                CONVFMT = "%.9g"
                OFMT = "%.9g"
        }
        {
          if(substr($0,5,6)=="NAN") {IN=1}
          else {IN=2}
       }

..

Syntax error:

awk: syntax error Context is:
>>> IN{FS=";";OFS=";";CONVFMT="%.9g";OFMT="%.9g"}{(if <<<

I also tried with no braces across if-else but did not help.

Please help me with this.

---------- Post updated at 02:41 PM ---------- Previous update was at 02:38 PM ----------

thanks for pointing it..Blind i am..:frowning:

Thanks everyone for the help..