Basic usage of awk

Keywords: Linux ascii

How awk works:

Execute the statement in the BEGIN{action;...} statement block.
Read a line from a file or standard input (stdin), then execute the pattern{action;...} block, which scans the file line by line, repeating the process from the first line to the last line until all the files are read.
When read to the end of the input stream, execute the END{action;...} block.
The BEGIN statement block is executed before awk starts reading the input stream. This is an optional block of statements, such as variable initialization, table headers for printing out tables and so on, which can usually be written in the BEGIN block.
An END statement block is persisted after awk reads all from the input stream. If all analysis results are printed, such information aggregation is completed in the END statement block. It is also an optional statement block.
The pass command in the pattern statement block is the most important part and optional. If there is no attern statement block provided, the default is to hold {print}, that is, print every read and every awk read will hold the statement block.

Be careful:

Comma separator;
Each item output can be either a string or a value; the field, variable, or awk expression of the current record;
If item is omitted, it is equivalent to print $0.

Basic usage of awk:

- F: Specifies the field delimiter to be entered at the time of transmission;
- v var=value: Defines variables.

eg:
    awk -F: '{print $1}' /etc/passde
    Specify: Undelimited, print the first field of each row record and output $1

Variable usage of awk

Built-in variables:
FS: Output automatic separator. Default blank character
OFS: Output automatic separator, default blank character
RS: Output record delimiter, line break when specified input, original line break is still valid
ORS: Output record delimiter with specified symbols instead of newline characters
NF: Number of characters
NR: line number
FNR: Each file is counted separately and the record number is given.
FILENAME: Current file name;
ARGC: Number of command line parameters
ARGV: Array, saved parameters given by the command line

eg:

Input field separator FS: 
    ~]# awk -v FS=':' '{print $1,FS,$3}' /etc/passwd
    root : 0
    bin : 1
    daemon : 2
    adm : 3
    lp : 4
    sync : 5
    ...
 //Input and Output Field Separator OFS
    ~]# awk -v FS=':' -v OFS='----' '{print $1,$3,$7}' /etc/passwd
    root----0----/bin/bash
    bin----1----/sbin/nologin
    daemon----2----/sbin/nologin
    adm----3----/sbin/nologin
    lp----4----/sbin/nologin
    sync----5----/bin/sync
    shutdown----6----/sbin/shutdown
    ...
//Record Delimiter RS
    ~]# head -1 /etc/passwd | awk -v RS=':' '{print}'
    root
    x
    0
    0
    root
    /root
    ...
//Output record separator ORS
    ~]# awk -v OR=' ' -v ORS='----' '{print}' /etc/passwd
     root:x:0:0:root:/root:/bin/bash----bin:x:1:1:bin:/bin:/sbin/nologin----
    daemon:x:2:2:daemon:/sbin:/sbin/nologin----adm:x:3:4:adm:/var/adm:/sbin/nologin--
    --lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin----
    ...
//Number of fields NF
    ~]#  awk -F: '{print NF}' /etc/passwd
    7
    7
    7
    7
    7
    ...
//Number NR variable
    ~]#  awk -F: '{print NR,$1}' /etc/passwd
    1 root
    2 bin
    3 daemon
    4 adm
    5 lp
    6 sync
    7 shutdown
    ...
//Record No. FNR for each piece
     ~]#  awk '{print FNR,$1}' /etc/fstab /etc/passwd
     1 UUID=0c239cec-c2f7-49b6-9090-c1adf0f074cb
    2 /dev/mapper/vg_magedu-lv_root
    3 /dev/mapper/vg_magedu-lv_swap
    1 root:x:0:0:root:/root:/bin/bash
    2 bin:x:1:1:bin:/bin:/sbin/nologin
    3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
    4 adm:x:3:4:adm:/var/adm:/sbin/nologin
    ...
//Current item name FILENAME
     ~]#  awk '{print FILENAME,FNR,$1}' /etc/fstab /etc/passwd  
     /etc/fstab 1 UUID=0c239cec-c2f7-49b6-9090-c1adf0f074cb
    /etc/fstab 2 /dev/mapper/vg_magedu-lv_root
    /etc/fstab 3 /dev/mapper/vg_magedu-lv_swap
    /root/awktest.txt 1 root:x:0:0:root:/root:/bin/bash
    /root/awktest.txt 2 bin:x:1:1:bin:/bin:/sbin/nologin
    /root/awktest.txt 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
    ...
//Number of command parameters ARGC
     ~]#  awk 'BEGIN {print ARGC}' /etc/fstab /etc/inittab 
     3
//Array ARGV for each parameter given by the command
    ~]#  awk 'BEGIN {print ARGV[1]}' /etc/fstab /etc/inittab
    /etc/fstab

awk custom variable

eg:

Array ARGV variables for each parameter given by the command
    ~]#  awk -F: -v name='username:' '{print name,$1}' /etc/passwd
    username: root
    username: bin
    username: daemon
    username: adm
    ...

Formatted output of awk

Format character: one-to-one correspondence with item
    % c: ASCII codes showing elevation characters;
    % d,% i: distinct binary integers;
    % e,% E: scientific counting method numerical value;
    % f: Floating point number;
    % g,% G: show the value in the form of scientific counting or floating point;
    % s: display string;
    % u: Symbolic integers;
    %% Significant%.
Modifier:
    # [. #]: The first # is the width of the digital control display, and the second # is the precision after counting points, such as% 3.1f.
    - Left alignment (default right alignment), such as%-15s;
    + Signs of positive and negative values, e.g.%+d.

awk operator

Assignment operator:
    = The right is assigned to the left.
    += First add, then assign.
    -= First subtract, then assign value.
    *= First multiply, then assign value.
    /= First divide, then assign value.
    %= First take the surplus, then assign the value.
    ^= First power operation, then assignment;
    ++ Incremental operation;
    --: Decreasing operation.
_Operator:
    == Equivalent judgment;
    !=: Different judgments;
    > Judgment;
    >= Judgment is equal to or equal to;
    <: Judgment;
    <=: Judgment equals.
Pattern matcher:
    ~ Whether the left side matches the right side contains;
    ! ~: Does it not match? 
Logical operators:
    && Logic and;
    || Logic or;
    !: Logic.

Encourage each other!

Posted by dnast on Mon, 06 May 2019 13:20:39 -0700