LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   AIX (https://www.linuxquestions.org/questions/aix-43/)
-   -   Problem with if statement in awk in AIX (https://www.linuxquestions.org/questions/aix-43/problem-with-if-statement-in-awk-in-aix-925612/)

sukhdip 01-25-2012 05:20 AM

Problem with if statement in awk in AIX
 
Hi,



My requirement is to check wheather some csv files have mandatory columns value as empty if empty log a error message or set a flag.

The problem here is that the column number varies for different CSVs hence we can not hardcode $1 or $3 like this in the awk command hence we are reading a proprty file to get the mandatory fields & then setting it to a variable by prefixing $ to that column number & checking the empty value for that column.

I am writing the following awk command :

Code:

for file in $run_count"_"*.csv; do
 echo "file name "$file
 PROP_KEY=$file
 Property_Value=""
 Property_Value=$(cat ${PROPERTY_FILE_NAME} | grep "${PROP_KEY}" | cut -d'=' -f2)
 echo $Property_Value 
 OFIS=$IFS
 IFS=','
 array=$Property_Value
 flag=""
 for x in $array
 do
  COL_NUMBER="$""$x"
  echo "COL numbr-"$COL_NUMBER
  flag=$(awk -F"|" '{ if( '$COL_NUMBER' ~ /^ *$/ ){ printf("1"); }}' $file )

 done
echo "flag = "$flag
 var=$(echo $flag | cut -c1)
 echo "var= " $var
 if [ "$var" == "1" ]; then
  echo "\n error encounterd in file" $file
  exitFlag=1
 else
  echo "Successful"
 fi
done

It works well in ksh shell of Unix but in AIX it fails with error :
Code:

Syntax Error The source line is 1.
 The error context is
 <<<            { if( >>>  $1
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
 Syntax Error The source line is 1.

Then we found out that '{ if( ' is creating problem as AIX is treating this as command for awk if I can get a solution to retreive the $COL_NUMBER as $1 then my problem will be solved. as $COL_NUMBER contains $1 or $2 something like this.

Any help will be appreciated

Thanks & Regards,
Sukhi

catkin 01-25-2012 08:41 AM

The error message is from awk. Probably the version of awk on AIX is not the same as whatever other UNIX you are using for testing. Originally there was awk, then there was nawk (new awk) then there was gawk (GNU awk) ... for a full list see here. For convenience (ha!) nawk, gawk etc. are usually invoked by the awk command. The man page on each system should say which version is being used.

trbennett48 01-27-2012 01:46 PM

I'm not exactly sure what your problem is, but I don't get the error that you mentioned.

I "stripped" out the "awk" statement and a couple of other statements from your script and put them
into a "test" script. I also wrote a temporary file (assuming fields separated by a | based upon your awk statement).
I received no error, when I ran my "test" script.

My temp_file (Note it begins with a blank character):
Code:

|2|3|4|5
My test script:
Code:

#!/bin/ksh
#

file=temp_file
x=1
COL_NUMBER="$""$x"
echo "COL numbr-"$COL_NUMBER
flag=$(awk -F"|" '{ if( '$COL_NUMBER' ~ /^ *$/ ){ printf("1"); }}' $file )

echo "flag-->$flag<--"

Here is the output I get:
Code:

COL numbr-$1
flag-->1<--

You will note the "awk" statement is unchanged from your script.

sukhdip 01-31-2012 02:28 AM

Hi! trbennett48
luckily solved the problem.

Thanks for the support.:)

diwakar.bo 07-09-2015 06:59 AM

Same type of problem
 
Hi All,

I am getting same type of problem, Below is the code & the error,

Code:

#!/bin/sh
  for HOST in $(cat servers.txt ) ;
  do
  ssh $HOST '
        df -g | grep -i db2 | awk '{ print $4 " " $1 }' | while read output;
                  usep=$(echo $output | awk '{ print $1}' | cut -d'' -f1  )
                  partition=$(echo $output | awk '{ print $2 }' )
                  if [ $usep -ge 50 ]; then
                        echo "Running out of space "$partition ($usep)" on $(hostname) as on $(date)" >> filesystem_$(hostname)
                  fi
                        echo "$filesystem_$(hostname)" | mail -s "Alert: Almost out of disk space $usep" XXXX@XXX.com
        done '
  done

Here is the Error,

Code:


Error : getting error for awk command ()
 Syntax Error The source line is 1.
 The error context is
                {print >>>  $4 <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
Server2
 Syntax Error The source line is 1.
 The error context is
                {print >>>  $4 <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.


NevemTeve 07-09-2015 11:20 AM

Please edit your post: add [code] and [/code] tags.

NevemTeve 07-13-2015 10:42 PM

You should realize that you have nested string-literals (eg: 'outer 'inner' outer-again') which don't work in shell.
First you should get the 'inner part' working (locally, without ssh), then find out how to execute it remotely.

diwakar.bo 07-29-2015 01:18 AM

Same type of problem
 
Thank you for your reply,

The code is working fine without the awk in it, When i put the awk command only i am getting an error.

Please suggest if any alternate for the awk command.

Regards
Diwakar


All times are GMT -5. The time now is 10:16 PM.