LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem with grep on aix (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-grep-on-aix-926490/)

sukhdip 01-30-2012 08:02 AM

problem with grep on aix
 
Hi..
I am facing a strange problem on AIX with ksh scripting.
The problem is with grep command. It's not able to work in the below code.

Code:

#!/bin/ksh

run_count=1
catalog_name="rrc"
cd /Dataload_Scripts/Files/$catalog_name
#file=""
for file in $run_count"_"*.csv; do
 echo $file

 # Getting row count from property file
 FILE_NAME=$run_count"_"$catalog_name"_Record_Count.txt"
 echo "FILE_NAME " $FILE_NAME
 PROP_KEY=$file
 echo "PROP_KEY" $PROP_KEY
 RowCount_from_prop=$(cat ${FILE_NAME} | grep "${PROP_KEY}" | cut -d'=' -f2 | tr -d '\r')
 echo "RowCount_from_prop" $RowCount_from_prop
 
 # Get row count from csv file
 RowCount_from_csv=$(awk 'END { print NR-1 }' $file)
 echo "RowCount_from_csv= "$RowCount_from_csv

 if [ "$RowCount_from_csv" == "$RowCount_from_prop" ]
 then
  echo "column header count row proper"
 else
  echo "column header count not correct in " $file
  ret_flag=1
 fi
done
if [ $ret_flag -eq 1 ]; then
 echo "exiting with errors"
 exit 1
else
 exit 0
fi

I'm not able to get value in RowCount_from_prop. echo is showing only blanks :( upto cat command its working fine. but at grep dunno whats happening.

Code:

1_rrc_attribute.csv
FILE_NAME  1_rrc_Record_Count.txt
PROP_KEY 1_rrc_attribute.csv
RowCount_from_prop
RowCount_from_csv= 203
column header count not correct in  1_rrc_attribute.csv

Can anyone help me out.
Thanks and regards,

MensaWater 01-30-2012 08:21 AM

Maybe the AIX version of grep or ksh doesn't like the quotes around the variable?

Code:

grep "${PROP_KEY}"
What happens at command line if you do:
PROP_KEY=test
echo $PROP_KEY
echo ${PROP_KEY}
echo "${PROP_KEY}"

Typically I only use double quotes with egrep (a/k/a grep -E). Maybe the AIX version doesn't expect the quotes for standard grep. If so maybe use egrep (or grep -E) with the quotes works?

sukhdip 01-30-2012 08:35 AM

I also used without the quotes but nothing works. Lemme try above one will let you know

sukhdip 01-30-2012 08:37 AM

using
echo $PROP_KEY
echo ${PROP_KEY}
echo "${PROP_KEY}"
its giving the same output...:( correct prop_key name

TB0ne 01-30-2012 10:55 AM

Quote:

Originally Posted by sukhdip (Post 4588312)
using
echo $PROP_KEY
echo ${PROP_KEY}
echo "${PROP_KEY}"
its giving the same output...:( correct prop_key name

AIX isn't typically a 'normal' version of Unix. Read the man page on the AIX grep utility (and the cut/tr commands too), and you'll probably spot differences. There are a few ways to address it:
  • Write your script to check the platform, and use a different grep/cut/tr command(s) if you're on AIX
  • Write a script especially for AIX
  • Install the GNU versions of those utilities on AIX, and use them.

MensaWater 01-30-2012 01:31 PM

Ok it must be something the shell is doing.

I noticed you're doing a cat of file and piping to grep - this isn't necessary - you can just grep the file.

What happens if you change:
Code:

RowCount_from_prop=$(cat ${FILE_NAME} | grep "${PROP_KEY}" | cut -d'=' -f2 | tr -d '\r')
to:
Code:

RowCount_from_prop=$(grep "${PROP_KEY}" ${FILE_NAME} | cut -d'=' -f2 | tr -d '\r')

sukhdip 01-31-2012 12:24 AM

Quote:

Originally Posted by MensaWater (Post 4588555)
Ok it must be something the shell is doing.

I noticed you're doing a cat of file and piping to grep - this isn't necessary - you can just grep the file.

What happens if you change:
Code:

RowCount_from_prop=$(cat ${FILE_NAME} | grep "${PROP_KEY}" | cut -d'=' -f2 | tr -d '\r')
to:
Code:

RowCount_from_prop=$(grep "${PROP_KEY}" ${FILE_NAME} | cut -d'=' -f2 | tr -d '\r')

I tried with above changes also.but result remains same. Still i'm not able to get RowCount_from_prop.:(:( any other alternate that can replace grep. coz i guess grep is the only one creating problem here. BUT I have same logic in some other script, WHERE its WORKING pretty fine without any issue. I'm posting a bit code here of other script.
Code:

#!/usr/bin/ksh

run_count=1
catalog_name=rrc
PROPERTY_FILE_NAME="/Dataload_Scripts/Properties/"$catalog_name"_Mandatory_Field.prop"
cd /Dataload_Scripts/Files/$catalog_name
pwd
for file in $run_count"_"*.csv; do
 echo "file name "$file
 PROP_KEY=""
 PROP_KEY=`echo $file | cut -c3-`
 echo "PROP_KEY= "$PROP_KEY
 Property_Value=""
 Property_Value=$(cat ${PROPERTY_FILE_NAME} | grep "${PROP_KEY}" | cut -d'=' -f2)
 echo "Property_Value = "$Property_Value

Above code is working perfectly in same system same OS i.e., AIX.
Now whats the problem it has with same grep command in other script.

Any solutions?? :(

sukhdip 01-31-2012 02:29 AM

Thanks all for your support.. Solved the problem. it was with the input text file.:)

Thanks & Regards,


All times are GMT -5. The time now is 09:44 PM.