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

  • 相关阅读:
    windows安装python2.7后的注册(registry)问题
    用python解析pdf中的文本与表格【pdfplumber的安装与使用】
    python pdfplumber用于pdf表格提取
    python xlsxwriter写excel并操作各种格式属性
    ShellExecute, WinExec, CreateProcess区别
    Python调用Windows外部程序
    pynput使用简单说明
    有关/proc/uptime这个文件里两个参数所代表的意义
    Beyond Compare 4 提示错误“这个授权密钥已被吊销”的解决办法
    Android: adb push apk 到 system/app 目录时报“remote Read-only file system”
  • 原文地址:https://www.cnblogs.com/xiaofeng666/p/13771325.html
Copyright © 2011-2022 走看看