Help regarding if condition in AIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help regarding if condition in AIX
# 1  
Old 01-25-2012
Help regarding if condition 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 08:46 AM..
# 2  
Old 01-25-2012
I move your thread into the shell scripting area, as this is not really AIX specific as more a type of shell/awk and might have better chances for an answer.
# 3  
Old 01-25-2012
If you provide an input file and which field you are specifically searching for, it will be better for us to help you...

--ahamed
# 4  
Old 01-25-2012
Please try this:
Code:
#!/bin/bash

pvfile=prop_value.tmp
for file in ${run_count}_*.csv; do
    echo "file name "$file
    grep "${file}" ${PROPERTY_FILE_NAME} | cut -d'=' -f2 > $pvfile  #stash the col numbers into a file
    awk 'NR==FNR{c[$0];next}  #store the column numbers from '$pvfile' in an array
      {
        for(col in c) {
          if($col ~ /^ *$/) {
             print "empty column " col " in file " FILENAME
          }
        }
      }' RS="," $pvfile RS="\n" FS="|" $file

done

The outer for loop can be actually omitted also, with awk doing the grep and extracting the column numbers, but this may be more comprehensive approach. Instead of making awk print a statement into stdout, you can print it into a file, and then check whether the file is empty, if not, then some columns in some files contained errors and take appropriate measures.

Last edited by mirni; 01-25-2012 at 10:21 AM..
# 5  
Old 01-25-2012
Try:
Code:
 for x in $array
 do
  flag=$(awk -vCOL_NUMBER=$x -F"|" '{ if( "$COL_NUMBER" ~ /^ *$/ ){ printf("1"); }}' $file )
 done

# 6  
Old 01-31-2012
MySQL

hi first thanks all for your input to the thread.
I used something like this:
Code:
flag=$( awk -F"|" -v var="$Property_Value" 'BEGIN{n=split(var,a,",")} NR > 1 { for(i=1;i<=n;i++) { if($a[i] ~ /^ *$/){printf("1") }} }' $file)

And problem is solved.

ThanksSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. AIX

Samba 3.6 on AIX 7.1 - Windows 10 Access to AIX file shares using Active Directory authentication

I am running AIX 7.1 and currently we have samba 3.6.25 installed on the server. As it stands some AIX folders are shared that can be accessed by certain Windows users. The problem is that since Windows 10 the guest feature no longer works so users have to manually type in their Windows login/pwd... (14 Replies)
Discussion started by: linuxsnake
14 Replies

2. AIX

Elseif & for condition in AIX

I'm using the below statements in my script if && then sqlplus sysadm/abcdefgh12@${dbarr} @/u1/scripts/ResetPswd.sql elif then for idx in 0 1 2 3 4 5 6 7 do sqlplus sysadm/abcdefgh12@${dbarr} @/u1/scripts/ResetPswd.sql done else exit fi It give me... (5 Replies)
Discussion started by: Pandee
5 Replies

3. AIX

Will it affect my AIX LPAR security, when i set up email alerts on AIX server.

Hello, I've set up email alerts on AIX Servers. so that i can get email notifications (via mail relay server) when ever there is abnormal behavior. for example 1) my script monitors CPU/disk/memory etc... when it reaches high water ark, it will send an email alert. 2) disk usage alerts 3)... (5 Replies)
Discussion started by: System Admin 77
5 Replies

4. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

5. AIX

Nim on AIX 7.1 used to migrate AIX 5.3 to AIX 6.1...is possible?

Using nimadm: nimadm -j nimadmvg -c sap024 -s spot_6100 -l lpp_6100 -d "hdisk1" -Y Initializing the NIM master. Initializing NIM client sap024. 0505-205 nimadm: The level of bos.alt_disk_install.rte installed in SPOT spot_6100 (6.1.3.4) does not match the NIM master's level (7.1.1.2).... (2 Replies)
Discussion started by: sciacca75
2 Replies

6. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

8. AIX

How to apply aix 5.3 TL8 properly on ML5 aix system ?

Is it necessary to put system into single user mode for applying aix 5.3 TL8 on a aix 5.3.5.0 system ? Is the TL8 installation not totally safe ? thank you. (6 Replies)
Discussion started by: astjen
6 Replies
Login or Register to Ask a Question