AWK学习笔记一:入门

     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.
posted @ 2010-01-08 21:09  小楼  阅读(479)  评论(0编辑  收藏  举报