Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to search non-numeric charaters with in a variable.. 1

Status
Not open for further replies.

buccaneers

Programmer
Jun 3, 2004
28
US
Hello All,

I am stuck at a point where in i am to check for any non-numeric charaters in a variable. Is there a way to do it. I want to avoid using multiple if checks.

Basically i expect a variable to $VAR to contain only numeric values and if it contains any non numeric value it should exit.

I tried using echo $VAR | grep -cve[0-9]

but this one fails when VAR=Abc-123

Is there a way to do this in one line along the line of using nawk/awk.... I am sure there is a simple solution to it which i am unable to think of.

Any ideas/ solutions ?

Appreciate ur feedback
 
You are close. You need to construct your regex to say "starts with one or more digits until the end of the string"

In short you are missing the "^" and "$" anchors.

Use egrep so you can use the "+" (which means 1 or more) and the "?" (which means zero or one):

[[ $(echo "$VAR" | egrep -v "^-?[0-9]+$" ]] && echo "$VAR is all digits" || echo "$VAR is not a valid number"

OR use grep with the "*" like this:

[[ $(echo "$VAR" | grep -v "^[-0-9][0-9]*$" ]] && echo "$VAR is all digits" || echo "$VAR is not a valid number"

Both regex will handle negative numbers
 
Thx dkyrtata.

I still didn't understand the regex part.

Although following seems to be working

VAR="zp#- 123"
echo $VAR| egrep -v "^-?[0-9]+$" #==> displays $VAR if and only if it has some nonnumeric values

and

echo $TKT | egrep "^-?[0-9]+$" #==> doesn't display $VAR is it has only numeric values

need to get more info on regular expression in general. Something new.
 
Hi

buccaneers said:
need to get more info on regular expression in general.
If you only want digits ( I mean no sign ), then you can solve it without regular expression : remove all digits and if anything remains, that is non-digit :
Code:
[teal][[[/teal] [green][i]"$( echo "[/i][/green][navy]$VAR[/navy][green][i]" | tr -d '[0-9]' )"[/i][/green] [teal]]][/teal] [teal]&&[/teal] echo [green][i]"$VAR is not a valid number"[/i][/green] [teal]||[/teal] echo [green][i]"$VAR is all digits"[/i][/green]

Feherke.
 
Another way without any external command:
Code:
case $VAR in
*[!0-9]*) echo "'$VAR' invalid";;
*) echo "'$VAR' OK";;
esac

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
buccaneers

you did not initialize your variable, $TKT.

Here is another solution that will work with the Korn Shell. It too handles negative numbers. It is probably more efficient than most of the other solutions:

[[ "$VAR" = ?(-)+([0-9]) ]] && echo "$VAR is numeric" || echo "$VAR not numeric"

The implementation of regex of the korn shell is a bit different in that the expression is inside round brackets and the quantifiers, "?" and "+" are before the expression - not after.

In short, a regex is simply an expression that makes use of wild-cards (. * + ?[] ()^ $) to describe a string. Different programs (such as grep, sed, awk, Perl) support different wildcards. Some may even implement the same wildcard a bit differently thereby yielding different results for a particular regex.

Arguably, the best implementation of regex are those in Perl. So if you are to learn about them, pick up a Perl book, unless you need them with another language you use regularly (like Java or Oracle SQL).



 
Hi

dkyrtata said:
The implementation of regex of the korn shell is a bit different in that the expression is inside round brackets and the quantifiers, "?" and "+" are before the expression - not after.
Just to not confuse a beginner : those are patterns, not regular expressions.

( The difference is better visible in Bash where both patterns ( as in your 27 Oct 10 13:26 code ) and regular expressions ( as in your 26 Oct 10 18:51 code ) are available : between [tt][[[/tt]..[tt]]][/tt] the patterns are evaluated with [tt]=[/tt] operator and the regular expressions with [tt]=~[/tt] operator. )


Feherke.
 

The term regular-expression (regex) and "pattern" are often used interchangeably.

However, I would define "pattern" as the text being searched and "regex" as the language to find the pattern.

The "=~" operator is the means of telling the Perl interpreter that a regex follows. Likewise, the "[ [] ]" brackets tells the Korn shell interpreter that the enclosed string is a regex but not necessarily the same type of regex that is recognized by Perl, grep, or sed and other programs. Different programs support different variations of regular expressions. At times in can be difficult to keep track of what is supported by each. It is safe to say the Perl's are the most versatile which many languages try to model after.

The reasons for the differences are rooted to the beginnings of Unix. Unix was intended for internal usage at Bell Labs. The various programs, grep, sed, awk were written by different programmers to address their immediate needs. There was few common standards.

When Perl was written decades later, it implemented the best features of all these programs. It was intended to help programmers write code quickly and easily
 
Thanks dkyrtata, feherke and phv. Really appreciate your responses.

dkyrtata -- Apologies. I typo'ed on $TKT instead of $VAR. then I was multi-tasking and working two scripts.$TKT was variable in the second one. :(

Again thanks a lot to all.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top