• 第十四章 Linux三剑客之老大—awk



    一、awk     # 擅长取列 计算 数组 函数 编程语言

    内部命令 内部变量


    NR     #行号

    $0      #完整的一行内容

    $n      # n 是数字 表示取出第几列 多列用逗号分割

    -F       #FS 分隔符的变量

    NF     #最后一列 为第几列

    $NF     #显示最后一列的内容

    二、查 过滤


    [root@jindada ~]# awk '/root/' passwd
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin

    [root@jindada ~]# awk '/root/;/adm/' passwd
    root:x:0:0:root:/root:/bin/bash
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin


    [root@jindada ~]# awk '/root|adm/' passwd
    root:x:0:0:root:/root:/bin/bash
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin


    [root@jindada ~]# awk '/adm/,/mail/' passwd
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin


    [root@jindada ~]# awk 'NR==1' passwd
    root:x:0:0:root:/root:/bin/bash
    [root@jindada ~]# awk 'NR==1,NR==3' 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
    [root@jindada ~]# awk 'NR==1;NR==3' passwd
    root:x:0:0:root:/root:/bin/bash
    daemon:x:2:2:daemon:/sbin:/sbin/nologin


    [root@jindada ~]# awk 'NR>10' passwd
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    [root@jindada ~]# awk 'NR<10' 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
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin


    [root@jindada ~]# awk 'NR<=2' passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    [root@jindada ~]# awk 'NR>=15' passwd
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin


    && #并且


    [root@jindada ~]# awk 'NR>10 && NR<16' passwd
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin

    || #或者

    [root@jindada ~]# awk 'NR<3 || NR>15' passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin


    #打印整个文件内容

    [root@jindada ~]# awk '{print $0}' 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
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin

    #给文件内容加上行号

    [root@jindada ~]# awk '{print NR,$0}' passwd
    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
    5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    6 sync:x:5:0:sync:/sbin:/bin/sync
    7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    8 halt:x:7:0:halt:/sbin:/sbin/halt
    9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10 operator:x:11:0:operator:/root:/sbin/nologin
    11 games:x:12:100:games:/usr/games:/sbin/nologin
    12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13 nobody:x:99:99:Nobody:/:/sbin/nologin
    14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15 dbus:x:81:81:System message bus:/:/sbin/nologin
    16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin


    三、#取反 排除

    ! 取反 排除

    [root@jindada ~]# awk '!/sbin/' passwd
    root:x:0:0:root:/root:/bin/bash

    [root@jindada ~]# awk 'NR!=1' passwd
    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
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin

    #排除了第5行到第15行

    [root@jindada ~]# awk 'NR<5 || NR >15' 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
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin


    # 取列


    [root@jindada ~]# awk -F: '{print $1}' passwd
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    ftp
    nobody
    systemd-network
    dbus
    polkitd
    sshd
    postfix
    [root@jindada ~]# awk -F: '{print $1,$2}' passwd
    root x
    bin x
    daemon x
    adm x
    lp x
    sync x
    shutdown x
    halt x
    mail x
    operator x
    games x
    ftp x
    nobody x
    systemd-network x
    dbus x
    polkitd x
    sshd x
    postfix x


    [root@jindada ~]# awk -F: '{print $NF}' passwd
    /bin/bash
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /bin/sync
    /sbin/shutdown
    /sbin/halt
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin

    $(NF-1) 倒数第二列

    [root@jindada ~]# awk -F: '{print $(NF-1)}' passwd
    /root
    /bin
    /sbin
    /var/adm
    /var/spool/lpd
    /sbin
    /sbin
    /sbin
    /var/spool/mail
    /root
    /usr/games
    /var/ftp
    /
    /
    /
    /
    /var/empty/sshd
    /var/spool/postfix


    [root@jindada ~]# awk -F '[:]' '{print $1}' passwd
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    ftp
    nobody
    systemd-network
    dbus
    polkitd
    sshd
    postfix

    #默认分隔符为 空白字符

    [root@jindada ~]# ifconfig eth0
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
    inet6 fe80::3310:9d15:9ee4:43e8 prefixlen 64 scopeid 0x20<link>
    ether 00:0c:29:eb:ea:8d txqueuelen 1000 (Ethernet)
    RX packets 15508 bytes 1698801 (1.6 MiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 10471 bytes 1145384 (1.0 MiB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    [root@jindada ~]# ifconfig eth0 | awk 'NR==2'
    inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
    [root@jindada ~]# ifconfig eth0 | awk 'NR==2' | awk '{print $2}'
    10.0.0.100
    [root@jindada ~]# ifconfig eth0 | awk 'NR==2{print $2}'
    10.0.0.100

    #指定多个分隔符

    [root@jindada ~]# ip a s eth0
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:eb:ea:8d brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
    valid_lft forever preferred_lft forever
    inet6 fe80::3310:9d15:9ee4:43e8/64 scope link noprefixroute
    valid_lft forever preferred_lft forever
    [root@jindada ~]# ip a s eth0 | awk 'NR==3'
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
    [root@jindada ~]# ip a s eth0 | awk 'NR==3' | awk -F '[ /]' '{print $6}'
    10.0.0.100
    [root@jindada ~]# ip a s eth0 | awk 'NR==3' | awk -F '[ /]*' '{print $3}'
    10.0.0.100
    [root@jindada ~]# echo " // inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0" >ip.txt
    [root@jindada ~]# cat ip.txt
    // inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
    [root@jindada ~]# awk -F '[ /]' '{print $11}' ip.txt
    10.0.0.100
    [root@jindada ~]# awk -F '[ /]*' '{print $3}' ip.txt
    10.0.0.100


    #指定分隔符 使用双引号引起来

    [root@jindada ~]# awk -F: '{print $1":"$2}' passwd
    root:x
    bin:x
    daemon:x
    adm:x
    lp:x
    sync:x
    shutdown:x
    halt:x
    mail:x
    operator:x
    games:x
    ftp:x
    nobody:x
    systemd-network:x
    dbus:x
    polkitd:x
    sshd:x
    postfix:x
    [root@jindada ~]# awk -F: '{print $1"脱产10期"$2}' passwd
    root脱产10期x
    bin脱产10期x
    daemon脱产10期x
    adm脱产10期x
    lp脱产10期x
    sync脱产10期x
    shutdown脱产10期x
    halt脱产10期x
    mail脱产10期x
    operator脱产10期x
    games脱产10期x
    ftp脱产10期x
    nobody脱产10期x
    systemd-network脱产10期x
    dbus脱产10期x
    polkitd脱产10期x
    sshd脱产10期x
    postfix脱产10期x


    [root@jindada ~]# awk -F: '{print $NF":"$2":"$3":"$4":"$5":"$6":"$1}' passwd
    /bin/bash:x:0:0:root:/root:root
    /sbin/nologin:x:1:1:bin:/bin:bin
    /sbin/nologin:x:2:2:daemon:/sbin:daemon
    /sbin/nologin:x:3:4:adm:/var/adm:adm
    /sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
    /bin/sync:x:5:0:sync:/sbin:sync
    /sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
    /sbin/halt:x:7:0:halt:/sbin:halt
    /sbin/nologin:x:8:12:mail:/var/spool/mail:mail
    /sbin/nologin:x:11:0:operator:/root:operator
    /sbin/nologin:x:12:100:games:/usr/games:games
    /sbin/nologin:x:14:50:FTP User:/var/ftp:ftp
    /sbin/nologin:x:99:99:Nobody:/:nobody
    /sbin/nologin:x:192:192:systemd Network Management:/:systemd-network
    /sbin/nologin:x:81:81:System message bus:/:dbus
    /sbin/nologin:x:999:998:User for polkitd:/:polkitd
    /sbin/nologin:x:74:74:Privilege-separated SSH:/var/empty/sshd:sshd
    /sbin/nologin:x:89:89::/var/spool/postfix:postfix

  • 相关阅读:
    遗传算法
    UVa 11584 Partitioning by Palindromes
    UVa1625 Color Length
    UVa10003 Cutting Sticks
    UVa1347 Tour
    UVa116 (单向TSP,多决策问题)
    uVa 12563 Jin Ge Jin Qu
    模糊综合评判
    Python进阶(5)_进程与线程之协程、I/O模型
    Python进阶(4)_进程与线程 (python并发编程之多进程)
  • 原文地址:https://www.cnblogs.com/jhno1/p/13301617.html
Copyright © 2020-2023  润新知