Checking for Alphabets


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking for Alphabets
# 1  
Old 01-25-2010
Checking for Alphabets

Code:
echo -n "read this also:"
read NewAuthor


if [ -z $( echo "$NewAuthor" | tr -d '[[:digit:]]' ) ] ; then
echo "its a digit"
else
echo "something else"
fi

Hey guys , i am trying to do a search to check if the input is using alphabets and nothing else. I tried using [[:digit:]] and [[:alpha:]] but none seems to work


When i use digit, it read 22.k as alphabet and not as a wrong syntax.
For alpha , it does not allow me to input data which has spaces such as " hello world". how can i solve this issue.
# 2  
Old 01-25-2010
Code:
#!/bin/ksh
input="1234abc"
digits=$(echo "$input" | tr -dc '[:digit:]')
alpha=$(echo "$input" | tr -dc '[:alpha:]')
[[ ${#alpha} -eq ${#input} ]] && echo 'all alphabetic characters'
[[ ${#digits} -eq ${#input} ]] && echo 'all numeric characters'
[[ ${#alpha} -ne ${#digits} ]] && echo 'not all alpha and not all digits '

# 3  
Old 01-25-2010
thanks jim. But i would only like the search to allow those using alphabets only to be true.
If there is any "." or numbers or any other characters, it should return a false.
# 4  
Old 01-25-2010
The above example shows you how to detect a string that's all alphabetic characters. What else do you want?
# 5  
Old 01-25-2010
Sorry, what i meant is . when i entered "." or "" , it seems to give me an error saying ./test: line 8: [[: A time: syntax error in expression (error token is "time").

my test data was

#!/bin/ksh
input="abc time"
digits=$(echo "$input" | tr -dc '[:digit:]')
alpha=$(echo "$input" | tr -dc '[:alpha:]')
[[ ${#alpha} -eq ${#input} ]] && echo 'all alphabetic characters'
[[ ${#digits} -eq ${#input} ]] && echo 'all numeric characters'
[[ ${#alpha} -ne ${#digits} ]] && echo 'not all alpha and not all digits

why does it not read space ?
# 6  
Old 01-25-2010
It works fine for me in sh when you add the missing ' to the end of the last line. You are using ksh, which is radically different. Try replacing the [[ ]] with [ ] . Try making all the variables enclosed in ""'s. And reject empty input explicitly.

And lastly, code tags for code. You've done it before, you know how. Smilie

Last edited by Corona688; 01-25-2010 at 04:31 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace alphabets in a string with their ASCII values

hi. I have a requirement where I need to REPLACE all alphabets from an alphanumeric input string into their respective ASCII decimal value. For example: If the input string is ABAC123, the output should be 65666567123 I am seeking a single line command, and I was trying searching for options... (21 Replies)
Discussion started by: kumarjt
21 Replies

2. UNIX for Dummies Questions & Answers

Replace alphabets from certain positions

Hi all, I have column 2 full of values like HIVE4A-56 and HIVE4-56. I want to convert all values like HIVE4A-56 to HIVE4-56. So basically I want to delete all single alphabets before the '-' which is always preceded by a number. Values already in the desired format should remain unchanged... (4 Replies)
Discussion started by: ames1983
4 Replies

3. Shell Programming and Scripting

Print combinations of alphabets in a sequence

Hi Friends, I have a series of alphabets like this AGCAA The values inside the square brace indicate that either of them can be present at that position. And those ones without a brace, means that they are the only ones that could be printed at that location. Now, I would like to know... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. Shell Programming and Scripting

How to sort the numbers with symbol like '%' and alphabets '5M' and '52G'?

Hi all , I have below input 124M 56M 63M 48% /opt/isv/microsoft 1.1G 133M 922M 13% /opt/isv/mqm 5.0G 2.5G 2.3G 52% /opt/isv/oracle 30G 0 30G 0% /opt/isv/db my required output... (3 Replies)
Discussion started by: vinothsekark
3 Replies

5. Shell Programming and Scripting

filter unique alphabets

Filter unique alphabets (bold) from input Thanx +SRR015270.1 HWI-B10_3_6069:2:1:653:875 length=32 SZZZZZZZZZZZZXZZZXZZZOECZZIZHUEM +SRR015270.2 HWI-B10_3_6069:2:1:455:450 length=32 ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZTT @SRR015270.3 HWI-B10_3_6069:2:1:453:499 length=32... (2 Replies)
Discussion started by: repinementer
2 Replies

6. Shell Programming and Scripting

Filter certain number of alphabets

filter the ones (ex:>1279_17_27_F3) that have 50letters (ABABABACACACACACAADADADADABABABABAACACACACACACAACAC) in input. And others that are less than 50 have to be ignore and the ones with more than 50 have to trimmed to first 50 letters. Thanx >1279_16_1960_F3 A >1279_16_2010_F3 BCCC... (2 Replies)
Discussion started by: ruby_sgp
2 Replies

7. Shell Programming and Scripting

count alphabets in a flat file and print

I have a text file with a huge dataset, and each row in that dataset has some data(65479 rows). In that file I need to find the number of times a-z & A-Z Appears. How Can I Initialise Array into an Array to parse the count also I need to parse a,A into a single array preferably. example of... (3 Replies)
Discussion started by: vmsenthil
3 Replies

8. Shell Programming and Scripting

Unix Remove repetitive alphabets

Hi, I am trying to write a script that will take 2 or more instances of repetitive alphabets (ZZ) to be removed from a field. This should only happen from beginning and end of a field. For Example : Input File a) ZZZIBM Corporation b) ZZZIBM Corporation ZZZZZ b) IBM ZZZ... (26 Replies)
Discussion started by: msalam65
26 Replies

9. Shell Programming and Scripting

Omitting the last 2 alphabets in the words

Hi Guys, Bit new to Unix shell scripting so this question might seems little kiddish for you. what im trying to achieve here is : I have file which is compressed like Account_52320090605076_log.Z so in my shell script i call this file also as one of my parameters like ... (4 Replies)
Discussion started by: coolrekz
4 Replies

10. UNIX for Advanced & Expert Users

Extracting only Alphabets from a value

Hi, I have file name (abcd001). I want to extract on the alphabets from this file name. I don't want the numeric part of it. Once i extract the alphabets the i can search for all those file. Could anyone help on this. Thanks in advance (2 Replies)
Discussion started by: amitkhiare
2 Replies
Login or Register to Ask a Question