Value validation

Hi All

I am trying to validate a value using if condition

requirement is need to check whether its a valid numeric value

the input contains ( space, #N/A and negative and positive decimal values and Zeros)

if it contains the space, I need to display the error message as space

and the same applies for #N/A and negative values and zeros

I have tried a different ways but no joy

Please provide me the help on ths

Assuming your variable is called A:

if [[ "$A" =~ " " ]]; then
  echo got a space
elif [[ ... ]]
...
fi

It might be better to use a case-statemnt for this, depending on how many patterns you are looking for.

edit: Moved thread. This has nothing to do with Solaris.

I am getting the below error when I tried that

syntax error at line 4 : `=~' unexpected

Please advice

As you didn't mention the shell you're using, we're urged to guess. Above was a bashism. Try this for testing for negative number strings:

[ "$A" = "${A//-}" ] && echo OK || echo NOK

You need to test for all your patterns, though.

Thanks for that

I am using ksh on sun solaris

Thats throwing an error

"${A//-}": bad substitution

I need the help to validate the value either using if or case, please provide for space and no-numeric and special character values

This is my bad, I moved the thread from the Solaris forum, and didn't consider the OS when submitting my code.

I would use a case statement, in that case.

case "$var" in
  " ") echo got a space;;
  "N/A") echo got NA;;
  -[0-9]*) echo negative;;
  ...
esac

I replaced my only Solaris server, with Oracle on it, for a Red Hat one, so I can't test specifically in KSH on Solaris.

Perhaps an old ksh version? According to the man page, ksh93 should have above parameter expansion.

I am giving more details about my requirement
I got a file which has two columns, first column is of city and the second is of temperature
I search for the city and get the temperature value and dsplay based on that value
If temperature might be a space or number of space or non numeric or a negative value......I need to display what value
it is and change it to in all the above cases
if it is a positive value ( either decimal) then I need to used it for addition or subtraction
as per your above, it seems you need to give each value seperatly and it is exactly checking for that paticular value,
say suppose

" ") echo got a space;;

its only checking for one space, if i get more spaces its not considering

 length=`echo $temp | wc -c`
if [ $length -le 0 ]
then
temp=0
fi
if [ $temp -lt 0 ]
then
echo "$city Negative temp"
temp=0
else
if [ $temp = 0 ]
then
echo "$city No temp" temp=0
fi
fi
 

Please advice

Show us your sample input file and expected output.

Input

Newyork -10
London 20
Mumbai #N/A
Dubai 
Paris 10.45

Output

Newyork Negative value
London 20
Mumbai Invalid value
Dubai No value
Paris 10.45

Assuming that this is processing a file, then there is a high cost to calculating the length this way. Have you considered:-

length="${#temp}"

You should also consider a case statement to branch to appropriate code as if...then...else....if....fi....fi can be confusing to read through. If you only have three options, it would be neater as a start to have something like:-

for val in a b c a b a
do
   if [ $val = a ]
   then
      echo val=a
   elif [ $val = b ]
   then
      echo val=b
   else
      echo other
   fi
done

Overall it would be better to write it as:

for val in a b c a b a
do
   case "$val" in
      a) echo val=a ;;
      b) echo val=b ;;
      *) echo other ;;
   esac
done

Can you adapt this to fit your input and generate your output? You might need to slice up your variable and work on the first character, so if you are holding the the value val then the first character is "${val%${val#?}"

Does this help?

Robin
Liverpool/Blackburn
UK

$ cat file
Newyork -10
London 20
Mumbai #N/A
Dubai
Paris 10.45
$ awk '$2~/[0-9]/ && $2<0{$2="Negative value"}!$2{$2="No value"}$2~/^#.*/{$2="Invalid value"}1' file
Newyork Negative value
London 20
Mumbai Invalid value
Dubai No value
Paris 10.45

thanks for that.

But its throwing the error

awk: syntax error near line 1
awk: bailing out near line 1

Solaris ? then use nawk

Slightly longer than a magical awk but perhaps more readable and/or flexible depending what you want to do next:-

while read label val
do
   valtest="${val%${val##?}}"                  # Get first character of value

   case "$valtest" in
      "-")   echo "$label Negative value"   ;;
      "#")   echo "$label Invalid value"    ;;
      [0-9]) echo "$label $val"             ;;
      "")    echo "$label No value"         ;;
      *)     echo "$label Unexpected value" ;;
   esac

done < file

I hope that this helps. I've tested in on HP-UX 11.11, RHEL 6.1 & AIX 4.3.3 to 6.1

Robin
Liverpool/Blackburn
UK

No joy, I have tried it and it is not producing any output