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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read alphanumeric characters 1

Status
Not open for further replies.

geffry

Programmer
Jun 26, 2002
33
US
Hi,

I need to validate a user entered input, the input must be alphanumeric characters.

What is the method to do in shell script. Should i grep for each non-alphanumeric character.


te1%@i -- is an invalid input .. how to detect . In perl it is easy.
 
hi,
may be we should make a faq out of this since I believe this is 4th time this has been asked.

The answer to your question is

NO. you don't grep for each non valid character.
you grep for NOT the legal characters.



echo "enter word"
set word = $<
echo $word | egrep -v '[a-zA-Z0-9]' > /dev/null
if ( $status == 0 ) then
echo &quot;bad word&quot;
else
echo &quot;good word&quot;
endif

The egrep will return status based on whether it finds any non legal characters. my sh is a little rusty but I think this is about the same...

echo &quot;enter word&quot;
read $word
echo $word | egrep -v '[a-zA-Z0-9]' > /dev/null
if [ $? == 0 ]; then
echo &quot;Bad word&quot;
else
echo &quot;good word&quot;
fi

 
It does not solve my problem..

if i give input as &quot;asd#s!&quot; ... it is saying as good word.

My whole word should have only alphanumeric characters..
 
Hi!

Try this:
egrep '[^A-Za-z0-9]'.
This will look for any line with any character not (^) in [A-Za-z0-9] and set status to 0 if at least one is found.
So the word 'asd#s!' is found invalid because it contains some invalid chars.

While
egrep -v '[A-Za-z0-9]'
looked for lines with not a single character from the set [A-Za-z0-9].
So the word 'asd#s!' was not found invalid because it contains at least one valid char.

Does this work ?
 
Sorry. can't follow my own advice. Yes ^ is better than -v. sorry about that.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top