zoukankan      html  css  js  c++  java
  • 文本处理三剑客_grep、awk、sed

    在shell中使用最多的支持正则表达式的处理文本的三个命令:

    ①grep:默认不支持扩展表达式,加-E选项开启ERE(扩展正则表达式),如果不加-E使用花括号要加转义符{}--------egrep支持基础和扩展正则表达式

    ②awk:支持egrep所有的正则表达式

    ③sed:默认不支持扩展表达式,加-r选项开启ERE。如果不加-r使用花括号要加转义符{}

    grep

    格式:grep  参数  文件

    作用:用于在文本中执行关键词搜索,并显示匹配结果

    参数

    -E  模式是扩展正则表达式

    -i  忽略大小写

    -n  打印行号

    -o  只打印匹配内容

    -c  只显示匹配次数

    -B  打印匹配的前几行

    -A  打印匹配的后几行

    -C  打印匹配的前后几行

    --color  匹配字体颜色

    -v  取反,打印不匹配的行

    awk

    格式

    作用:awk不仅仅是linux系统中的一个命令,而且是一种编程语言,用来处理数据,生成报告,可以在命令行直接操作,也可以编写成awk程序来进行更复杂的运用

    参数

    -F:指定分隔符

    NR:记录的编号,awk每读取一行,NR就+1,可以理解为行号

    NF:字段数量,记录了当前行包含多少字段,可以理解为有多少列

    FS:字符分隔符,默认的以空格为分隔符

    输出字段表达方式

    $1 $2 $3............$n  输出一个指定的字段

    $NF  输出最后一个字段

    $0  输出整条记录

    awk执行过程

    awk进阶--正则

    练习:提取本机IP地址

    awk特殊模式-BEGIN模式与END模式

    BEGIN模块在awk读取文件之前执行,一般来定义内置变量(预定义变量,eg:FS,RS)

    需要注意的是BEGIN模块后面要接一个action操作块,包含在大括号内。

    BEGIN模块

    END模块

    EHD在awk读取完所有的文件的时候,再执行END模块,一般用来输出一个结果(累加,数组结果),也可以是和BEGIN模块类似的结尾标识信息

    与BEGIN模式相对应的END模式,格式一样,但是END模式仅在awk处理完所有输入行后才进行处理。

    sed

    格式:sed  参数  命令  文件

    说明: 

    1,注意sed软件以及后面选项,sed命令和输入文件,每个元素之间都至少有一个空格。 

    2,sed -commands(sed命令)是sed软件内置的一些命令选项,为了和前面的options(选项)区分,故称为sed命令 

    3,sed -commands 既可以是单个sed命令,也可以是多个sed命令组合。

    4,input -file (输入文件)是可选项,sed还能够从标准输入如管道获取输入。

    作用:编辑文件 

    工作原理

    sed读取一行,首先将这行放入到缓存中

    然后,才对这行进行处理

    处理完成以后,将缓冲区的内容发送到终端

    存储sed读取到的内容的缓存区空间称之为:模式空间(Pattern Space)

    参数

    参数  

    解释说明

    -n  no

    取消默认的软件的输出,常与sed的命令的p连用
    -e  entry 一行命令语句可以执行多条sed命令,多分支
    -r  ruguler 使用扩展正则表达式,默认情况sed只识别基本正则表达式
    -i  inside  直接修改文件内容,而不是输出到终端
    命令  解释说明
    a  append 追加,在行后添加一行或多行文本
    c  change  取代指定的行
    d  delete 删除指定的行
    i  insert 插入,在指定行前添加一行或多行文本
    p  print  答应模式空间内容,常与-n一起连用
    特殊符号 解释说明
    对指定行以外的所有行执行命令

     sed增删改查

    1. 增

    这里我们需要用到2个sed命令,分别是:

    •   “a”:追加文本到指定行后,记忆方法:a的全拼是apend,意思是追加。
    •    “i“:插入文本到指定行前,记忆方法:i的全拼是insert,意思是插入。

    实例1:a

    复制代码
    [root@ken ~]# sed "2a 这是新添加的一行" test
    this is the first line
    this is the second line
    这是新添加的一行
    this is the third line
    this is the forth line
    this is the fivth line
    this is the sixth line
    this is the seventh line
    this is the eighth line
    this is the ninth line
    this is the tenth line
    复制代码
    1. 2代表指定对第2行操作,其他的行忽略
    2. a代表插入的意思,2i即在第2行前插入文本
    3. 2a后面加上空格,然后跟上你想要插入的文本即可

    实例2:i

    复制代码
    [root@ken ~]# sed "2i 我又新添加了一行" test
    this is the first line
    我又新添加了一行
    this is the second line
    this is the third line
    this is the forth line
    this is the fivth line
    this is the sixth line
    this is the seventh line
    this is the eighth line
    this is the ninth line
    this is the tenth line
    复制代码

    实例3:同时增加多行(/n)

    复制代码
    [root@ken ~]# sed "2i 这是第一条记录
    这是第二条记录
    这是第三条记录" test
    this is the first line
    这是第一条记录
    这是第二条记录
    这是第三条记录
    this is the second line
    this is the third line
    this is the forth line
    this is the fivth line
    this is the sixth line
    this is the seventh line
    this is the eighth line
    this is the ninth line
    this is the tenth line
    复制代码

     

    2.删

    • 这个功能也是非常得有用,比如我们想删除文件中的某些行,以前最常用的是vi或vim命令,但现在我们知道了sed命令,就应该使用这个高逼格的命令完成任务了。
    • “d”:删除文本,记忆方法:d的全拼是delete,意思是删除。
    • sed软件可以对单行或多行文本进行处理。如果在sed命令前面不指定地址范围,那么默认会匹配所有行。

    实例1:删除所有的行

    [root@ken ~]# cp test{,.bak}
    [root@ken ~]# sed 'd' test

    命令说明:如果在sed命令前面不指定地址范围,那么默认会匹配所有行,然后使用d命令删除功能就会删除这个文件的所有内容

    实例2:删除指定的行

    复制代码
    [root@ken ~]# cat test.bak >test
    [root@ken ~]# sed '2d' test
    this is the first line
    this is the third line
    this is the forth line
    this is the fivth line
    this is the sixth line
    this is the seventh line
    this is the eighth line
    this is the ninth line
    this is the tenth line
    复制代码

    实例3:删除指定范围行

    复制代码
    [root@ken ~]# sed '2,5d' test
    this is the first line
    this is the sixth line
    this is the seventh line
    this is the eighth line
    this is the ninth line
    this is the tenth line
    复制代码

    实例4:删除匹配的行

    复制代码
    [root@ken ~]# sed '/sixth/d' test
    this is the first line
    this is the second line
    this is the third line
    this is the forth line
    this is the fivth line
    this is the seventh line
    this is the eighth line
    this is the ninth line
    this is the tenth line
    复制代码

    命令说明:在sed软件中,使用正则的格式和awk一样,使用2个”/“包含指定的正则表达式,即“/正则表达式/”。

    实例5:删除指定行到行尾的内容

    [root@ken ~]# sed '2,$d' test
    this is the first line

    第二行也会被删掉

    实例6:取反

    一、

    [root@ken ~]# sed '2,3!d' test
    this is the second line
    this is the third line

    二、

    [root@ken ~]# sed '/tenth/!d' test
    this is the tenth line

    3.改

    • “c”:用新行取代旧行,记忆方法:c的全拼是change,意思是替换。
    复制代码
    [root@ken ~]# sed '2c 改过之后的第二行' test
    this is the first line
    改过之后的第二行
    this is the third line
    this is the forth line
    this is the fivth line
    this is the sixth line
    this is the seventh line
    this is the eighth line
    this is the ninth line
    this is the tenth line
    this is sixth line
    复制代码

    文本替换

      • 接下来说的这个功能,有工作经验的同学应该非常的熟悉,因为使用sed软件80%的场景就是使用替换功能。
      • 这里用到的sed命令,选项:
        “s”:单独使用-->将每一行中第一处匹配的字符串进行替换==>sed命令
        “g”:每一行进行全部替换-->sed命令s的替换标志之一(全局替换),非sed命令。
        “-i”:修改文件内容-->sed软件的选项,注意和sed命令i区别。

     

    sed软件替换模型

    sed -i 's/目标内容/替换内容/g'  ken.log
    sed -i 's#目标内容#替换内容#g'

    实例1:

    复制代码
    [root@ken ~]# sed 's/line/hang/g' test
    this is the first hang
    this is the second hang
    this is the third hang
    this is the forth hang
    this is the fivth hang
    this is the sixth hang
    this is the seventh hang
    this is the eighth hang
    this is the ninth hang
    this is the tenth hang
    this is sixth hang
    复制代码

    命令说明:从上面命令的结果我们就知道sed命令默认不会修改文件的内容

    实例2:

    复制代码
    [root@ken ~]# sed -i 's/line/hang/g' test
    [root@ken ~]# cat test
    this is the first hang
    this is the second hang
    this is the third hang
    this is the forth hang
    this is the fivth hang
    this is the sixth hang
    this is the seventh hang
    this is the eighth hang
    this is the ninth hang
    this is the tenth hang
    this is sixth hang
    复制代码

    命令说明:如果想真正的修改文件内容,我们就需要使用选项“-i”,这个要和sed命令“i”区分开来。同时我们可以发现命令执行后的结果是没有任何输出的。

    4.查

    • 这个功能也是非常得有用,比如我们想查看文件中的某些行,以前最常用的是cat或more或less命令等,但这些命令有些缺点,就是不能查看指定的行。而我们用了很久的sed命令就有了这个功能了。而且我们前面也说过使用sed比其他命令vim等读取速度更快!
    • 这里我们需要用到1个sed命令
    • “p”:输出指定内容,但默认会输出2次匹配的结果,因此使用-n选项取消默认输出,记忆方法:p的全拼是print,意思是打印。

    实例1:

    复制代码
    [root@ken ~]# sed '2p' test
    this is the first hang
    this is the second hang
    this is the second hang
    this is the third hang
    this is the forth hang
    this is the fivth hang
    this is the sixth hang
    this is the seventh hang
    this is the eighth hang
    this is the ninth hang
    this is the tenth hang
    this is sixth hang
    [root@ken ~]# sed -n '2p' test
    this is the second hang
    复制代码

    实例2:

    [root@ken ~]# sed -n '2,5p' test
    this is the second hang
    this is the third hang
    this is the forth hang
    this is the fivth hang

    实例3:

    [root@ken ~]# sed -n '/ninth/p' test
    this is the ninth hang

    补充:-e多点操作

    实例1:

    复制代码
    [root@ken ~]# sed -e '2d' -e '5d' test
    this is the first hang
    this is the third hang
    this is the forth hang
    this is the sixth hang
    this is the seventh hang
    this is the eighth hang
    this is the ninth hang
    this is the tenth hang
    this is sixth hang
    复制代码

    实例2:

    [root@ken ~]# sed -n -e '2p' -e '5p' test
    this is the second hang
    this is the fivth hang

    sed用法总结

    1.查找指定的字符串

    例子:显示/etc/passwd中保含root的行(显示模式空间中的内容)

    方法1:set '/root/p' /etc/passwd
    
    方法2:cat /etc/passwd | sed '/root/p'

    2.在指定的位置做增删

    例子:删除以root为开头的行

    # sed '/^root/d' a.txt

    例子:在包含root的行后添加一行 i am ken

    # sed '/root/a i am ken' a.txt

    3.按行替换

    例子:将5到9行的内容替换为 i am ken

    # sed '5,9c i am ken' a.txt

    4.按照字符替换

    例子:将/etc/selinux/config中的SELINUX=enforcing改成 disabled

    写法1:# sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' config

    写法2:# sed -r -i 's/(SELINUX=)disabled/1enforcing/g' config

    5.查找指定的内容再做替换

    例子:将以r开头的行中的oo替换为qq

    # sed '/^r/{s/oo/qq/g}' passwd

    6.多点编辑

    例子:去除文件中的注释行和空白行

    # grep -v -E "(^#)|(^$)" passwd.bak >passwd

    # cat passwd.bak | sed -e '/^#/d' -e '/^$/d' >passwd

    7)取反操作

    显示非1-3行

    # sed -n '1,3!p' passwd

  • 相关阅读:
    java.lang.NoClassDefFoundError: org.junit.runner.Runner
    SpringMVC 八大注解
    spring @autowired使用总结
    Git使用入门
    N21_栈的压入弹出序列。
    N20_包含min函数的栈
    N19_顺时针打印指针
    N17_判断树B是不是树A的子结构
    N16_合并两个排序链表
    N15_反转链表后,输出新链表的表头。
  • 原文地址:https://www.cnblogs.com/it-ljm1903/p/10836573.html
Copyright © 2011-2022 走看看