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.
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.
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.
作者:Shane
出处:http://bluescorpio.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://bluescorpio.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。