zoukankan      html  css  js  c++  java
  • linux系统中如何精确匹配0

    1、测试数据

    root@DESKTOP-1N42TVH:/home/test/test# cat test.txt
    2013    2014    2013    2014
    1       1.3     0       0
    0.9     1.7     0       0
    0.9     1.3     4.2     0.9
    1       1.6     0       0.9
    0       1.6     0       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0

    2、awk实现只匹配0的行

    root@DESKTOP-1N42TVH:/home/test/test# cat test.txt
    2013    2014    2013    2014
    1       1.3     0       0
    0.9     1.7     0       0
    0.9     1.3     4.2     0.9
    1       1.6     0       0.9
    0       1.6     0       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0
    root@DESKTOP-1N42TVH:/home/test/test# awk '/^0[^.]/ || /[\t ]0[^.]/' test.txt
    1       1.3     0       0
    0.9     1.7     0       0
    1       1.6     0       0.9
    0       1.6     0       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0
    root@DESKTOP-1N42TVH:/home/test/test# cat test.txt
    2013    2014    2013    2014
    1       1.3     0       0
    0.9     1.7     0       0
    0.9     1.3     4.2     0.9
    1       1.6     0       0.9
    0       1.6     0       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0
    root@DESKTOP-1N42TVH:/home/test/test# awk '/^0[\t ]/ || /[\t ]0[\t ]/' test.txt
    1       1.3     0       0
    0.9     1.7     0       0
    1       1.6     0       0.9
    0       1.6     0       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0
    root@DESKTOP-1N42TVH:/home/test/test# cat test.txt
    2013    2014    2013    2014
    1       1.3     0       0
    0.9     1.7     0       0
    0.9     1.3     4.2     0.9
    1       1.6     0       0.9
    0       1.6     3       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0
    0.9     1.3     8       0
    root@DESKTOP-1N42TVH:/home/test/test# awk '/^0[^.]/ || /\t0[^.]/ || /\t0$/' test.txt
    1       1.3     0       0
    0.9     1.7     0       0
    1       1.6     0       0.9
    0       1.6     3       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0
    0.9     1.3     8       0

    3、grep实现

    
    

    root@DESKTOP-1N42TVH:/home/test/test# cat test.txt
    2013 2014 2013 2014
    1 1.3 0 0
    0.9 1.7 0 0
    0.9 1.3 4.2 0.9
    1 1.6 0 0.9
    0 1.6 3 0.9
    0.9 1.2 0 0
    0.9 1.3 0 0
    0.9 1.3 8 0
    root@DESKTOP-1N42TVH:/home/test/test# sed 's/\t/ /g' test.txt > test2.txt
    root@DESKTOP-1N42TVH:/home/test/test# ls
    test.txt test2.txt
    root@DESKTOP-1N42TVH:/home/test/test# cat test2.txt
    2013 2014 2013 2014
    1 1.3 0 0
    0.9 1.7 0 0
    0.9 1.3 4.2 0.9
    1 1.6 0 0.9
    0 1.6 3 0.9
    0.9 1.2 0 0
    0.9 1.3 0 0
    0.9 1.3 8 0
    root@DESKTOP-1N42TVH:/home/test/test# grep -E "^0[^.]| 0 | 0$" test2.txt
    1 1.3 0 0
    0.9 1.7 0 0
    1 1.6 0 0.9
    0 1.6 3 0.9
    0.9 1.2 0 0
    0.9 1.3 0 0
    0.9 1.3 8 0

     

    b、grep命令实现

    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    2013    2014    2013    2014
    1       1.3     0       0
    0.9     1.7     0       0
    0.9     1.3     4.2     0.9
    1       1.6     0       0.9
    0       1.6     0       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0
    root@DESKTOP-1N42TVH:/home/test# cat -A a.txt
    2013    2014    2013    2014$
    1       1.3     0       0$
    0.9     1.7     0       0$
    0.9     1.3     4.2     0.9$
    1       1.6     0       0.9$
    0       1.6     0       0.9$
    0.9     1.2     0       0$
    0.9     1.3     0       0$
    root@DESKTOP-1N42TVH:/home/test# grep $'[\t ]\+0' a.txt   ## 提取0前面有空格或者制表符的行
    1       1.3     0       0
    0.9     1.7     0       0
    0.9     1.3     4.2     0.9
    1       1.6     0       0.9
    0       1.6     0       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0
    root@DESKTOP-1N42TVH:/home/test# grep $'[\t ]\+0' a.txt | grep $'0[\t ]\+'  ## 利用管道,增加提取0后面有空格或者制表符的行
    1       1.3     0       0
    0.9     1.7     0       0
    1       1.6     0       0.9
    0       1.6     0       0.9
    0.9     1.2     0       0
    0.9     1.3     0       0
  • 相关阅读:
    java环境变量配置 win7/win8 java配置
    (JSON转换)String与JSONObject、JSONArray、JAVA对象和List 的相互转换
    (yum)更新yum报错:yum makecache: error: argument timer: invalid choice: 'fast' (choose from 'timer')
    (ElasticSearch)中文字符串精确搜索 term 搜不到结果
    (端口)打开阿里云服务组端口和防火墙端口
    (乱码)Spring Boot配置文件出现乱码解决方案
    (特殊字符)url中包含特殊字符导致请求报错的解决方案
    (端口占用)Windows 查看所有端口和PID
    (注释)IDEA快捷键注释不能自动对齐
    (JDK)oracle下载jdk需要注册?
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15755613.html
Copyright © 2011-2022 走看看