AWK學習筆記一:入門

來源:互聯網
上載者:User
     AWK is a programming language that is designed for processing text-based data, either in files or data streams, and was created at Bell Labs in the 1970s.

 

Basic Concept of AWK:

A file consists of records, which by default are the lines of the file. One line becomes one record.
FS: The special variable FS (Field Separator) determines how awk will split up each record into fields.
$n: Integer variables can be used to refer to fields, each filed are separated by FS.
$0: $0 is representing the entire line.
NF: The special variable NF tells you how many fields are in this record.
NR: The special variable NR tells you which record this is. It is incremented each time a new record is read in.
 
AWK One Line Command Structure
$ awk <search pattern> {<program actions>}
AWK searches through the input file for each line that contains the search pattern. For each of these lines found, Awk then performs the specified actions. If no search pattern is specified. Awk will match all lines in the input file, and perform the actions on each one.
such as $ awk '{print NR,$0}' filename
If the AWK sentence is too long, we can put it in a file and run it with
$ awk -f program-file input-file1 input-file2 ...
AWK Script File Structure
 awk 'BEGIN              {<initializations>}
        <search pattern 1> {<program actions>}
        <search pattern 2> {<program actions>}
        ...
        END                {<final actions>}'
    The BEGIN clause performs any initializations required before Awk starts scanning the input file. The subsequent body of the Awk program consists of a series of search patterns, each with its own program action. AWK scans each line of the input file for each search pattern, and performs the appropriate actions for each string found. Once the file has been scanned, an END clause can be used to perform any final actions required.

聯繫我們

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