zoukankan      html  css  js  c++  java
  • vim grep sed awk对大小写不敏感

    环境

    1 [root@osker ~]# cat /etc/redhat-release
    2 CentOS Linux release 7.6.1810 (Core)
    3 [root@osker ~]# uname -r
    4 3.10.0-957.el7.x86_64
    1 [root@osker ~]# cat 3.txt
    2 Mike Harrington:[510] 548-1278:250:100:175
    3 Christian Dobbins:[408] 538-2358:155:90:201
    4 tom
    5 Chet Main:[510] 548-5258:50:95:135
    6 Tom Savage:[408] 926-3456:250:168:200

    文档中有tom和Tom两个关键词


    vim
    :set ic   (ignorecase 的缩写)忽略大小写
    :set noic  (noignorecase 的缩写)不忽略大小写



    grep
    查询man帮助可以找到,使用-i参数可以忽略大小。
    -i, --ignore-case:Ignore case distinctions in both the PATTERN and the input files.  (-i is specified by POSIX.)

    1 [root@osker ~]# grep -i 'tom' 3.txt
    2 tom
    3 Tom Savage:[408] 926-3456:250:168:200


    sed

    1 [root@osker ~]# sed -n '/tom/Ip' 3.txt
    2 tom
    3 Tom Savage:[408] 926-3456:250:168:200
    4 [root@osker ~]# sed -n '/tom/ip' 3.txt
    5 p

    可以看出在/后加入 I 可以忽略大小写过滤,使用i会出错。

    1 [root@osker ~]# sed -n 's#tom#qiu#gp' 3.txt
    2 qiu
    3 [root@osker ~]# sed -n 's#tom#qiu#gpi' 3.txt
    4 qiu
    5 qiu Savage:[408] 926-3456:250:168:200
    6 [root@osker ~]# sed -n 's#tom#qiu#gpI' 3.txt
    7 qiu
    8 qiu Savage:[408] 926-3456:250:168:200

    在使用替换功能时候可以时用i或者I,对需要替换的词忽略大小写。
    在GNU找了好久,找到这么一段话:
    i I :The I modifier to regular-expression matching is a GNU extension which makes sed match regexp in a case-insensitive manner.
    引用链接:
    https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html#The-_0022s_0022-Command


    awk

    1 [root@osker ~]# awk '/tom/' IGNORECASE=1 3.txt
    2 tom
    3 Tom Savage:[408] 926-3456:250:168:200

    查看man帮助可以查询到,awk内置了一个IGNORECASE变量,专门用于处理大小写的忽略。当IGNORECASE的值为真时,则进行忽略大写的匹配。
    IGNORECASE:
    Controls the case-sensitivity of all regular expression and string operations.  If IGNORECASE has a non-zero value, then string comparisons and pattern matching in rules, field splitting with FS and FPAT, record separating with RS, regular expression matching with ~ and  !~,  and  the  gensub(),  gsub(),  index(),  match(),  patsplit(),  split(),  and  sub()  built-in  functions  all  ignore  case when doing regular expression operations.  NOTE: Array subscripting is not affected.  However, the asort() and asorti() functions are affected.
    Thus, if IGNORECASE is not equal to zero, /aB/ matches all of the strings "ab", "aB", "Ab", and "AB".  As with all AWK variables, the initial value of IGNORECASE is zero, so all regular expression and string operations are normally case-sensitive.


  • 相关阅读:
    音频算法之我思
    图像去模糊算法 循序渐进 附完整代码
    音频算法之小黄人变声 附完整C代码
    RocketMQ(2)---核心概念、特性、使用等
    RocketMQ(1)---架构原理及环境搭建
    RabbitMQ(2)---高级使用
    面试问题---JAVA程序CPU占用过高怎么定位
    RabbitMQ(1)---基本概念及简单demo
    JUC(4)---java线程池原理及源码分析
    JUC(3)---CountDownLatch、CyclicBarrier和AQS
  • 原文地址:https://www.cnblogs.com/osker/p/12678253.html
Copyright © 2011-2022 走看看