grep\sed的简单用法

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操作对象是文本中的一行。

请添加图片描述

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命令定位文本的方法
选项含义
xx指定行号
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. grep grep 的基本语法为: ``` grep [OPTION]... PATTERN [FILE]... ``` 其中,`PATTERN` 是要搜索的字符串或正则表达式,`FILE` 是要搜索的文件名。如果不指定 `FILE`,则默认从标准输入读取数据。 常用的选项包括: - `-i`:忽略大小写。 - `-v`:反向匹配,输出不包含 `PATTERN` 的行。 - `-n`:输出匹配行的行号。 - `-r`:递归搜索一个目录下的所有文件。 - `-E`:使用扩展正则表达式。 - `-w`:匹配整个单词,而不是子字符串。 例如,要在文件 `file.txt` 中搜索所有包含 `hello` 的行,可以使用: ``` grep hello file.txt ``` 2. sed sed 的基本语法为: ``` sed [OPTION]... {script-only-if-no-other-script} [input-file]... ``` 其中,`script` 是要执行的编辑命令,支持多个命令以分号分隔。如果不指定 `input-file`,则默认从标准输入读取数据。 常用的命令包括: - `s/PATTERN/REPLACEMENT/g`:替换文本中的 `PATTERN` 为 `REPLACEMENT`,`g` 表示全局替换。 - `d`:删除匹配行。 - `p`:输出匹配行。 - `n`:读入下一行,替换模式空间中的内容,并打印输出。 例如,要将文件 `file.txt` 中的所有 `hello` 替换为 `world`,可以使用: ``` sed 's/hello/world/g' file.txt ``` 3. awk awk 的基本语法为: ``` awk [OPTIONS] 'pattern {action}' file ``` 其中,`pattern` 是要匹配的条件,`action` 是要执行的操作。如果不指定 `file`,则默认从标准输入读取数据。 常用的命令包括: - `$0`:表示整个文本行。 - `$1`:表示第一个字段,以空格为分隔符。 - `-F`:指定字段分隔符。 - `BEGIN`:在处理文件之前执行的命令。 - `END`:在处理文件之后执行的命令。 例如,要在文件 `file.txt` 中输出第二列的内容,可以使用: ``` awk '{print $2}' file.txt ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值