tags:

views:

45

answers:

3

hi all

I need to verify if the second word from $line_from_file is a string with lowercase or uppercase characters

(WORD - string can be with numbers)

How to match WORD string? (I use this syntax in ksh script)

    [[  ` echo $line_from_file | awk '{print $2}' ` =   WORD    ]] && print "MATCH"

WORD - can be small or capital characters with numbers (but not only numbers)

for example

WORD=textBIG

WORD=HOME_DIR

WORD=COMPUTER1

WORD=HOST_machine

Lidia

A: 

You can use awk functions toupper (or tolower) to check this.

WORD == toupper(WORD) means word is in uppercase

FabienAndre
A: 
$ shopt -s extglob
$ s="abCD123"
$ case "$s" in +([0-9]) ) echo "not match" ;;+([-a-zA-Z0-9_]) ) echo "match";; esac
match
$ s="12345"
$ case "$s" in +([0-9]) ) echo "not match" ;;+([-a-zA-Z0-9_]) ) echo "match";; esac
not match
$ s="a12345-asf"
$ case "$s" in +([0-9]) ) echo "not match" ;;+([-a-zA-Z0-9_]) ) echo "match";; esac
match
ghostdog74
A: 

If you want to validate that the string does not consist of only numbers:

string=$(echo "$line_from_file" | awk '{print $2}')
not_digits_only='[^[:digit:]]+'
allowed_chars='[[:alnum:]_]'
[[ $string =~ $not-digits-only && $string =~ $allowed-chars ]] && echo "Valid"

If you want to see if the string matches without regard to upper or lower case (case insensitive match):

string=$(echo "$line_from_file" | awk '{print tolower($2)}')
word_lc=$(echo $word | awk '{print tolower($0)}')
[[ $string == $word_lc ]] && echo "Match"

If you have Bash 4:

string=$(echo "$line_from_file" | awk '{print $2}')
[[ ${string,,} == ${word,,} ]] && echo "Match"

In Bash, you can get the second word from a line in a couple of ways without calling awk. One of them is:

string=($line_from_file)
string=${string[1]}
Dennis Williamson