LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2012, 08:02 AM   #1
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Rep: Reputation: Disabled
Question 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,
 
Old 01-30-2012, 08:21 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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?
 
Old 01-30-2012, 08:35 AM   #3
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Original Poster
Rep: Reputation: Disabled
I also used without the quotes but nothing works. Lemme try above one will let you know
 
Old 01-30-2012, 08:37 AM   #4
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Original Poster
Rep: Reputation: Disabled
using
echo $PROP_KEY
echo ${PROP_KEY}
echo "${PROP_KEY}"
its giving the same output... correct prop_key name
 
Old 01-30-2012, 10:55 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by sukhdip View Post
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.
 
Old 01-30-2012, 01:31 PM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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')
 
1 members found this post helpful.
Old 01-31-2012, 12:24 AM   #7
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by MensaWater View Post
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??
 
Old 01-31-2012, 02:29 AM   #8
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Original Poster
Rep: Reputation: Disabled
Thanks all for your support.. Solved the problem. it was with the input text file.

Thanks & Regards,
 
1 members found this post helpful.
  


Reply



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
LXer: Java on AIX: Data Collection For AIX Core Dumps LXer Syndicated Linux News 0 05-17-2007 06:46 PM
System hangs in update of AIX from 4.3.3 to AIX 5.2 jmurray67 AIX 2 07-25-2004 08:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:07 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
Open Source Consulting | Domain Registration