1、grep

grep是一个强大的文本搜索工具

(1)用法

grep [选项] [文件]


常用选项及含义

选项

含义

-c

只输出匹配行的数量

-i

搜索时忽略大小写

-h

查询多文件时不显示文件名

-n

列出所匹配行,并显示行号

-w

匹配整词

-x

匹配整行

-r

递归搜索

-E

支持扩展的正则表达式

-F

不支持正则,按照字符串字面意义进行匹配

(2)示例

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W26lBOBC-1627306788259)(D:\Desktop\markdown笔记图片\Snipaste210705_215101.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-45Pjw3AG-1627306788265)(D:\Desktop\markdown笔记图片\Snipaste210705_215747.png)]

2、sed

sed是一个非交互式的行文本编辑器,它与文本编辑器的区别是操作对象不同。文本编辑器操作对象是整个文件,而sed操作对象是文本中的一行。

grep console配色方案_字符串

sed只对缓冲区中的原始文件的副本进行编辑后输出到屏幕,并不编辑原始文件。因此若需要保存改动的内容,需要重定向到另一个文件。

(1)用法

sed [选项] `{命令}[flags]` 文件名


选项及含义

选项

含义

-n

不打印所有行到标准输出

-e

表示将下一个字符串解析为sed编辑命令,如果只传一个编辑命令 -e可忽略

sed [opt] -e command1 -e command2 ... -e commandn filename

-f

表示正在调用sed脚本

sed [opt] -f scriptfile inputfile

-i

直接改动源文件,慎用。可以用-i.bak修改源文件的同时创建.bak备份文件

sed命令定位文本的方法

选项

含义

x

x指定行号

x,y

指定x行到y行范围

/pattern/

匹配包含pattern的行

/pattern/pattern/

查询包含两个模式的行

/pattern/,x

从与pattern的匹配行到x行之间的行

x,/pattern/

从x行到与pattern匹配行之间的行

x,y!

查询不包括x和y行号的行

sed常用编辑命令

选项

含义

p

打印匹配的行

=

打印文件行号

a\

在定位行号之后追加

i\

在定位行号之前插入

d

删除定位行

c\

表示将指定行中的所有内容,替换成选项后面的字符串

s

替换指定字符串

w filename

将文本写入到文件

r filename

从filename文本中读行

y

变换字符,把一个字符翻译成另外的字符

q

第一个模式匹配完退出

n

读取下一个输入行,用下一个命令处理新的行

h

将模式缓冲区的文本复制到保持缓冲区

H

将模式缓冲区的文本追加到保持缓冲区

x

互换模式缓冲区和保持缓冲区的内容

g

将保持缓冲区的内容复制到模式缓冲区

G

将保持缓冲区的内容追加到模式缓冲区

(2)示例

①sed命令选项的例子

-n选项
[root@localhost shell]# cat example				#用到的示例文本
1 white fox is looking at a white dog
2 white fox is looking at a white dog
3 white fox is looking at a white dog
4 white fox is looking at a white dog
5 white fox is looking at a white dog
[root@localhost shell]# sed -n '1p' example		#带-n选项,打印第一行
1 white fox is looking at a white dog
[root@localhost shell]# sed  '1p' example		#不带-n选项,打印第一行
1 white fox is looking at a white dog
1 white fox is looking at a white dog
2 white fox is looking at a white dog
3 white fox is looking at a white dog
4 white fox is looking at a white dog
5 white fox is looking at a white dog
[root@localhost shell]# sed -n '2,4p' example	#带-n打印2到4行
2 white fox is looking at a white dog
3 white fox is looking at a white dog
4 white fox is looking at a white dog
#可以清楚的看到-n选项不打印所有行是指不打印sed编辑对象的全部内容,只打印指定行
-e选项

sed不支持同时带多个编辑命令的用法。如:sed -n '/2/p=' example这样是错误的。要想使用多个编辑命令使用如下格式

sed [选型] -e '编辑命令1' -e '编辑命令2' ... -e '编辑命令n' filename
[root@localhost shell]# cat example				#用到的示例文本
1 white fox is looking at a white dog
2 white fox is looking at a white dog
3 white fox is looking at a white dog
4 white fox is looking at a white dog
5 white fox is looking at a white dog
[root@localhost shell]# sed -n -e '2p' -e '=' example	
1
2 white fox is looking at a white dog
2
3
4
5
#第一个编辑命令'2p'表示打印第二个,第二个编辑命令'='打印所有行号
-f选项

vim 创建一个sed脚本文件vim sedscript 执行chmod +x sedscript授予脚本执行权限,脚本内容如下

#!/bin/sed -f	#和bash一样指明解释器的路径
2,4a\			#2到4行追加 ‘\’仅仅表示此处换行追加文本					
we append a new line
5s/fox/cat/			#5行将fox替换为cat
1d					#删除第一行
[root@localhost sed]# vim sedscript
[root@localhost sed]# chmod u+x sedscript
[root@localhost sed]# ./sedscript example
2 white fox is looking at a white dog
we append a new line
3 white fox is looking at a white dog
we append a new line
4 white fox is looking at a white dog
we append a new line
5 white cat is looking at a white dog
#由结果看出上面的sed脚本执行成功,第一行删除,2到4行追加,第5行替换
-i选项
[root@localhost sed]# sed -i.bak 's/fox/cat/' example
[root@localhost sed]# ls
example  example.bak
[root@localhost sed]# cat example.bak
1 white fox is looking at a white dog
2 white fox is looking at a white dog
3 white fox is looking at a white dog
4 white fox is looking at a white dog
5 white fox is looking at a white dog
[root@localhost sed]# cat example
1 white cat is looking at a white dog
2 white cat is looking at a white dog
3 white cat is looking at a white dog
4 white cat is looking at a white dog
5 white cat is looking at a white dog
# -i.bak修改源文件并创建.bak副本 s/fox/cat替换全文的fox为cat

②sed文本定位的例子

[root@localhost sed]# cat example	#示例文本
1 Yellow fox is looking at a white dog
2 Red fox is looking at a white dog
3 Blue fox is looking at a white dog
4 white fox is looking at a white dog
5 green fox is looking at a white dog
[root@localhost sed]# sed -n '3p' example			#x
3 Blue fox is looking at a white dog
[root@localhost sed]# sed -n '2,4p' example			#x,y
2 Red fox is looking at a white dog
3 Blue fox is looking at a white dog
4 white fox is looking at a white dog
[root@localhost sed]# sed -n '/green/p' example		#/pattern/
5 green fox is looking at a white dog
[root@localhost sed]# sed -n '2,5!p' example		#x,y!
1 Yellow fox is looking at a white dog
[root@localhost sed]# sed -n '1,/Blue/p' example	#x,/pattern/
1 Yellow fox is looking at a white dog
2 Red fox is looking at a white dog
3 Blue fox is looking at a white dog

③sed编辑命令的例子

i插入文本

插入文本是在匹配行的前面插入一行

[root@localhost sed]# cat example
1 Yellow fox is looking at a white dog
2 Red fox is looking at a Red dog
3 Blue fox is looking at a white dog
4 white fox is looking at a white dog
5 green fox is looking at a white dog
[root@localhost sed]# sed  '2i\we append a new line' example	#在第二行之前追加新行
1 Yellow fox is looking at a white dog
we append a new line
2 Red fox is looking at a Red dog
3 Blue fox is looking at a white dog
4 white fox is looking at a white dog
5 green fox is looking at a white dog
[root@localhost sed]# sed  '/dog/i\we append a new line' example	#在包含dog行的前一行追加行
we append a new line
1 Yellow fox is looking at a white dog
we append a new line
2 Red fox is looking at a Red dog
we append a new line
3 Blue fox is looking at a white dog
we append a new line
4 white fox is looking at a white dog
we append a new line
5 green fox is looking at a white dog
c修改文本

修改文本是指将所有匹配的文本行利用新文本代替

[root@localhost sed]# sed  '2c\a new line' example	#第二行用新行代替
1 Yellow fox is looking at a white dog
a new line
3 Blue fox is looking at a white dog
4 white fox is looking at a white dog
5 green fox is looking at a white dog
[root@localhost sed]# sed  '/green/c\a new line' example	#含green的行用新行代替
1 Yellow fox is looking at a white dog
2 Red fox is looking at a Red dog
3 Blue fox is looking at a white dog
4 white fox is looking at a white dog
a new line
[root@localhost sed]#
d删除

可以删除指定行、指定范围行、包含某些字符串的行

[root@localhost sed]# sed  '2,3d' example	#删除2到3行
1 Yellow fox is looking at a white dog
4 white fox is looking at a white dog
5 green fox is looking at a white dog
[root@localhost sed]# sed  '2,/Blue/d' example	#删除2行到包含Blue的行
1 Yellow fox is looking at a white dog
4 white fox is looking at a white dog
5 green fox is looking at a white dog
[root@localhost sed]#
s替换

替换可以将匹配的文本利用新文本替换,替换的格式为:s/被替换的字符串/新字符串/[替换选项]

选项

含义

g

所有匹配到的字符串全被替换

p

与-n结合,指打印替换行

w filename

表示将输出定向到一个文件

n(数字)

范围1-512之间,表示替换某行中第n次匹配的字符串

[root@localhost sed]# cat example	#查看示例文本
1 dog Yellow fox is looking at a white dog
2 dog Red fox is looking at a Red dog
3 dog Blue fox is looking at a white dog
4 dog white fox is looking at a white dog
5 dog green fox is looking at a white dog
[root@localhost sed]# sed  '2,3s/dog/cat/' example	#2-3行的dog替换成cat
1 dog Yellow fox is looking at a white dog
2 cat Red fox is looking at a Red dog
3 cat Blue fox is looking at a white dog
4 dog white fox is looking at a white dog
5 dog green fox is looking at a white dog
[root@localhost sed]# sed  -n '2,3s/dog/cat/p' example	#p选项与上面对比,看出指打印了替换的行
2 cat Red fox is looking at a Red dog
3 cat Blue fox is looking at a white dog
————————————————————————————————————————————分割线——————————————————————————————————————————————————
[root@localhost sed]# sed 's/dog/cat/' example		#不加g选项,只替换了每行的第一个dog
1 cat Yellow fox is looking at a white dog
2 cat Red fox is looking at a Red dog
3 cat Blue fox is looking at a white dog
4 cat white fox is looking at a white dog
5 cat green fox is looking at a white dog	
[root@localhost sed]# sed 's/dog/cat/g' example		#加g选项,替换了文本中所有出现的dog
1 cat Yellow fox is looking at a white cat
2 cat Red fox is looking at a Red cat
3 cat Blue fox is looking at a white cat
4 cat white fox is looking at a white cat
5 cat green fox is looking at a white cat
[root@localhost sed]# sed  -e '3s/dog/cat/2' -e '4s/dog/fish/4' example	
#解释,第3行第二次匹配到的dog替换为cat,第4行第四次匹配到的dog替换为fish
1 dog Yellow fox is looking at a white dog dog dog
2 dog Red fox is looking at a Red dog   dog dog
3 dog Blue fox is looking at a white cat        dog dog
4 dog white fox is looking at a white dog       dog fish
5 dog green fox is looking at a white dog       dog dog
[root@localhost sed]#
y变换

将一系列的字符变换为相应的字符,格式sed 'y/被变换的字符序列/变换的字符序列/' inputfile 被变换的字符序列要和变换的字符序列等长

sed 'y/1234/abcd/' inputfile
# 1变换为a,2变换为b,3变换为为c,依次类推。
[root@localhost sed]# sed   '2y/adg/ADG/' example	#将第2行的所有小写'a''d''g'转换成大写的'A''D''G'
1 dog Yellow fox is looking at a white dog dog dog
2 DoG ReD fox is lookinG At A ReD DoG   DoG DoG
3 dog Blue fox is looking at a white dog        dog dog
4 dog white fox is looking at a white dog       dog dog
5 dog green fox is looking at a white dog       dog dog

常用的就介绍到这里

3、awk