zoukankan      html  css  js  c++  java
  • Shell grep / sed 学习

    听说过sed 和 awk 比较强大,专门学习了一下。

    使用这些shell工具需要一些正则表达式的知识,这里先来了解一些特殊符号的意思:

    特殊符号 代表意义
    [:alnum:] 代表英文大小写字节及数字,亦即 0-9, A-Z, a-z
    [:alpha:] 代表任何英文大小写字节,亦即 A-Z, a-z
    [:blank:] 代表空白键与 [Tab] 按键两者
    [:cntrl:] 代表键盘上面的控制按键,亦即包括 CR, LF, Tab, Del.. 等等
    [:digit:] 代表数字而已,亦即 0-9
    [:graph:] 除了空白字节 (空白键与 [Tab] 按键) 外的其他所有按键
    [:lower:] 代表小写字节,亦即 a-z
    [:print:] 代表任何可以被列印出来的字节
    [:punct:] 代表标点符号 (punctuation symbol),亦即:" ' ? ! ; : # $...
    [:upper:] 代表大写字节,亦即 A-Z
    [:space:] 任何会产生空白的字节,包括空白键, [Tab], CR 等等
    [:xdigit:] 代表 16 进位的数字类型,因此包括: 0-9, A-F, a-f 的数字与字节

    结合实例来练习,使用下面的文本来练习(regular_express.txt):

    "Open Source" is a good mechanism to develop programs.
    apple is my favorite food.
    Football game is not use feet only.
    this dress doesn't fit me.
    However, this dress is about $ 3183 dollars.^M
    GNU is free air not free beer.^M
    Her hair is very beauty.^M
    I can't finish the test.^M
    Oh! The soup taste good.^M
    motorcycle is cheap than car.
    This window is clear.
    the symbol '*' is represented as start.
    Oh!     My god!
    The gd software is a library for drafting programs.^M
    You are the best is mean you are the no. 1.
    The world <Happy> is the same with "glad".
    I like dog.
    google is the best tools for search keyword.
    goooooogle yes!
    go! go! Let's go.
    # I am VBird


    搜寻特定字符串:

    [root@www ~]# grep -n 'the' regular_express.txt
    8:I can't finish the test.
    12:the symbol '*' is represented as start.
    15:You are the best is mean you are the no. 1.
    16:The world <Happy> is the same with "glad".
    18:google is the best tools for search keyword.

    反向选择,可以使用 'v' 选项:

    [root@www ~]# grep -vn 'the' regular_express.txt

    则所有包含 ‘the’ 的行都不会显示

    如果不需要区分大小写,可以使用 ‘i’ 选项:

    [root@www ~]# grep -in 'the' regular_express.txt
    8:I can't finish the test.
    9:Oh! The soup taste good.
    12:the symbol '*' is represented as start.
    14:The gd software is a library for drafting programs.
    15:You are the best is mean you are the no. 1.
    16:The world <Happy> is the same with "glad".
    18:google is the best tools for search keyword.

    利用中括号 [] 来搜寻集合字节

    [root@www ~]# grep -n 't[ae]st' regular_express.txt
    8:I can't finish the test.
    9:Oh! The soup taste good.

    如果需要匹配某一部分,但是不想匹配以某些开头的情况:(匹配oo,但不匹配以g开头的情况)

    [root@www ~]# grep -n '[^g]oo' regular_express.txt
    2:apple is my favorite food.
    3:Football game is not use feet only.
    18:google is the best tools for search keyword.
    19:goooooogle yes!

     但是我的机器上(使用ubuntu11.04),下面这种情况不能匹配成功:

    [root@www ~]# grep -n '[^a-z]oo' regular_express.txt
    3:Football game is not use feet only.  #本来该有这样的输出,但是却无输出

    转而使用下面的命令则成功:

    [root@www ~]# grep -n '[^[:lower:]]oo' regular_express.txt
    3:Football game is not use feet only.

    行首与行尾字节 ^ $

    [root@www ~]# grep -n '^the' regular_express.txt
    12:the symbol '*' is represented as start.

    匹配以小写字母开头的行:

    [root@www ~]# grep -n '^[a-z]' regular_express.txt
    2:apple is my favorite food.
    4:this dress doesn't fit me.
    10:motorcycle is cheap than car.
    12:the symbol '*' is represented as start.
    18:google is the best tools for search keyword.
    19:goooooogle yes!
    20:go! go! Let's go.

    匹配非大小写字母开头的行:(很纳闷,这里使用 [^a-zA-Z] 为什么就能成功?)

    [root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt
    1:"Open Source" is a good mechanism to develop programs.
    21:# I am VBird

    注意:那个 ^ 符号,在字节集合符号(括号[])之内与之外是不同的! 在 [] 内代表“反向选择”,在 [] 之外则代表定位在行首的意义!

    匹配空行:

    [root@www ~]# grep -n '^$' regular_express.txt
    22:

    任意一个字节 . 与重复字节 *

    [root@www ~]# grep -n 'goo*g' regular_express.txt
    18:google is the best tools for search keyword.
    19:goooooogle yes!

    在正则表达式中‘.’ 0个或1个任意字符,‘*’代表任意多个任意字符
     
    限定连续 RE 字符范围 {}

    [root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt
    18:google is the best tools for search keyword.

    上面的式子匹配了2个‘o’,表达式的意思是匹配2-5个‘o’
    若第二个参数为空,则匹配 第一个参数到无穷多个的情况:

    [root@www ~]# grep -n 'go\{2,\}g' regular_express.txt
    18:google is the best tools for search keyword.
    19:goooooogle yes!

    将基础的正规表示法特殊字符汇整如下:
    RE 字符 意义与范例
    ^word 意义:待搜寻的字串(word)在行首!
    范例:搜寻行首为 # 开始的那一行,并列出行号
    grep -n '^#' regular_express.txt
    word$ 意义:待搜寻的字串(word)在行尾!
    范例:将行尾为 ! 的那一行列印出来,并列出行号
    grep -n '!$' regular_express.txt
    . 意义:代表『一定有一个任意字节』的字符!
    范例:搜寻的字串可以是 (eve) (eae) (eee) (e e), 但不能仅有 (ee) !亦即 e 与 e 中间『一定』仅有一个字节,而空白字节也是字节!
    grep -n 'e.e' regular_express.txt
    \ 意义:跳脱字符,将特殊符号的特殊意义去除!
    范例:搜寻含有单引号 ' 的那一行!
    grep -n \' regular_express.txt
    * 意义:重复零个到无穷多个的前一个 RE 字符
    范例:找出含有 (es) (ess) (esss) 等等的字串,注意,因为 * 可以是 0 个,所以 es 也是符合带搜寻字串。另外,因为 * 为重复『前一个 RE 字符』的符号, 因此,在 * 之前必须要紧接著一个 RE 字符喔!例如任意字节则为 『.*』 !
    grep -n 'ess*' regular_express.txt
    [list] 意义:字节集合的 RE 字符,里面列出想要撷取的字节!
    范例:搜寻含有 (gl) 或 (gd) 的那一行,需要特别留意的是,在 [] 当中『谨代表一个待搜寻的字节』, 例如『 a[afl]y 』代表搜寻的字串可以是 aay, afy, aly 即 [afl] 代表 a 或 f 或 l 的意思!
    grep -n 'g[ld]' regular_express.txt
    [n1-n2] 意义:字节集合的 RE 字符,里面列出想要撷取的字节范围!
    范例:搜寻含有任意数字的那一行!需特别留意,在字节集合 [] 中的减号 - 是有特殊意义的,他代表两个字节之间的所有连续字节!但这个连续与否与 ASCII 编码有关,因此,你的编码需要配置正确(在 bash 当中,需要确定 LANG 与 LANGUAGE 的变量是否正确!) 例如所有大写字节则为 [A-Z]
    grep -n '[A-Z]' regular_express.txt
    [^list] 意义:字节集合的 RE 字符,里面列出不要的字串或范围!
    范例:搜寻的字串可以是 (oog) (ood) 但不能是 (oot) ,那个 ^ 在 [] 内时,代表的意义是『反向选择』的意思。 例如,我不要大写字节,则为 [^A-Z]。但是,需要特别注意的是,如果以 grep -n [^A-Z] regular_express.txt 来搜寻,却发现该文件内的所有行都被列出,为什么?因为这个 [^A-Z] 是『非大写字节』的意思, 因为每一行均有非大写字节,例如第一行的 "Open Source" 就有 p,e,n,o.... 等等的小写字
    grep -n 'oo[^t]' regular_express.txt
    \{n,m\} 意义:连续 n 到 m 个的『前一个 RE 字符』
    意义:若为 \{n\} 则是连续 n 个的前一个 RE 字符,
    意义:若是 \{n,\} 则是连续 n 个以上的前一个 RE 字符!
     范例:在 g 与 g 之间有 2 个到 3 个的 o 存在的字串,亦即 (goog)(gooog)
    grep -n 'go\{2,3\}g' regular_express.txt


    sed 工具:


    [root@www ~]# sed [-nefr] [动作]选项与参数:
    -n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN
    的数据一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过
    sed 特殊处理的那一行(或者动作)才会被列出来。
    -e :直接在命令列模式上进行 sed 的动作编辑;
    -f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的
    sed 动作;
    -r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
    -i :直接修改读取的文件内容,而不是由萤幕输出。

    动作说明: [n1[,n2]]function
    n1, n2 :不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作
    是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』

    function 有底下这些咚咚:
    a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
    c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
    d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
    i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
    p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
    s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配


    范例一:将 /etc/passwd 的内容列出并且列印行号,同时,请将第 2~5 行删除!
    [root@www ~]# nl /etc/passwd | sed '2,5d'
    1 root:x:0:0:root:/root:/bin/bash
    6 sync:x:5:0:sync:/sbin:/bin/sync
    7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    .....(后面省略).....


    范例二:承上题,在第二行后(亦即是加在第三行)加上『drink tea?』字样!
    [root@www ~]# nl /etc/passwd | sed '2a drink tea'
    1 root:x:0:0:root:/root:/bin/bash
    2 bin:x:1:1:bin:/bin:/sbin/nologin
    drink tea
    3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
    .....(后面省略).....


    范例三:我想将第2-5行的内容取代成为“No 2-5 number”呢?
    [root@www ~]# nl /etc/passwd | sed '2,5c No 2-5 number'
    1 root:x:0:0:root:/root:/bin/bash
    No 2-5 number
    6 sync:x:5:0:sync:/sbin:/bin/sync
    .....(后面省略).....

     

    范例四:仅列出 /etc/passwd 文件内的第 5-7 行
    [root@www ~]# nl /etc/passwd | sed -n '5,7p'
    5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    6 sync:x:5:0:sync:/sbin:/bin/sync
    7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

     
    部分数据的搜寻并取代的功能

    基本使用方式:

    sed 's/要被取代的字串/新的字串/g'

    步骤一:先观察原始信息,利用 /sbin/ifconfig 查询 IP 为何?
    [root@www ~]# /sbin/ifconfig eth0
    eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84
    inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
    inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    .....(以下省略).....
    # 因为我们还没有讲到 IP ,这里你先有个概念即可啊!我们的重点在第二行,
    # 也就是 192.168.1.100 那一行而已!先利用关键字捉出那一行!

    步骤二:利用关键字配合 grep 撷取出关键的一行数据
    [root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr'
    inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
    # 当场仅剩下一行!接下来,我们要将开始到 addr: 通通删除,就是像底下这样:
    # inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
    # 上面的删除关键在於『 ^.*inet addr: 』啦!正规表示法出现! ^_^

    步骤三:将 IP 前面的部分予以删除
    [root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | \
    > sed 's/^.*addr://g'
    192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
    # 仔细与上个步骤比较一下,前面的部分不见了!接下来则是删除后续的部分,亦即:
    # 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
    # 此时所需的正规表示法为:『 Bcast.*$ 』就是啦!

    步骤四:将 IP 后面的部分予以删除
    [root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | \
    > sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
    192.168.1.100

    范例六:利用 sed 将 regular_express.txt 内每一行结尾若为 . 则换成 !
    [root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt  

    # 上头的 -i 选项可以让你的 sed 直接去修改后面接的文件内容而不是由屏幕输出!注意使用的时候需要对 ‘.’ 进行转义,而且需要指明是对结尾为‘.’ 的进行替换,使用 $ 符号

    这里就顺便来实践一下,我有一个dos环境下创建的文件,当复制到 unix 环境下之后,结尾就会出现难看的“^M” 符号,如果要把这个符号去掉使用sed工具可以轻松搞定:

    [root@www ~]# sed -i 's/\^M$//g' regular_express.txt

    :学习内容来源于“鸟哥的私房菜”,自己都在Ubuntu11.04下运行过这些实例,最后一个是自己使用sed解决的一个实际问题。

  • 相关阅读:
    hdu--2578--与女孩约会
    hdu--2588--欧拉函数||容斥原理
    hdu--2586--lca_tarjan<证明tarjan这个人很流弊>
    hdu--3743--归并排序<自顶向下&&自底向上>2种写法
    hdu--4911--归并排序||树状数组
    hdu--2639--01背包第K解<不同决策的不同解法>
    hdu--2642--二维BIT
    hdu--3833--4000ms才能过的O(N^2)算法
    hdu--3835--最简单的数学..
    hdu--3836--tarjan+缩点
  • 原文地址:https://www.cnblogs.com/zhuyp1015/p/2572289.html
Copyright © 2011-2022 走看看