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
  • 相关阅读:
    什么是布局?Android中的布局是怎样的?
    如何优化UI布局?
    Android SDK提供的常用控件Widget “常用控件”“Android原生”
    Android中Adapter类的使用 “Adapter”
    Android自定义属性
    Android中View的绘制流程(专题讲解)
    Android的自定义View及View的绘制流程
    如何创建新控件? “复合控件”“定制控件”
    Android应用程序支持不同屏幕(尺寸、密度)
    支持不同Android设备,包括:不同尺寸屏幕、不同屏幕密度、不同系统设置
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14695013.html
Copyright © 2011-2022 走看看