Introduction to linux awk built-in Variables

Source: Internet
Author: User

Introduction to linux awk built-in variables awk is an excellent text processing tool and can be said to be a programming language. The following are the built-in variables of awk. 1. built-in variable table attribute description $0 current record (as a single variable) $1 ~ $ N indicates the nth field of the current record. The number of fields in the current record of Space NF is the default delimiter used to separate the FS input fields. This is the number of columns of records that NR has read, it is the row number. The record input by RS starts from 1. Its Delimiter is considered to be the record separator output by space ORS by default, the default value is the line break ARGC command line parameter count ARGV command line parameter array FILENAME the name of the current input file IGNORECASE if true, the ARGV flag of the currently processed ARGIND file will be replaced with the case-insensitive match. CONVFMT digital conversion format %. 6 gENVIRON UNIX environment variable errno unix system error message FIELDWIDTHS input field width blank separator string FNR current number of records OFMT number output format %. 6. The first rlength of the string matched by the gRSTART function is the length of the string matched by the function. SUBSEP \ 034 2. instance 1. Common Operations [chengmo @ Localhost ~] $ Awk '/^ root/{print $0}'/etc/passwd root: x: 0: 0: root:/root: /bin/bash/^ root/is the selection expression. $0 indicates line-by-line 2. Set the field separator number (FS usage) [chengmo @ localhost ~] $ Awk 'in in {FS = ":"}/^ root/{print $1, $ NF} '/etc/passwdroot/bin/bash FS is the field separator, you can set it by yourself. The default value is space. Because passwd is separated by ":", You need to modify the default delimiter. NF indicates the total number of fields, $0 indicates the current row record, $1-$ n indicates the current row, and each field corresponds to the value. 3. Number of records (NR, FNR usage) [chengmo @ localhost ~] $ Awk 'in in {FS = ":"} {print NR, $1, $ NF} '/etc/passwd1 root/bin/bash2 bin/sbin/nologin3 daemon/sbin/nologin4 adm/sbin/nologin5 lp/sbin/nologin6 sync/bin/syncshut7 down/ sbin/shutdown ...... NR returns the row where the current record is located. 4. Set the output field separator (OFS usage) [chengmo @ localhost ~] $ Awk 'in in {FS = ":"; OFS = "^"}/^ root/{print FNR, $1, $ NF} '/etc/passwd1 ^ root ^/bin/bash OFS set the default field separator 5. Set the output line record separator (ORS usage) [chengmo @ localhost ~] $ Awk 'in in {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 above, ORS is a line break by default. Here it is changed to "^", and all rows are separated by "^. 6. Obtain input parameters (used by ARGC and ARGV) [chengmo @ localhost ~] $ Awk 'in in {FS = ":"; print "ARGC =" ARGC; for (k in ARGV) {print k "=" ARGV [k];} '/etc/passwdARGC = 20 = awk1 =/etc/passwd ARGC get the number of all input parameters, ARGV get the input parameter content, is an array. 7. Obtain the input file name (used for FILENAME) [chengmo @ localhost ~] $ Awk 'in in {FS = ":"; print FILENAME} {print FILENAME} '/etc/passwd FILENAME, $0-$ N, NF cannot be used in BEGIN, and BEGIN cannot obtain any variables that operate on file records. 8. Obtain the linux environment variable (used by ENVIRON) [chengmo @ localhost ~] $ Awk 'in in {print ENVIRON ["PATH"];} '/etc/passwd/usr/lib/qt-3.3/bin:/usr/kerberos/bin: /usr/lib/ccache:/usr/lib/icecc/bin:/usr/local/sbin: /usr/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 typical sub-array and its value can be obtained through the corresponding key value. 9. Output data format settings: (for OFMT) [chengmo @ localhost ~] $ Awk 'in in {OFMT = "%. 3f "; print 2/3, 123.11111111;} '/etc/passwd 0.667 123.111 OFMT default output format: %. 6g retain six decimal places. Modifying OFMT here will modify the default data output format. 10. Specify the delimiter by width (used by FIELDWIDTHS) [chengmo @ localhost ~] $ Echo 20100117054932 | awk 'in in {FIELDWIDTHS = "4 2 2 2 3"} {print $1 "-" $2 "-" $3, $4 ": "$5": "$6} 'FIELDWIDTHS is a string of numbers separated by spaces to separate records by fields, FIELDWIDTHS = "4 2 2 2 2" indicates that $1 is 4 in width, $2 is 2, and $3 is 2 ..... This will ignore the: FS separator. 11. Use [chengmo @ localhost ~] For RSTART RLENGTH $ Awk 'in in {start = match ("this is a test",/[a-z] + $/); print start, RSTART, RLENGTH} '11 11 4 [chengmo @ localhost ~] $ Awk 'in in {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 character length and cannot be found as-1.

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.