2012-05-16 94 views
0

我想驗證output.txt中的數據類型。驗證數據類型

例如:

52 40.5 60 yes 
30.3 20 40 no 

結果:

52 is Integer 
40.5 is Decimal 
60 is Integer 
Yes is Character 

應該用什麼更好的選擇適合這個任務 - bashawk

謝謝。使用bash

回答

1

,你可以嘗試這樣的:

#!/usr/bin/env bash 

while read -r; do 
    for token in $REPLY; do     # no quotes here! 
     if [[ "$token" =~ ^[[:digit:]]+$ ]] 
     then printf -- "%d is digit\n" "$token" 
     elif [[ "$token" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]] 
     then printf -- "%f is float\n" "$token" 
     elif [[ "$token" =~ ^[[:alpha:]]+$ ]] 
     then printf -- "%s is string\n" "$token" 
     else printf -- "unknown class: %s\n" "$token" 
     fi 
    done 
done < file.txt 
  • 讀一本線(while readfile.txt,行是VAR REPLY
  • 劈令牌(for /每個令牌線REPLY
  • 發現當前令牌的類型(與正則表達式和類匹配完成^..$令牌)
2
awk ' 
BEGIN { 
    types["Integer"] = "^[[:digit:]]+$"; 
    types["Decimal"] = "^[[:digit:]]+[.][[:digit:]]+$"; 
    types["Character"] = "^[[:alpha:]]+$" 
} 
{ 
    for (i = 1; i <= NF; i++) { 
     found = 0; 
     for (type in types) { 
      if ($i ~ types[type]) { 
       print $i, "is", type; 
       found = 1 
      } 
     } 
     if (! found) { 
      print "Type not found for:", $i 
     } 
    } 
    printf "\n" 
}' inputfile 
2

使用bash patterns

shopt -s extglob 
while read line; do 
    set -- $line 
    for word; do 
    case $word in 
     ?([-+])+([[:digit:]])) echo "$word is an integer" ;; 
     ?([-+])@(*([[:digit:]]).+([[:digit:]])|+([[:digit:]]).*([[:digit:]]))) echo "$word is a decimal" ;; 
     +([[:alpha:]])) echo "$word is alphabetical" ;; 
     *) echo "$word is a mixed string" ;; 
    esac 
    done 
done < output.txt 
+1

+1用於識別數字有一個前導符號,不同於其他人。 – Kaz

1

TXR:正則表達式的一點點,和類型系統的一點點。如果令牌看起來像一個數字,那麼讓我們嘗試將它從字符串轉換爲一個數字對象,其中num-str。如果失敗了,它必須是一個範圍錯誤。 typeof函數爲我們提供了對象的類型:fixnum,bignumfloat

@(freeform) 
@(coll)@{token /[^\s]+/}@(end) 
@(output) 
@ (repeat) 
@token @(if (eql (match-regex token #/[+\-]?\d+([.]\d+)?([Ee][+\-]?\d+)?/) 
       (length token)) 
      (let ((x (num-str token))) 
      (if x (typeof x) "out-of-range")) 
      "non-numeric") 
@ (end) 
@(end) 

運行:

$ txr verify.txr - 
hello world  
1.5E900 1.43 52 5A 12341234123412341234 12341234123412341234243.42 42 
[Ctrl-D] 
hello non-numeric 
world non-numeric 
1.5E900 out-of-range 
1.43 float 
52 fixnum 
5A non-numeric 
12341234123412341234 bignum 
12341234123412341234243.42 float 
42 fixnum