awk指令操作詳解

來源:互聯網
上載者:User

標籤:awk指令操作詳解

  1. 記錄與欄位

    awk一次從檔案中讀取一條記錄,並將記錄儲存在欄位變數$0中。記錄被分隔為欄位比功能儲存在$1,$2,...$NF中,(預設使用空格或指標符為分隔字元)。內建參數NF為記錄的欄位數。

    #輸出demo.txt 檔案的第1個,第2個,第3個,第4個欄位

    awk ‘{print $1,$2,$3,$4}‘ demo.txt

    #輸出demo.txt 檔案整行記錄

    awk ‘{print $0}‘ demo.txt

    #輸出demo.txt 該行的欄位個數。

    awk ‘{print NF}‘ demo.txt

    #輸出demo.txt 該行的欄位個數。

    awk ‘{print $NF}‘ demo.txt

  2. 欄位分隔符號

    awk預設讀取資料以空格或指標符為分隔字元,但可以通過-F或FS(field separator)變數來改變更個符。

    awk -F: ‘{print $1}‘ /etc/passwd
    awk ‘BEGIN {FS = ":"} {print $1}‘ /etc/passwd

    echo ‘hello the:world,!‘ |awk ‘BEGIN{FS="[:, ]"} {print $1,$2,$3,$4}‘

  3. 內建變數                            描述

    ARGC                                命令列參數個數

    FILENAME                         當前輸入檔案的名稱

    FNR                                   當前輸入文檔的目前記錄編號,尤其是當有多個輸入文檔時有用

    NR                                     輸入資料流的當前輸入記錄編號

    NF                                     目前記錄的欄位個數

    FS                                     欄位分隔符號

    OFS                                   輸出欄位分隔符號,預設為空白格

    ORS                                   輸出記錄分隔字元,預設我為分行符號\n

    RS                                     輸入記錄分隔字元,預設我為分行符號\n

    cat test1.txt

    This is a test file.
    Welcome to Jacob‘s Class.

    cat test2.txt

    Hello the world.
    Wow, I‘m overwhelmed.
    Ask for more.

    #輸出當前文檔的當前行號

    awk ‘{print FNR}‘ test1.txt test2.txt   

    #將兩個文檔當做一個整體的輸入資料流,通過NR輸入當前行號

    awk ‘{print NR}‘ test1.txt test2.txt 

    #輸出每一行當前的欄位數

    awk ‘{print NF}‘ test1.txt   

    #內建變數FS欄位分隔符號

    awk ‘BEGIN{FS="[:]"} {print $1}‘ /etc/passwd

    #輸出欄位伏案分隔字元

    awk ‘BEGIN{OFS="-"}{print $1,$2,$3}‘ test1.txt

    cat test.txt

    name=lianjie
    age=26
    Tel=18219995524

    name=ccc
    age=20
    Tel=13899246656

    name=alex
    age=25
    Tel=19812345623

    awk  ‘BEGIN{FS="\n";RS=""}{print $1,$2,$3,$4}‘ test.txt
    name=lianjie age=26 Tel=18219995524
    name=ccc age=20 Tel=13899246656
    name=alex age=25 Tel=19812345623

    awk ‘BEGIN{FS="\n";RS=""}{print $1}‘ test.txt

  4. 運算式與操作符

    操作符

    +   加

    -    減

    *    乘

    /     除

    %   求餘

    ^    冪運算

    ++ 自增1

    --    自減1

    +=    (x+=9等價於x=x+9)

    -=     (x-=9等價於x=x-9)

    *=     (x*=9等價於x=x*9)

    /=     (x/=9等價於x=x/9)

    >    大於

    <    小於

    >=  大於等於

    <=   小於等於

    !=    不等於

    ~    匹配

    !~    不配

    &&    與

    ||     或

    echo ‘test‘ | awk ‘x=2 {print x+3}‘

    echo ‘test‘ | awk ‘x=2,y=3 {print x*3,y*3}‘

    awk ‘/^$/ {print x+=1}‘ test.txt                     #統計所有的空白行

    awk ‘/^$/ {x+=1}END{print x}‘ test.txt         #列印總空白行的行個數

    awk -F: ‘$1~/root/ {print $7}‘ /etc/passwd

    awk -F: ‘$3>500 {print $1}‘ /etc/passwd

本文出自 “心靜梵音” 部落格,請務必保留此出處http://mastters.blog.51cto.com/6516495/1532781

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.