ksh - test if string contains alphanumeric...

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh - test if string contains alphanumeric...
# 1  
Old 10-15-2007
Bug ksh - test if string contains alphanumeric...

Okay I will let users input spaces as well Smilie

I am having a mental block. I have done a couple of searches but havent found anything that I understand (the likes of :alpha: and awk).

Basically I want to give the user an option to enter some text which will go down as a field within a flat file ( which is delimeted by : ).

I want to limit this to 50 characters (I believe I can use typeset -z50 for this)

I also just want a-z,A-Z,(spaces) and numbers. Whats the best way to validate the input?
# 2  
Old 10-15-2007
one way is to use tr to remove unwanted character types. -dc removes everything except those char types you specify. [:alnum;] is alphanumeric, so if you check the length of the string it should be unchanged if it contains only the char types you want.
# t is the input string, ck is a variable to check the contents of t

ck=$( echo "$t" | tr -dc '[:alnum:]')
if [[ ${#t} -eq ${#ck} ]]; then
      echo "ok"
      echo "not ok"
      exit 1
# t is all good chars at this point
# check length of t

if [[ ${#t} -gt 50 ]]; then
      echo "not ok too long"

# 3  
Old 10-16-2007

Thanks for the response. Having tested the code thi doesn;t allow me to have spaces within the string. Any ideas?

Also what does the # mean within ${#t}?

Thank you
# 4  
Old 10-16-2007
Think I've cracked it

I am using this code. The tests I have done appear to work.

while true
echo "Enter Text: \c"
read t
case $t in
+([a-z]|[A-Z]|[0-9]|[' ']))
echo test string is okay
echo test string not okay
sleep 2
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate a string of alphanumeric characters

Hi, I want a script of a code that will allow me to generate all possible combinations of alphanumberica characters of length 12 such that each string will contain numbers and either small or capital letters. For example a string may look like this: 123AB45cd678. (11 Replies)
Discussion started by: faizlo
11 Replies

2. UNIX for Beginners Questions & Answers

Need to compare numbers in alphanumeric string

Hi, I will be having file names like below, 1420SP1.01804 1420SP1.01805D 1420SP1.01805 1420SP1.01806D 1420SP1.01806 1420SP1.01901D 1420SP1.01901 1420SP1.01902D 1420SP1.01902 1420SP1.01903D 1420SP1.01903 1420SP1.01904 1420SP1.01905 From this, I need to list file names which is... (3 Replies)
Discussion started by: Sumanthsv
3 Replies

3. Shell Programming and Scripting

how to insert space in alphanumeric string

Hi everyone, I want help to insert space between digits and letters in a alphanumeric string. INPUT TRY234TER PHY1TYR EXPECTED OUTPUT TRY 234 TER PHY 1 TYR The lines always begin with the letters and the alphabets will be a three letter combination before and after the number. The... (2 Replies)
Discussion started by: kaav06
2 Replies

4. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

5. Shell Programming and Scripting

ksh to compare alphanumeric values from 2 files

Hi there, I want to compare 2nd column which are alphanumeric values from each of the 2 files i.e.,lspv_pre.out and lspv_post.out , if found echo some message. lspv_pre.out hdisk0 00c39eaa451144dd rootvg active hdisk1 00c39eaa45223322 ... (3 Replies)
Discussion started by: mbak
3 Replies

6. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

7. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

8. Shell Programming and Scripting

test if a filename exists with specified string (ksh)

I'm trying to do a simple if statement that tests if a filename exists with a user specified string. So say I have these files: Assigned_1day_after_due_chuong Assigned_1day_after_due_gallen Assigned_1day_after_due_heidenre and i'm running a script and want to know if a... (6 Replies)
Discussion started by: bob122480
6 Replies

9. UNIX for Dummies Questions & Answers

AlphaNumeric String Operations

Hi :) I am writing a ksh I have a string of general format A12B3456CD78 the string is of variable length the string always ends with numbers (here it is 78.. it can be any number of digits may be 789 or just 7) before these ending numbers are alphabets (here it is CD can even be... (3 Replies)
Discussion started by: lakshmikanth
3 Replies

10. Shell Programming and Scripting

matching alphanumeric string

how to match an alphanumeric string like the following. i have to do like the following. if the input line is the data is {clock_91b} i have to replace that with the string was ("clock_91b") i tried like $line =~ s/the data is\s+\{(+)\}/the string was \(\"$1\"\)/ which... (4 Replies)
Discussion started by: sskb
4 Replies
Login or Register to Ask a Question