转:談談gawk 裡的 igawk

http://bbs.chinaunix.net/thread-1450345-1-1.html

我們寫腳本,很多情況下是一次性的,所以傾向寫一些常用的函數
掉用,例如我的$HOME/lib/awk 就寫了一些函數, 然而怎樣掉用這些函數,
不成每用一次就複製到腳本上,我在命令列又如何掉用呢?

鑑於這種需求,Gnu awk  為使用者提供了一個變量,和一個可以包含你寫的函數的命令,
他們就是 AWKPATH 和 igawk

AWKPATH 這個變量內定的路徑為 , 手冊上答案是.:/usr/local/share/awk
http://www.gnu.org/manual/gawk/html_node/AWKPATH-Variable.html
但我的cygwin是 .:/usr/share/awk
根據手冊所指,利用ENVIRON["AWKPATH"]這變量, 便可得到路徑, 那就

gawk 'BEGIN{print  ENVIRON["AWKPATH"]}'

就可顯示.而該默認路徑已有很多函數供掉用設定很簡易,在
$HOME/.bash_profile 加入

export AWKPATH=$AWKPATH:/path/where/you/store/function
我的就是 AWKPATH=$AWKPATH:$HOME/lib/awk

source ~/.bash_profile 後就完成

User@User-PC ~
$ ls lib/awk
cap.awk  commas.awk  hex2dec.awk  passgen.awk  reverse.awk  swapcase.awk  whois.awk

我有七個函數在這裡,在命令列上就可如此掉用

User@User-PC ~
$ echo -1234567 | gawk -f commas.awk --source '{print commas($0)}'
-1,234,567

就是這樣 :)

但不在命列上使用,放到腳本又如何呢? 不要在腳本頂部這樣寫,

#! /usr/bin/gawk -f commas.awk --source

那不成的, gawk 維護人寫了一個命令,也是一個shell 腳本, 名
igawk, 當你要掉用函數的時候,用igawk就對了, 格式這樣

#! /usr/bin/igawk -f
@include commas.awk
@include hex2dec.awk
                .
                .
@include N 個函數檔案名

下面就正常寫你的腳本就可.. :), 例如

#! /usr/bin/igawk -f
@include commas.awk
@include swapcase.awk
  
  {
     if ($0 ~ /^-?[0-9]+$/){
           print commas($0) 
      } else if ($0 ~ /[a-zA-Z]+/){
           print swapcase($0)
      }   
  }   

User@User-PC ~
$ echo 12345 | ./igawk_test 
12,345

User@User-PC ~
$ echo rOOT | ./igawk_test 
Root

就是這樣,寫得不正確,請版友代為修正,最後難為情地
顯上這些醜醜的函數

#把數字格式化,如 1234 改成 1,234
function commas(x,    y, z, j, len, flag, out){

             if (x ~ /^-/) {
                   x=substr(x, 2)
                   j="-"
                 }
             if (match(x, /\./)) {
                   flag="float"
                   y=substr(x, RSTART)
                   z=substr(x, 1, RSTART - 1)
                   x=z
                  }
                  else {
                      flag="int"
                  }
              len=length(x)

              while ( len > 3 ){
                   len -= 3
                   x=substr(x, 1, len)","substr(x, len + 1)
                  }

              if ( flag == "int" ){
                      out=x
                  }
                  else {
                      out=x y
                  }
              if ( j == "-" ) out=j out
          return out
   }

# end of function

#大小字母對掉,大變小,小變大
function swapcase(input,   output, step, wlen, char){
           output=""
           step=1
           wlen=length(input)
             while ( step <= wlen ){
               char=substr(input, step, 1)
               if ( char ~ /[A-Z]/ ){
                  char=tolower(char)
                  } else if ( char ~ /[a-z]/ ){
                  char=toupper(char)
                  } else {
                  char=char
                  }
                output=output char
                step++
               }
            return output
          }
# end of function

# translate hex number to dec number 
# 這個不是我寫的
function hex2dec(h      ,i,x,v){
       h=tolower(h);sub(/^0x/,"",h)
       for(i=1;i<=length(h);++i){
            x=index("0123456789abcdef",substr(h,i,1))
            if(!x)return "NaN"
              v=(16*v)+x-1
            }
       return v
}

# end of function

#reverse the word
function reverse(x,   output, wlen, char){
          output=""
          wlen=length(x)
          while ( wlen > 0 ) {
            char=substr(x, wlen, 1)
            output=output char
            wlen -= 1
           }

       return output
   }
# end of function

你可能感兴趣的:(gawk)