Letters, Numbers or Alphanumerical


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Letters, Numbers or Alphanumerical
# 1  
Old 09-13-2003
Letters, Numbers or Alphanumerical

How do I check if a variable consisted of letters, numbers or both letters and numbers?

For example, I have a variable $X and I want to print "1" if it contains only letters, "2" if it contains only numbers and "3" if it contains both
# 2  
Old 09-13-2003
typical regex problem

This is a bit of a FAQ, worth searching for.

My slant is to advise using regular expressions
Research regexes (regular expressions) for the environment you are using.
In most environments (perl, bash, C) you will find the character classifiers 'isalpha', 'isdigit' etc very helpful.
# 3  
Old 09-17-2003
Courtesy of Mastering Unix Shell Scripting Script is free to download and manipulate for your own purpose.
Code:
#!/bin/ksh
#
#  SCRIPT: test_string.ksh
#  AUTHOR: Randy Michael
#  REV: 1.0.D  - Used for developement 
#  DATE: 10/15/2002
#  PLATFORM: Not Platform Dependent
#
#  PURPOSE: This script is used to test a character
#           string, or variable, for its composition.
#           Examples include numeric, lowercase or uppercase
#           characters, alpha-numeric characters and IP address.
#
#  REV LIST:
#
#
# set -x # Uncomment to debug this script
# set -n # Uncomment to verify syntax without any execution.
#        # REMEMBER: Put the comment back or the script will
#        # NOT EXECUTE!
#
####################################################
############## DEFINE FUNCTIONS HERE ###############
####################################################

test_string ()
{
# This function tests a character string

# Must have one argument ($1)

if (( $# != 1 ))
then
     # This error would be a programming error

     print "ERROR: $(basename $0) requires one argument"
     return 1
fi
# Assign arg1 to the variable --> STRING

STRING=$1

# This is where the string test begins

case $STRING in

+([0-9]).+([0-9]).+([0-9]).+([0-9]))
         # Testing for an IP address - valid and invalid
         INVALID=FALSE

         # Separate the integer portions of the "IP" address
         # and test to ensure that nothing is greater than 255
         # or it is an invalid IP address.

         for i in $(echo $STRING | awk -F . '{print $1, $2, $3, $4}')
         do
             if (( i > 255 ))
             then
                  INVALID=TRUE
             fi
         done

         case $INVALID in
         TRUE) print 'INVALID_IP_ADDRESS'
               ;;
        FALSE) print 'VALID_IP_ADDRESS'
               ;;
         esac
         ;;
+([0-1])) # Testing for 0-1 only 
         print 'BINARY_OR_POSITIVE_INTEGER'
         ;;
+([0-7])) # Testing for 0-7  only
         print 'OCTAL_OR_POSITIVE_INTEGER'
         ;;
+([0-9])) # Check for an integer
         print 'INTEGER'
         ;;
+([-0-9])) # Check for a negative whole number
          print 'NEGATIVE_WHOLE_NUMBER'
          ;;
+([0-9]|[.][0-9]))
          # Check for a positive floating point number
          print 'POSITIVE_FLOATING_POINT'
          ;;
+(+[0-9][.][0-9]))
          # Check for a positive floating point number
          # with a + prefix
          print 'POSITIVE_FLOATING_POINT'
          ;;
+(-[0-9][.][0-9]))
          # Check for a negative floating point number
          print 'NEGATIVE_FLOATING_POINT'
          ;;
+([-.0-9]))
          # Check for a negative floating point number
          print 'NEGATIVE_FLOATING_POINT'
          ;;
+([+.0-9]))
          # Check for a positive floating point number
          print 'POSITIVE_FLOATING_POINT'
          ;;
+([a-f])) # Test for hexidecimal or all lowercase characters
         print 'HEXIDECIMAL_OR_ALL_LOWERCASE'
         ;;
+([a-f]|[0-9])) # Test for hexidecimal or all lowercase characters
         print 'HEXIDECIMAL_OR_ALL_LOWERCASE_ALPHANUMERIC'
         ;;
+([A-F])) # Test for hexidecimal or all uppercase characters
         print 'HEXIDECIMAL_OR_ALL_UPPERCASE'
         ;;
+([A-F]|[0-9])) # Test for hexidecimal or all uppercase characters
         print 'HEXIDECIMAL_OR_ALL_UPPERCASE_ALPHANUMERIC'
         ;;
