zoukankan      html  css  js  c++  java
  • shell中awk模式小结

    awk是文本处理工具,功能小结如下:

    1、条件表达式

      

    [root@centos17 shell]# awk -F: '$3>1&&$3<3{print $0}' /etc/passwd
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    [root@centos17 shell]# awk -F: '{if($3>1&&$3<3){print $0}}' /etc/passwd
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    [root@centos17 shell]# awk -F: '{if($3>1&&$3<3)print $0}' /etc/passwd
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    [root@centos17 shell]# 
    

      

    2、算术运算+ - * / % (模)  ^(幂2^3)

    可以在模式中执行计算,awk都将按浮点数方式执行算术运算

    [root@centos17 shell]# awk -F: '$3*5 > 10000' /etc/passwd
    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    [root@centos17 shell]# awk -F: '{if($3*5>10000){print $0}}' /etc/passwd
    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    

     

    3、逻辑操作符和复合模式

    &&   逻辑与  a&&b

    ||   逻辑或  a||b

    !    逻辑非    !a

    [root@centos17 ~]# awk -F: '$1~/root/&&$3<=2' /etc/passwd
    #root:x:0:0:root:/root:/usr/sbin/poweroff
    root:x:0:0:root:/root:/bin/bash
    [root@centos17 ~]# awk -F: '$1~/root/||$3<=1' /etc/passwd
    #root:x:0:0:root:/root:/usr/sbin/poweroff
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    [root@centos17 ~]# awk -F: '!($1~/root/||$3>=2)' /etc/passwd
    bin:x:1:1:bin:/bin:/sbin/nologin
    [root@centos17 ~]# 
    

     

    4、范围模式

    [root@centos17 ~]# awk '/sshd/,/feng/' /etc/passwd
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    chrony:x:997:995::/var/lib/chrony:/sbin/nologin
    nginx:x:996:992:nginx user:/var/cache/nginx:/sbin/nologin
    feng:x:1000:1000::/home/feng:/sbin/poweroff
    [root@centos17 ~]# 
    

     

    5、正则表达式

    awk '/^alice/' /etc/passwd
    awk '$0~/^alice/' /etc/passwd
    awk '!/alice/' /etc/passwd
    awk  -F: '$NF~/bash$/' /etc/passwd
    awk -F: '$1~/^alice/' /etc/passwd
    

      

    6、关系运算符

    运算符        含义        示例
    <         小于        x<y
    <=        小于或等于    x<=y
    ==        等于       x==y
    !=        不等于       x!=y
    >=        大于或等于      x>=y
    >        大于        x>y

    awk -F: '$3==0' /etc/passwd
    awk -F: '$3<10' /etc/passwd
    awk -F: '$7=="/bin/bash"' /etc/passwd
    awk -F: '$1!~/alice/' /etc/passwd
    

      

     

    awk示例:

    awk '/west/' datafile
    awk '/^north/' datafile
    awk '$3~/^north/' datafile
    awk '/^(no|so)/' datafile
    awk '{print $3,$2}' datafile

    awk '{print $3 $2}' datafile
    awk '{print $0}' datafile
    awk '/north/{print $3,$2}' datafile
    awk '/E/' datafile

    awk '/^[ns]/{print $1}' datafile
    awk '$5~/.[7-9]+/' datafile
    awk '$2!~/E/{print $1,$2}' datafile
    awk '$3~/^joel/{print $3" is a nice boy."}' datafile
    awk '$8~/[0-9][0-9]$/{print $8}' datafile

    awk '$4~/Chin$/{print "The price is $"$8"."}' datafile
    awk '/Tj/{print $0}' datafile

    awk -F"[ :]" '{print $0}' datafile
    awk -F"[ :]+" '{print $0}' datafile

    awk '$7==5' datafile
    awk '$7!=5' datafile
    awk '$3=="CT"{print $1,$2}' datafile

    awk -F: '$2~/nest/{print $3+50}' datafile
    awk -F: '$3~/rpc$/{$1="laowang";print $0}' datafile

  • 相关阅读:
    bzoj1453
    276D
    855E
    bzoj1458
    树莓派 自启动连接阿里云物联网平台(一)
    树莓派 实现脚本程序自启动
    树莓派4B 系统镜像安装与boot配置
    2019-2020-2《网络对抗技术》 Exp1 PC平台逆向破解
    2019-2020-2 《网络对抗技术》 Exp0 kali 安装
    kali 添加使用 KDE 桌面环境
  • 原文地址:https://www.cnblogs.com/xiaofeng666/p/13771325.html
Copyright © 2011-2022 走看看