Linux Shell 進階編程技巧1---深入討論(awk、<<)

來源:互聯網
上載者:User

標籤:

1.深入討論(awk、<<)
    1.1.深入討論awk
        記錄和域,模式和動作,Regex和元字元
            基礎教程中已經介紹
        條件和邏輯操作符
            <    小於
            >=    大於等於
            <=    小於等於
            ==    等於
            !=    不等於
            ~    匹配Regex
            !~    不匹配Regex
            
            &&    and
            ||    or
            !    not
            
            例子(注釋:www.log是apache的一個記錄檔,這是在日常生活中很常遇見的)

#!/bin/bashecho "210-219網段的訪問量是:`awk ‘{if ($1~/^21[0-9]/) print $0}‘ www.log | wc -l`"echo "非210-219網段的訪問量是:`awk ‘{if ($1!~/^21[0-9]/) print $0}‘ www.log | wc -l`"echo "2004年07月07日的訪問量是:`awk ‘{if ($4~/^\[07\/Jul\/2004/) print $0}‘ www.log | wc -l`"echo "2004年07月07日/htm/free_call.php的訪問量是:`awk ‘{if ($4~/^\[07\/Jul\/2004/) print $0}‘ www.log | awk ‘{if ($7=="/htm/free_call.php") print $0}‘ | wc -l`"

        awk內建變數
            內建變數
                ARGC        命令列參數個數
                ARGV        命令列參數排列,是一個數組,用ARGV[0]、ARGV[1]……的方式
                ENVIRON        支援隊列中系統內容變數的使用
                FILENAME    awk瀏覽的檔案名稱
                FNR            瀏覽檔案的記錄數
                FS            設定輸入欄位分隔字元,等價於命令列參數-F選項
                NF            瀏覽記錄的域個數
                NR            已讀的記錄數
                OFS            輸出域分隔字元
                ORS            輸出記錄分隔字元
                RS            控制記錄分隔字元
            例子:
                awk -F ‘#‘ ‘{print NF,NR,$0}‘ grade.txt
                awk -F ‘#‘ ‘{print NF,NR,ENVIRON["USER"],$0}‘ grade.txt
                awk -F ‘#‘ ‘{print NF,NR,ENVIRON["USER"],$0,FILENAME}‘ grade.txt
                awk -F ‘#‘ ‘{print NF,NR,ENVIRON["USER"],$0,FILENAME,ARGC}‘ grade.txt
                awk -F ‘#‘ ‘{print NF,NR,ENVIRON["USER"],$0,FILENAME,ARGC,ARGV[0]}‘ grade.txt
                補充:grade.txt檔案內容
                    85#senior
                    87#junior
                    75#senior
                    79#senior
                    92#senior
                    23#junior
        內建的字串函數
            內建字串函數
                gsub(r,s)        在整個$0中用s替代r
                gsub(r,s,t)        在整個t中用s替代r
                index(s,t)        返回s中字串t的第一個位置
                length(s)        返回s的長度
                match(s,r)        測試s是否包含匹配r的字串
                split(s,a,fs)    在fs上將s分成序列a
                sprint(fmt,exp)    返回經fmt格式化後的exp
                sub(r,s)        用$0中最左邊最長的子串代替s
                substr(s,p)        返回字串s中從p開始的後部分
                substr(s,p,n)    返回字串s中從p開始長度為n的後部分
            例子
                awk -F ‘#‘ ‘{if (gsub("#","||")) print $0}‘ grade.txt
                awk -F ‘#‘ ‘{if (gsub("s","S",$1)) print $0}‘ grade.txt
                awk -F ‘#‘ ‘{print (index($2,"e"))}‘ grade.txt
        awk逸出字元
            逸出字元
                \b        退格鍵
                \t        tab鍵
                \f        走紙換頁
                \ddd    八進位值
                \n        新行
                \c        任意其他特殊字元,例如\\為反斜線符號
                \r        斷行符號鍵
            例子
                awk -F ‘#‘ ‘{print (index($2,"s")),"\t",$2}‘ grade.txt
                awk -F ‘#‘ ‘{print (index($2,"s")),"\n",$2}‘ grade.txt
        printf修飾符,類似於C語言的printf函數
            printf修飾符
                %c    ASCII字元
                %d    整數
                %f    浮點數,例如(123.44)
                %e    浮點數,科學計數法
                %f    新行
                %g    awk決定使用哪種浮點數轉換e或者f
                %o    八位元
                %s    字串
                %x    十六進位數
            例子
                awk -F ‘#‘ ‘{printf "%c\n",$1}‘ grade.txt
                awk -F ‘#‘ ‘{printf "%c\t%d\n",$1,$1}‘ grade.txt
                awk -F ‘#‘ ‘{printf "%c\t%f\t%d\n",$1,$1,$1}‘ grade.txt
        awk數組
            簡介
                awk數組下標是從 1 而不是 0 開始
            例子
                awk ‘BEGIN {print split("as#qw#1234",array2,"#")}‘
                awk ‘BEGIN {print split("as#qw#1234",array2,"#"); print array2[1]}‘
                awk ‘BEGIN {print split("as#qw#1234",array2,"#"); print array2[1],"\t",array2[2],"\t",array2[3]}‘
            awk的指令碼例子(awk_array.sh 檔案)
                執行命令./awk_array.sh grade.txt

#!/bin/awk -fBEGIN{    FS="#"    score["0-60"]=0    score["60-70"]=0    score["70-80"]=0    score["80-90"]=0    score["90-100"]=0    student["junior"]=0    student["senior"]=0}{    {        if [ $1<60 ]        score["0-60"]++    }    {        if [ $1<70 ] && [ $1>=60]        score["60-70"]++    }    {        if [ $1<80 ] && [ $1>=70 ]        score["70-80"]++    }    {        if [ $1<90 ] && [ $1>=80]        score["80-90"]++    }    {        if [ $1<=100 ] && [ $1>=90 ]        score["90-100"]++    }}{    for senior_junior in student    {        if [ $2==senior_junior ]        student[senior_junior]++    }}END{        for number in score            print "The score",number,"has",score[number],"students"    }    {        for senior_junior in student            print "The class has",student[senior_junior],senior_junior,"stuents"    }}

    1.2.深入討論<<

Linux Shell 進階編程技巧1---深入討論(awk、<<)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.