2010-08-26 46 views
1

我需要驗證如果從$ LINE_FROM_FILE的第二個字是用大寫或小寫字符匹配詞用在shell腳本字母

(WORD - 字符串可與數字)的字符串

如何搭配WORD串? (我用這個語法的ksh腳本)

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

WORD - 可以用數字小或資本字符(但不是唯一的號碼)

例如

WORD = textBIG

WORD = HOME_DIR

WORD = COMPUTER1

WORD = HOST_machine

利迪婭

回答

0
$ 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 
0

您可以用awk功能toupper(或tolower)進行檢查。

WORD == toupper(WORD)指字是用大寫的

0

如果您想驗證該字符串不僅由數字組成:

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

如果你想看看字符串,不考慮相匹配到大寫或小寫(不區分大小寫的匹配):

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

如果你有擊4:

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

在bash中,你可以從幾個方面行的第二個字,而無需調用awk。其中之一是:

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