Operating System - HP-UX
1847052 Members
5068 Online
110261 Solutions
New Discussion

Re: ignoring null values while greping

 
SOLVED
Go to solution
N Gopal
Frequent Advisor

ignoring null values while greping

Hi,

I have one file say:

myfile.txt, entries are:

"1234","0001"
"1235"," "
"1236"," "
"1237","0002"

I have writen code:

while read line
do
deep_cd= "NULL"
deep_cd=`grep "$deep_code" myfile.txt | cut -f2 -d','`
if [ deep_cd = null value ] then
deep_cd=NULL
else
echo " Value of deep_cd is $deep_cd "


fi;
echo "$deep_cd" >> diw_output
done
Output file will be something like:

my_output:

"0001"
NULL
"0002"

Can some one please help me in this regard.

Thanks






3 REPLIES 3
Ralph Grothe
Honored Contributor

Re: ignoring null values while greping

You can test for "definedness" (rather zero length string values) in the shell by
e.g.
[[ -z $deep_cd ]] && deep_cd=NULL

Does this help?
Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor
Solution

Re: ignoring null values while greping

Hi:

This would yield your desired output using the input you showed:

#!/usr/bin/sh
IFS=","
while read A B
do
if [ "${B}" = '""' -o "${B}" = '" "' -o -z "${B}" ]; then
echo "NULL"
else
echo ${B}
fi
done < ./myfile.txt

...please cut-and-paste since the Forum will make this difficult to read...

Regards!

...JRF...
N Gopal
Frequent Advisor

Re: ignoring null values while greping

Hi All,

Appologies for late reply.
Thanks a lot i got answer of my query.

Thanks