Introduction to Linux awk built-in variable usage

Source: Internet
Author: User
Tags command line regular expression

One, built-in variable table

Property Description
$ Current record (as a single variable)
$1~ $n Nth field in the current record, separated by FS
Fs The input field delimiter is by default a space
Nf The number of fields in the current record, that is, how many columns
Nr The number of records that have been read is the line number, starting at 1
Rs Enter the record he Fummer think line breaks
OFS Output field delimiter default is also a space
ORS The record separator for the output, which defaults to line breaks
ARGC Number of command line arguments
Argv Array of command line arguments
FILENAME The name of the current input file
IGNORECASE If true, a match that ignores the case
Argind argv identifier of the file currently being processed
Convfmt Digital conversion Format%.6g
ENVIRON UNIX Environment variables
Errno UNIX System error messages
FieldWidths Enter a blank delimited string of field widths
FNR Current number of records
Ofmt Output format for numbers%.6g
Rstart String first matched by matching function
Rlength Length of string matched by matching function
Subsep 34

2. Example

1. Common operation

The code is as follows:

[Chengmo@localhost ~]$ awk '/^root/{print $} '/etc/passwd

Root:x:0:0:root:/root:/bin/bash

/^root/is the choice expression, and the $ is a line-by-row

2, set the field separator symbol (fs use method)

The code is as follows:

[Chengmo@localhost ~]$ awk ' begin{fs= ': "}/^root/{print $, $NF} '/etc/passwd

Root/bin/bash

FS is a field separator, you can set it yourself, the default is a space, because the passwd inside is ":" Delimited, so you need to modify the default separator. NF is the total number of fields, which represents the current row record, $1-$n is the current row, and each field corresponds to a value.

3, record number (Nr,fnr use method)

The code is as follows:

[Chengmo@localhost ~]$ awk ' begin{fs= ': '}{print nr,$1, $NF} '/etc/passwd

1 Root/bin/bash

2 Bin/sbin/nologin

3 Daemon/sbin/nologin

4 Adm/sbin/nologin

5 Lp/sbin/nologin

6 Sync/bin/sync

7 Shutdown/sbin/shutdown

......

NR gets the current record in the row

4, set the output field separator (Ofs use method)

The code is as follows:

[Chengmo@localhost ~]$ awk ' begin{fs= ":" ofs= "^^"}/^root/{print fnr,$1, $NF} '/etc/passwd

1^^root^^/bin/bash

OFS Set Default Field separator

5, set the output line record separator (Ors use method)

The code is as follows:

[Chengmo@localhost ~]$ awk ' begin{fs= ":" ors= "^^"}{print fnr,$1, $NF} '/etc/passwd

1 root/bin/bash^^2 bin/sbin/nologin^^3 daemon/sbin/nologin^^4 adm/sbin/nologin^^5 lp/sbin/nologin

From the above, ORS is the default newline character, which is modified to read: "^^", separated by "^^" between all lines.

6, the input parameter obtains (ARGC, argv uses)

The code is as follows:

[Chengmo@localhost ~]$ awk ' begin{fs= ': ';p rint ' argc= ' argc;for (k in ARGV) {print k ' = ' argv[k];} '/etc/passwd

argc=2

0=awk

1=/etc/passwd

ARGC gets the number of all input parameters, argv gets the input parameter content, is an array.

7. Get incoming file name (filename used)

The code is as follows:

[Chengmo@localhost ~]$ awk ' begin{fs= ': ';p rint filename}{print FILENAME} '/etc/passwd

/etc/passwd

filename,$0-$N, NF cannot be used in begin, and no variables with file record operations can be obtained in begin.

8, obtain the Linux environment variable (environ use)

The code is as follows:

[Chengmo@localhost ~]$ awk ' begin{print environ[' PATH '];} '/etc/passwd

/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/lib/ccache:/usr/lib/icecc/bin:/usr/local/bin:/bin:/usr/bin:/usr/ Local/sbin:/usr/sbin:/sbi

N:/usr/java/jdk1.5.0_17/bin:/usr/java/jdk1.5.0_17/jre/bin:/usr/local/mysql/bin:/home/web97/bin

Environ is a child-typical array that can get its value by its corresponding key value.

9, the output data format set: (Ofmt use)

The code is as follows:

[Chengmo@localhost ~]$ awk ' begin{ofmt= '%.3f ';p rint 2/3,123.11111111;} '/etc/passwd

0.667 123.111

The OFMT default output format is:%.6g retains six decimal places, where modification ofmt modifies the default data output format.

10, according to the width of the specified separator (fieldwidths use)

Copy Code

The code is as follows:

[Chengmo@localhost ~]$ echo 20100117054932 | awk ' begin{fieldwidths= ' 4 2 2 2 2 3 "}{print $"-"$"-"$3,$4": "$": "$}"

2010-01-17 05:49:32

FieldWidths its format as a space-delimited string of digits to delimit records, fieldwidths= "4 2 2 2 2 2" means that the width is 4,$2 2,$3 is 2 ... This time is ignored: FS separator.

11, Rstart rlength use

The code is as follows:

[Chengmo@localhost ~]$ awk ' Begin{start=match ("This is a test",/[a-z]+$/); print start, Rstart, rlength} '

11 11 4

[Chengmo@localhost ~]$ awk ' Begin{start=match ("This is a test",/^[a-z]+$/); print start, Rstart, rlength} '

0 0–1

Rstart is matched to the first position of the regular expression, rlength matches the length of the character and is not found to be-1.

The above is: awk some of the built-in variables to use, I hope there is any problem to communicate with me.

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.