String Validation program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String Validation program
# 1  
Old 03-13-2006
String Validation program

Hi

I want to validate the sting which one having only A-Z, a-z, *, . ,_ and 0-9 digits. Can anyone send me the program? I tried with following program but its taking all special characters like @ , # % and ^.

echo " Enter Text :"
read text
while [ ! $(echo "$text" | tr -dc '[A-za-z0-9]') ]
do
echo "Character is wrong"
done
# 2  
Old 03-13-2006
Do you know about POSIX classes - maybe something using tr -<whatever> [:alnum:]

[A-z] lets some punctutation like ^ "through"

Plus, unless you turn off globbing, the * character will not come through as *, it will come through as a listing of files in the current directory.
# 3  
Old 03-13-2006
I had this script for sometime. Check it out.

Code:
[/tmp]$ cat string.ksh 
#! /bin/ksh 

set -f

(($#)) && STR="$@" || STR="abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}"
STRING="$STR"
typeset -R1 last
typeset -L1 first

while ((${#STR})) 
do
        first="${STR}"
        last="${STR}"

        [[ "$first" = [a-zA-Z0-9\.\_\*] ]] || { echo "Unwanted $first" ; exit 1 ; }
        [[ "$last" = [a-zA-Z0-9\.\_\*] ]] || { echo "Unwanted $last" ; exit 1 ; }

        STR="${STR%$last}"
        STR="${STR#$first}"

        ((${#STR} == 1)) && break;

done
        echo "$STRING looks fine."

Like jim has said, you need to turn off globbing or invoke the script as

Code:
sh -c 'set -f; ./string.ksh abcd@#$'

where abcd@#$ is the string you are trying to validate. All characters you wish to keep should go inside the [a-zA-Z0-9\.\_\*] construct.
# 4  
Old 03-15-2006
Hi,
Thanx to all...

The given program is working fine but typeset -R and -L values are not working in linux machine. Is there any other variables to declare this typeset for string in linux?

Regards
mpk
# 5  
Old 03-15-2006
Linux isn't the problem, it's your shell. Some linux distros use pdksh but this supports typeset -L and -R as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C program to read a binary file and search for a string?

Hi, I am not a C programmer. The only C exposure I have is reading and completing the exercises from the C (ANSI C ) Programming Language book:o At the moment, I am using the UNIX strings command to extract information for a binary file and grepping for a particular string and the value... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

String validation in shell script

Hi All, I am a newbie...I would like to have a function which ll check if a file contains valid strings before "=" operator. Just to give you my requirement: assume my file has content: hello= gsdgsd sfdsg sgdsg sgdgdg world= gggg hhhh iiiii xxxx= pppp ppppp pppp my... (1 Reply)
Discussion started by: rtagarra
1 Replies

3. Programming

segfault in pointer to string program

hello all, my question is not about How code can be rewritten, i just wanna know even though i am not using read only memory of C (i have declared str) why this function gives me segfault :wall:and the other code executes comfortably though both code uses same pointer arithmetic. ... (4 Replies)
Discussion started by: zius_oram
4 Replies

4. Shell Programming and Scripting

extract string and sending it into a new file in perl program

Hi, I have a input file with following values (test.out) I would like to grep all lines with word 'PANIC' and sent it another file using perl program with grep command. I have been trying different ways and not working. Pls advice. Thanks a lot for the help. --example--... (3 Replies)
Discussion started by: hudson03051nh
3 Replies

5. UNIX for Dummies Questions & Answers

Unix shell program that reverse a string...

Thanks so much vivekraj (0 Replies)
Discussion started by: andrew1400
0 Replies

6. Programming

How to trim the white space around a string in C program

I am coding a C program to read a plain text file. There are a lot of blank fields or a string with white spaces. I want to know is there such a function called trim() in C to clean the white space around a string? Or some other way can do this efficiently? Thanks. (18 Replies)
Discussion started by: hxm1303
18 Replies

7. Filesystems, Disks and Memory

shell program to reverse the string

pls help me in getting that program (1 Reply)
Discussion started by: saikiran
1 Replies

8. AIX

UserID password validation using C program

I am new to AIX. I was wondering if there is a security API on AIX which I can call from my C program to validate the userID and password of a user. My plan is to have my C program prompt the user for UserID and password. I'll then call the AIX security API to determine what authority the user... (1 Reply)
Discussion started by: AIX_user
1 Replies

9. Shell Programming and Scripting

String validation

All I want to validate a String. If it is "Deepak Xavier" then it will a valid string. But if the value "Deepak #&xavier" then it should be invalid. Please give me some commands. Iam using KORN shell. Thanx in advance. Regards Deepak Xavier (3 Replies)
Discussion started by: DeepakXavier
3 Replies

10. Shell Programming and Scripting

string validation on a column

Hi there I have a file that I recieve that looks like the folowing, As these details are being manually entered by the customer, we sometimes get a few typos on 4th column (the 14 digit number starting with 42....) As you can see the first one has a space as the first character and then a 13 number... (3 Replies)
Discussion started by: hcclnoodles
3 Replies
Login or Register to Ask a Question