LinuxQuestions.org
Review your favorite Linux distribution.
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, 05:20 AM   #1
sukhdip
Member
 
Registered: Sep 2011
Posts: 54

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 06:45 AM.
 
Old 01-25-2012, 08:41 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 30

Rep: Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193
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, 01:46 PM   #3
trbennett48
LQ Newbie
 
Registered: Apr 2011
Location: Oregon
Posts: 25

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, 02:28 AM   #4
sukhdip
Member
 
Registered: Sep 2011
Posts: 54

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

Thanks for the support.
 
Old 07-09-2015, 06:59 AM   #5
diwakar.bo
LQ Newbie
 
Registered: Jul 2015
Posts: 2

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

Last edited by diwakar.bo; 07-13-2015 at 12:24 AM. Reason: Need to add the CODE Braces
 
Old 07-09-2015, 11:20 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,896

Rep: Reputation: 821Reputation: 821Reputation: 821Reputation: 821Reputation: 821Reputation: 821Reputation: 821
Please edit your post: add [code] and [/code] tags.
 
Old 07-13-2015, 10:42 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,896

Rep: Reputation: 821Reputation: 821Reputation: 821Reputation: 821Reputation: 821Reputation: 821Reputation: 821
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.
 
Old 07-29-2015, 01:18 AM   #8
diwakar.bo
LQ Newbie
 
Registered: Jul 2015
Posts: 2

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


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 03:58 AM
Need help stripping statement from text file, ksh: sed awk? austin881 Programming 7 07-13-2009 04:31 PM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 08:39 AM
how to keep Field Separator in AWK when using a sub statement tmcguinness Programming 4 02-09-2009 02:24 PM
Case Statement With Aix Ksh Scripting ']['HeBroken AIX 2 02-09-2005 12:44 PM


All times are GMT -5. The time now is 05:02 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration