文本处理三剑客_grep、awk、sed

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

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

②awk:支持egrep全部的正则表达式shell

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

 

grepvim

格式:grep  参数  文件数组

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

参数app

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

-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 这是第一条记录\n这是第二条记录\n这是第三条记录" 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:取反

1、

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

 

2、

[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

相关文章
相关标签/搜索