1753524 Members
5061 Online
108795 Solutions
New Discussion юеВ

"" help

 
SOLVED
Go to solution
Pando
Regular Advisor

"" help

Dear Gurus,

I have file with the following content

....
OPERATOR,""
....

Sometimes, this field contains numbers.
What i would like to do is test the content if
it is "" or a number or string.

Maximum points to all correct answers.
6 REPLIES 6
RAC_1
Honored Contributor

Re: "" help

You mean to say that sometimes OPERATOR,"" (without double quotes) there might be a number and you want to test that?
There is no substitute to HARDWORK
Peter Godron
Honored Contributor

Re: "" help

Fernando,
if you assume a file format of:
Operator,""
Operator,"alpha"
Operator,"Alpha"
Operator,"123"

running the script:

# /usr/bin/sh
while read record
do
data=`echo $record | cut -d',' -f2|tr -d '"'`
echo "[$data]"
case ${data} in
[0-9]*) echo valid number entered
;;
[a-z]*) echo valid string entered
;;
[A-Z]*) echo valid string entered
;;
*) echo empty data
;;
esac
done < d.dat

Gives you:
[]
empty data
[alpha]
valid string entered
[Alpha]
valid string entered
[123]
valid number entered

Beware this script only checks the first character of the data given, so 1a2 would give you a number return !
Steve Lewis
Honored Contributor
Solution

Re: "" help

Do you mean like this?
if [ -z "$OP" ]
then echo "its empty"
else
if [ $OP -eq 0 ]
then echo "its a zero"
else echo "Its not a zero"
fi
fi


[dba]blprd:/home/dba>OP=""
[dba]blprd:/home/dba> if [ -z "$OP" ] ^J then echo "its empty" ^J else^J if [ $OP -eq 0 ] ^J then e>
its empty
[dba]blprd:/home/dba> OP=123
[dba]blprd:/home/dba> if [ -z "$OP" ] ^J then echo "its empty" ^J else^J if [ $OP -eq 0 ] ^J then e>
Its not a zero
[dba]blprd:/home/dba> OP=0
[dba]blprd:/home/dba> if [ -z "$OP" ] ^J then echo "its empty" ^J else^J if [ $OP -eq 0 ] ^J then e>
its a zero

RAC_1
Honored Contributor

Re: "" help

var=$(grep ^'OPERATOR\,\"\"' your_file|awk -F '\,' {print $2}')
if [ $(echo ${var}|tr -d '[0-9]') = "" ];then
echo "Yes, contains number"
else
echo "No number"
fi

There is no substitute to HARDWORK
john korterman
Honored Contributor

Re: "" help

Hi Fernando,


if you want to distinguish between three cases, try something like this, using your file as $1:

#!/usr/bin/sh

LINE=$(grep OPERATOR $1)
REST=${LINE##OPERATOR,}

echo "$REST" | grep -q "\"\"" 1>/dev/null
if [ "$?" = "0" ]
then
content=quoutes
else
echo "$REST" | grep -q "[0-9]" 1>/dev/null
if [ "$?" = "0" ]
then
content=numbers
else
# if no number present, assume string
content=string
fi
fi
echo $content



regards,
John K.
it would be nice if you always got a second chance
Pando
Regular Advisor

Re: "" help

Thanks the help. :-)
This forum is really great!