AWK study NOTE 4: awk Environment Variables

Source: Internet
Author: User
Variable Description
$ N The Nth field of the current record. The fields are separated by FS.
$0 Complete input records.
ARGC The number of command line parameters.
ARGIND Location of the current file in the command line (starting from 0 ).
ARGV Array containing command line parameters.
CONVFMT Number conversion format (default value: %. 6g)
ENVIRON Environment Variable join array.
ERRNO Description of the last system error.
FIELDWIDTHS Field width list (separated by Space key ).
FILENAME The current file name.
FNR Same as NR, but relative to the current file.
FS Field separator (any space by default ).
IGNORECASE If it is true, case-insensitive matching is performed.
NF The number of fields in the current record.
NR Number of current records.
OFMT The output format of the number (default value: %. 6g ).
OFS Delimiter of the output field (the default value is a space ).
ORS The output record delimiter (the default value is a line break ).
RLENGTH The length of the string matched by the match function.
RS Record separator (a line break by default ).
RSTART The first position of the string matched by the match function.
SUBSEP Array subscript separator (default value: \ 034 ).

Awk calls each row ending with a line break a record.

Record separator: the Default Input and Output delimiters are both carriage return and saved in the ORS and RS variables.

$0 variable: it refers to the entire record. For example, $ awk '{print $0}' test will output all records in the test file.

Variable NR: A counter. Every time a record is processed, the value of NR increases by 1. For example, $ awk '{print NR, $0}' test outputs all records in the test file and displays the record number before the record.

Each word in the record is called as a "field", which is separated by spaces or tabs by default. Awk can track the number of domains and save the value in the inner NF variable. For example, $ awk '{print $1, $3}' test prints the first and third columns (fields) separated by spaces in the test file ).

The built-in variable FS stores the value of the input domain separator. The default value is space or tab. You can use the-F command line option to modify the FS value. For example, $ awk-F: '{print $1, $5}' test prints the content of the first and fifth columns separated by colons.

Multiple Domain delimiters can be used at the same time. In this case, the separators should be written into square brackets, for example, $ awk-F' [: \ t] ''{print $1, $3} 'test, which is separated by space, colon, and tab.

The default delimiter of the output field is a space, which is saved in OFS. For example, $ awk-F: '{print $1, $5}' test. the comma between $1 and $5 is the value of OFS.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.