LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices



Reply
 
Search this Thread
Old 01-25-2012, 06:20 AM   #1
sukhdip
Member
 
Registered: Sep 2011
Posts: 50

Rep: Reputation: Disabled
Unhappy 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

Last edited by sukhdip; 01-25-2012 at 07:45 AM.
 
Old 01-25-2012, 09:41 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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.
 
Old 01-27-2012, 02:46 PM   #3
trbennett48
LQ Newbie
 
Registered: Apr 2011
Posts: 23

Rep: Reputation: 3
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.
 
Old 01-31-2012, 03:28 AM   #4
sukhdip
Member
 
Registered: Sep 2011
Posts: 50

Original Poster
Rep: Reputation: Disabled
Hi! trbennett48
luckily solved the problem.

Thanks for the support.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash script is reprinting part of a field from an awk statement ?? SharpyWarpy Linux - General 13 04-22-2010 04:58 AM
Need help stripping statement from text file, ksh: sed awk? austin881 Programming 7 07-13-2009 05:31 PM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 09:39 AM
how to keep Field Separator in AWK when using a sub statement tmcguinness Programming 4 02-09-2009 03:24 PM
Case Statement With Aix Ksh Scripting ']['HeBroken AIX 2 02-09-2005 01:44 PM


All times are GMT -5. The time now is 08:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration