2018.4.27 The third lesson in six weeks (awk tool)

awk tool introduction

awk也是流行的编辑器,针对文档中的行来操作,一行一行的操作;
awk具备sed的所有功能,而且更强大。

awk intercepts a paragraph in the document, the example is as follows:

  • The function of the -F option is to specify the delimiter. If the -F option is not added, a space or tab is used as the delimiter.
  • print is the action of printing, which is used to print a field. $1 is the first paragraph, $2 is the second paragraph, and $0 is the entire line.
    • Example 1:

      [root@localhost ~]# mkdir awk #Create an awk directory
      [root@localhost ~]# cp /etc/passwd awk/test.txt #Copy a file and change the name
      [root@localhost ~]# cd awk/
      [ root@localhost awk]# ls
      test.txt
      [root@localhost awk]# awk -F ':' '{print $1}' test.txt #Print out the first paragraph, the separator is colon ":"
      root
      bin
      daemon
      adm
      lp
      sync
      shutdown
      halt
      mail

    • Example 2, print out everything: $0

      [root@localhost awk]# awk '{print $0}' test.txt #$0表示整行,所有。
      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

match character or string

  • Print out the line containing oo.

    [root@localhost awk]# awk '/oo/' test.txt
    root:x:0:0:root:/root:/bin/bash
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin

  • Print the line where the first paragraph of the first line contains oo.

    [root@localhost awk]# awk -F ':' '$1 ~ /oo/' test.txt
    root:x:0:0:root:/root:/bin/bash

  • Print multiple times and match test multiple times. The example is as follows:

    [root@localhost awk]# awk -F ':' '/root/ {print $1,$3} /bash/ {print $1,$3,$7}' test.txt
    root 0
    root 0 /bin/bash
    operator 11
    #this The command means that the first and third paragraphs contain the characters of root, and the first paragraph, the third paragraph, and the seventh paragraph contain the characters of bash.

Conditional operator

awk中可以用逻辑符号进行判断,比如==就是等于,也可以理解为精确匹配;
另外还有>、>=、<等,在和数字比较时,
若把比较的数字用双引号因起来,那么awk不会认为是数字,
而会认为是字符,不加双引号则会认为是数字。
  • Example 1, exact match:

    [root@localhost awk]# awk -F ':' '$3=="0"' test.txt #数字0
    root:x:0:0:root:/root:/bin/bash

  • Example 2, print out the lines where the third paragraph is greater than or equal to 500

    [root@localhost awk]# awk -F ':' '$3>=500' test.txt
    polkitd:x:999:997:User for polkitd:/:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin

  • Example 3, ! = indicates a mismatch. The seventh paragraph is not equal to /sbin/nologin, as follows:

    [root@localhost awk]# awk -F ':' '$7!="/sbin/nologin"' test.txt
    root:x:0:0:root:/root:/bin/bash
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt

  • You can use && and ||, which represent and and or, respectively, as follows:

    [root@localhost awk]# awk -F ':' '$3<$4' test.txt
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

  • || or the usage is as follows:

    [root@localhost awk]# awk -F ':' '$3>100 || $7=="/sbin/nologin"' test.txt
    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
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin

awk's built-in variables

awk常用的变量有OFS,NF和NR,OFS和-F选项有类似的功能;
也是用来定义分隔符的,但是它实在输出的时候定义;
NF表示用分割符分割后一共有多少段;
NR表示行号。

OFS is used as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324871372&siteId=291194637