技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

转:談談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

posted on   codestyle  阅读(381)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示