zoukankan      html  css  js  c++  java
  • linux系统中awk命令 正则匹配

    1、测试数据

    [root@centos7 test3]# cat b.txt
    e t s e
    s g m x
    w d g i
    d t e g
    x g e w

    2、打印匹配w的行

    [root@centos7 test3]# cat b.txt
    e t s e
    s g m x
    w d g i
    d t e g
    x g e w
    [root@centos7 test3]# awk '/w/' b.txt
    w d g i
    x g e w
    [root@centos7 test3]# awk '/w/ {print $1, $3}' b.txt
    w g
    x e

    3、打印第一列匹配w的行

    [root@centos7 test3]# cat b.txt
    e t s e
    s g m x
    w d g i
    d t e g
    x g e w
    [root@centos7 test3]# awk '$1 ~ /w/' b.txt
    w d g i
    [root@centos7 test3]# awk '$1 ~ /w/ {print $1,$4}' b.txt
    w i

    4、打印全文没有匹配w的行

    [root@centos7 test3]# cat b.txt
    e t s e
    s g m x
    w d g i
    d t e g
    x g e w
    [root@centos7 test3]# awk '!/w/' b.txt
    e t s e
    s g m x
    d t e g

    5、打印第一列没有匹配w的行

    [root@centos7 test3]# cat b.txt
    e t s e
    s g m x
    w d g i
    d t e g
    x g e w
    [root@centos7 test3]# awk '$1 !~ /w/' b.txt
    e t s e
    s g m x
    d t e g
    x g e w

    6、打印同时匹配s或者w的行

    [root@centos7 test3]# cat b.txt
    e t s e
    s g m x
    w d g i
    d t e g
    x g e w
    [root@centos7 test3]# awk '/[sw]/' b.txt
    e t s e
    s g m x
    w d g i
    x g e w

    7、打印第3列匹配e同时第四列匹配g的行

    [root@centos7 test3]# cat b.txt
    e t s e
    s g m x
    w d g i
    d t e g
    x g e w
    [root@centos7 test3]# awk '$3 ~ /e/' b.txt
    d t e g
    x g e w
    [root@centos7 test3]# awk '$3 ~ /e/ && $4 ~ /g/' b.txt
    d t e g

    8、输出第3列没有匹配e的行

    [root@centos7 test3]# cat b.txt
    e t s e
    s g m x
    w d g i
    d t e g
    x g e w
    [root@centos7 test3]# awk '$3 !~ /e/' b.txt
    e t s e
    s g m x
    w d g i

    9、匹配包含两个关键词中间的所有行

    [root@centos7 test3]# cat c.txt
    1 张三 历史 81 B 0.367
    2 李四 物理 72 C 0.588
    3 李华 数学 87 B+ 0.677
    4 方咪 历史 91 A 0.876
    5 陈明 语文 81 B 0.812
    6 鱼鱼 英语 81 B 0.571
    1 张三 历史 81 B 0.367
    2 李四 物理 72 C 0.588
    3 李华 数学 87 B+ 0.677
    4 方咪 历史 91 A 0.876
    5 陈明 语文 81 B 0.812
    6 鱼鱼 英语 81 B 0.571
    [root@centos7 test3]# awk '/李四/,/陈明/' c.txt
    2 李四 物理 72 C 0.588
    3 李华 数学 87 B+ 0.677
    4 方咪 历史 91 A 0.876
    5 陈明 语文 81 B 0.812
    2 李四 物理 72 C 0.588
    3 李华 数学 87 B+ 0.677
    4 方咪 历史 91 A 0.876
    5 陈明 语文 81 B 0.812
    [root@centos7 test3]# cat a.txt
    1 张三 历史 81 B 0.367
    2 李四 物理 72 C 0.588
    3 李华 数学 87 B+ 0.677
    4 方咪 历史 91 A 0.876
    5 陈明 语文 81 B 0.812
    6 鱼鱼 英语 81 B 0.571
    [root@centos7 test3]# awk '$4==72,$4==91 {print $0}' a.txt
    2 李四 物理 72 C 0.588
    3 李华 数学 87 B+ 0.677
    4 方咪 历史 91 A 0.876
  • 相关阅读:
    Windows 下安装 Python + Django
    asp.net core 一个中小型项目实战的起手式——Swagger配置
    asp.net core 一个中小型项目实战的起手式——项目搭建与仓储模式下的持久层创建(1)
    c#一些常用知识点
    ADO.NET中的5个主要对象
    JavaScript的闭包特性如何给循环中的对象添加事件(一)
    在什么情况下会用到虚方法?它与接口有什么不同?
    一般处理程序Session
    什么是code-Behind技术?
    什么是事务?
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14695013.html
Copyright © 2011-2022 走看看