Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use linux awk built-in variables

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the use of linux awk built-in variables", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the use of linux awk built-in variables" bar!

I. built-in variable scale

Attribute description $0 current record (as a single variable) $1 separate n field of the current record, between fields separated by FS input FS input field separator default is the number of fields in the current record of the space NF, that is, the number of records that have been read by the NR, that is, the row number, the record separator entered by RS from 1 defaults to the newline character OFS output field separator default is also the record delimiter of the space ORS output The default is the newline character ARGC command line arguments ARGV command line argument array FILENAME the name of the current input file IGNORECASE if true Then ignore the case to match the ARGV flag CONVFMT digital conversion format of the currently processed file of ARGIND. 6gENVIRONUnix environment variable ERRNOUNIX system error message FIELDWIDTHS input field width blank delimited string FNR current record number OFMT digit output format% .6gRSTART string head RLENGTH matched by matching function string length SUBSEP\ 034

2. Examples

1. Common operations

The code is as follows:

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

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

/ ^ root/ is the selection expression, and $0 represents line by line.

2. Set the field separator (how to use FS)

The code is as follows:

[chengmo@localhost ~] $awk 'BEGIN {FS= ":"} / ^ root/ {print $1 heroine NF}' / etc/passwd

Root / bin/bash

FS is the field delimiter, which can be set by yourself. The default is a space. Because the passwd is separated by ":", you need to modify the default delimiter. NF is the total number of fields, $0 represents the current row record, and $1 Mustn is the current row, and each field corresponds to the value.

3. Number of records (usage of NR,FNR)

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 line of the current record

4. Set the output field separator (how to use OFS)

The code is as follows:

[chengmo@localhost ~] $awk 'BEGIN {FS= ":"; OFS= "^"} / ^ root/ {print FNR,$1,$NF}' / etc/passwd

1 ^ Root ^ / bin/bash

OFS sets the default field separator

5. Set the output line record separator (ORS usage)

The code is as follows:

[chengmo@localhost ~] $awk 'BEGIN {FS= ":"; ORS= "^"} {print FNR,$1,$NF}' / etc/passwd

1 root / bin/ Bash ^ 2 bin/ sbin/ no science ^ 3 daemon / sbin/ no science ^ 4 adm / sbin/ no science ^ 5 lp / sbin/nologin

From above, ORS defaults to a newline character, which is changed to "^ ^". All lines are separated by "^ ^".

6. Get the input parameters (ARGC, used by ARGV)

The code is as follows:

[chengmo@localhost ~] $awk 'BEGIN {FS= ":"; print "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, and ARGV gets the contents of input parameters, which is an array.

7. Get the passed file name (used by FILENAME)

The code is as follows:

[chengmo@localhost ~] $awk 'BEGIN {FS= ":"; print FILENAME} {print FILENAME}' / etc/passwd

/ etc/passwd

FILENAME,$0-$N,NF cannot be used in BEGIN, and BEGIN cannot get any variables that operate with file records.

8. Get the linux environment variable (used by ENVIRON)

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:/sbin:/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 subtypical array, and its value can be obtained by corresponding key values.

9. Output data format setting: (used by OFMT)

The code is as follows:

[chengmo@localhost ~] $awk 'BEGIN {OFMT= "% .3f"; print 2 print 3123.11111111;}' / etc/passwd

0.667 123.111

The default output format of OFMT is:% .6g keep six decimal places. Modifying OFMT here will change the default data output format.

10. Specify the delimiter by width (used by FIELDWIDTHS)

The code is as follows:

[chengmo@localhost ~] $echo 20100117054932 | awk 'BEGIN {FIELDWIDTHS= "4 2 2 2 3"} {print $1 "-" $2 "-" $3 recording 4 ":" $5 ":" $6}'

2010-01-17 05:49:32

FIELDWIDTHS is a series of digits separated by spaces to separate the records. FIELDWIDTHS= "4 2 2 2" means that the width of $1 is 4, 2, 2, 2, 3, 2. . The FS delimiter is ignored at this time.

11. RSTART RLENGTH usage

The code is as follows:

[chengmo@localhost ~] $awk 'BEGIN {start=match ("this is a test", / [Amurz] + $/); print start, RSTART, RLENGTH}'

11 11 4

[chengmo@localhost ~] $awk 'BEGIN {start=match ("this is a test", / ^ [Amurz] + $/); print start, RSTART, RLENGTH}'

0 0-1

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

Thank you for your reading, the above is the content of "the use of linux awk built-in variables", after the study of this article, I believe you have a deeper understanding of the use of linux awk built-in variables, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report