+([a-f]|[A-F]))
         # Testing for hexidecimal or mixed-case characters
         print 'HEXIDECIMAL_OR_MIXED_CASE'
         ;;
+([a-f]|[A-F]|[0-9]))
         # Testing for hexidecimal/alpha-numeric strings only
         print 'HEXIDECIMAL_OR_MIXED_CASE_ALPHANUMERIC'
         ;;
+([a-z]|[A-Z]|[0-9]))
         # Testing for any alpha-numeric string only
         print 'ALPHA-NUMERIC'
         ;;
+([a-z])) # Testing for all lowercase characters only
         print 'ALL_LOWERCASE'
         ;;
+([A-Z])) # Testing for all uppercase numbers only
         print 'ALL_UPPERCASE'
         ;;
+([a-z]|[A-Z])) 
         # Testing for mixed case alpha strings only
         print 'MIXED_CASE'
         ;;
         *) # None of the tests matched the string coposition
            print 'INVALID_STRING_COMPOSITION'
         ;;
esac
}


####################################################

usage ()
{
echo "\nERROR: Please supply one character string or variable\n"
echo "USAGE: $THIS_SCRIPT {character string or variable}\n"
}

####################################################
############# BEGINNING OF MAIN ####################
####################################################

# Query the system for the name of this shell script.
# This is used for the "usage" function.

THIS_SCRIPT=$(basename $0)

# Check for exactly one command-line argument

if (( $# != 1 ))
then
     usage
     exit 1
fi

# Everything looks okay if we got here. Assign the
# single command-line argument to the variable "STRING"

STRING=$1

# Call the "test_string" function to test the composition
# of the character string stored in the $STRING variable.

test_string $STRING

# End of script

added code tags for readability --oombera

Last edited by oombera; 02-17-2004 at 04:16 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

[FUN] Numbers to Roman letters/num

Heyas Just a little fun script (code block) i'd like to share for fun. #/bin/bash # roman.sh # # Function # num2roman() { # NUM # Returns NUM in roman letters # input=$1 # input num output="" # Clear output string len=${#input} # Initial length to count down ... (9 Replies)
Discussion started by: sea
9 Replies

3. UNIX for Dummies Questions & Answers

sed - extract a group of Letters/numbers

I have a file with hundreds of lines in it. I wanted to extract anything that matches the following: KR followed by 4 digits: example KR1201 cat list | sed "s///g" Is the closest I've come, and obviously it is not what I want. This would remove all of the items that I want and leave me... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. Shell Programming and Scripting

Sorting mixed numbers and letters

Hello, I have a file such as this: chr1 chr2 chr1 chr2 chr3 chr10 chr4 chr5 chrz chr1AI want to sort it, I use this command: sort -k1 -th -n testfilebut I get this output, how can I fix this? chr1 chr1 chr10 chr1A chr2 chr2 (3 Replies)
Discussion started by: Homa
3 Replies

5. Shell Programming and Scripting

awk : match only the pattern string , not letters or numbers after that.

Hi Experts, I am finding difficulty to get exact match: file OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE="" DHCP_ENABLE=0 INTERFACE_NAME="lan3:1"... (6 Replies)
Discussion started by: rveri
6 Replies

6. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

7. Shell Programming and Scripting

reducing values in columns with both numbers and letters

Hi, I columns with both number and letters however i need the number 4 trimmed off the lines that have 3 numbers in them so it just because the 2 preceding numbers only For example V25QG2-K18QG-V25CG2 L26HG-L17HA-L26CG I434QD1-L19HB2-I434CD1 I434QD1-A31QB-I434CD1 ... (7 Replies)
Discussion started by: olifu02
7 Replies

8. Shell Programming and Scripting

sed command, look for numbers following letters

If I have a set of strings, C21 F231 H42 1C10 1F113 and I want to isolate the ints following the char, what would the sed string be to find numbers after letters? If I do, *, I will get numbers after letters, but I am looking to do something like, sed 's/*/\t*/g' this will give me... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

9. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

10. UNIX for Dummies Questions & Answers

Help! scrolling numbers and letters

Hello all I am a unix newbie.... I have a sun netra t1 and it is freaking out I am connected to it through a console port, and it is just spitting out a ton on numbers and letters like below its just keeps going and going. I have tried rebooting it and I cannot get it back to any kind of a... (1 Reply)
Discussion started by: intraining11
1 Replies
Login or Register to Ask a Question