linux系统三剑客

一、 grep

基础用法

1. 搜索文件中的字符串

📃语法格式:grep “需要过滤筛选的信息” /目标文件路径/要过滤的文件名

[root@hcss-ecs-72ce H100]# cat ./test.txt
hhhhhh
name
[root@hcss-ecs-72ce H100]# grep "name" ./test.txt 
name
[root@hcss-ecs-72ce H100]# 

📢:./ 表示在当前目录下

2. 递归在当前目录及其子目录中搜索字符串

[root@hcss-ecs-72ce home]# grep -r "name" ./
./H100/test.txt:name
./H100/test.t:name

选项和修改器

1. 忽略大小写(-i)

搜索时不区分大小写

[root@hcss-ecs-72ce home]# grep -i  "name" ./H100/test.txt 
name
Name
[root@hcss-ecs-72ce home]#

2. 打印行号(-n)

输出匹配行及其对应的行号

[root@hcss-ecs-72ce home]# grep -n "name" ./H100/test.txt 
1:hhhhhh,my name is paipai
2:name
[root@hcss-ecs-72ce home]# 

3. 只列出文件名(-l)

只输出包含匹配字符的文件名,不显示具体的匹配内容

[root@hcss-ecs-72ce H100]# grep -l  “name” *
[root@hcss-ecs-72ce H100]# grep -l  "name" *
test.t
test.txt
[root@hcss-ecs-72ce H100]#

📢:可以发现第一次输入命令的时候是没有任何反应的,一度以为是命令用的❎,最后排查下来才发现,原来是字符串后面的引号输成了中文的😭~
⚠️:linux中,操作命令是无法识别中文的符号的,大家一定要注意,不要最后一通排查才发现是自己标点符号用发不对这个问题希望各位学习的小伙伴跟我一样多注意。

3. 反向匹配(-v)

输出不包括匹配字符串的行

[root@hcss-ecs-72ce H100]# cat test.log 
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
wow
come here
baby car
bye~
[root@hcss-ecs-72ce H100]# grep -v "name" ./test.log 
hhhhhh
wow
come here
baby car
bye~
[root@hcss-ecs-72ce H100]# 

4. 递归搜索(-r or -R)

搜索指定目录及其子目录下的所有文件

[root@hcss-ecs-72ce H100]# grep -r "name" /home
/home/H101/ok.txt:you name balala
/home/H100/test.log:my name is paipai
/home/H100/test.log:my name is peiqi
/home/H100/test.log:my name is hhhh
[root@hcss-ecs-72ce H100]# grep -R "name" /home
/home/H101/ok.txt:you name balala
/home/H100/test.log:my name is paipai
/home/H100/test.log:my name is peiqi
/home/H100/test.log:my name is hhhh
[root@hcss-ecs-72ce H100]# 

5. 正则表达式(-E)

使用正则表达式进行搜索,比如搜索文件中0-9的字符

[root@hcss-ecs-72ce H100]# cat test.log 
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
wow
come here
baby car
bye~
1
2
3
8
9
[root@hcss-ecs-72ce H100]# grep -E [0-9] ./test.log 
1
2
3
8
9
[root@hcss-ecs-72ce H100]# 

6.计数(-c)

不输出匹配到的字符而是输出匹配到字符的行数

[root@hcss-ecs-72ce H100]# cat test.log 
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
wow
come here
baby car
bye~
[root@hcss-ecs-72ce H100]# grep -c "name" ./test.log 
3
[root@hcss-ecs-72ce H100]# 

7.显示前后文(-B -A -C)

字符含义
-B nbefore,显示匹配行的前n行
-A nafter显示匹配行的前n行
-C ncounts显示匹配行的前n行
[root@hcss-ecs-72ce H100]# grep -B 2 "wow" ./test.log 
my name is peiqi
my name is hhhh
wow
[root@hcss-ecs-72ce H100]# grep -A 2 "wow" ./test.log 
wow
come here
baby car
[root@hcss-ecs-72ce H100]# grep -C 2 "wow" ./test.log 
my name is peiqi
my name is hhhh
wow
come here
baby car
[root@hcss-ecs-72ce H100]# 

⚠️ 注意区分-c 于-C的区别,不能用混了

8. 打印匹配的字节偏移量(-b)

显示匹配字节偏移量
字节偏移量——从文件的起始位置到关注的数据节点的字符数

[root@hcss-ecs-72ce H100]# grep -b "wow" ./test.log 
58:wow
[root@hcss-ecs-72ce H100]# 

💽 隐藏用法
在参数b后面加n,可以看到目标字符上下n行首字母对应的字节偏移量~

[root@hcss-ecs-72ce H100]# grep -b3 "wow" ./test.log 
7-my name is paipai
25-my name is peiqi
42-my name is hhhh
58:wow
62-come here
72-baby car
81-bye~
[root@hcss-ecs-72ce H100]# 

用管道命令与其它命令结合使用

grep常与其它命令结合使用,以对输出的文本进行过滤和搜索

[root@hcss-ecs-72ce H100]# cat ./test.log  |grep "name"
my name is paipai
my name is peiqi
my name is hhhh
[root@hcss-ecs-72ce H100]# 

几个常见的grep用法归纳

1. 直接在某个文件中找对应的字符串(两种实现方式)

方式一:标准实现方式

[root@hcss-ecs-72ce H100]# grep "name" ./test.log 
my name is paipai
my name is peiqi
my name is hhhh

方式二:结合管道进行过滤

[root@hcss-ecs-72ce H100]# cat ./test.log |grep "name" 
my name is paipai
my name is peiqi
my name is hhhh
[root@hcss-ecs-72ce H100]# 

2. 以不区分大小写的方式检查内核消息中的“usb”

[root@hcss-ecs-72ce H100]# dmesg |grep -i "usb"

查询出来的内容太多了,贴个图吧
在这里插入图片描述

3.在对应目录下进行递归搜索

[root@hcss-ecs-72ce H100]# grep -i -r "name" /home/
/home/H101/ok.txt:you name balala
/home/H101/ok.txt:NAME
/home/H100/test.log:my name is paipai
/home/H100/test.log:my name is peiqi
/home/H100/test.log:my name is hhhh
[root@hcss-ecs-72ce H100]# 

4. 递归搜索当前目录下每个“XXXX”出现的行及其行号

[root@hcss-ecs-72ce H100]# grep -n -r "name" /home/
/home/H101/ok.txt:1:you name balala
/home/H100/test.log:2:my name is paipai
/home/H100/test.log:3:my name is peiqi
/home/H100/test.log:4:my name is hhhh
[root@hcss-ecs-72ce H100]# 

二、sed

是一个强大的文本处理工具,修改替换文件内容-擅长对文件中的字符串进行增删改查操作而不需要打开编辑器。下面是整理的一次sed基础命令总结;

1. 🔭 查

根据行号进行查询

  • 查询单行(📌根据sed查询的原理,如果不加参数-n,会把查询的整个过程给全部打印出来)
[root@hcss-ecs-72ce ~]# sed -n '3p' /home/H100/test.log 
my name is peiqi
[root@hcss-ecs-72ce ~]# sed '3p' /home/H100/test.log 
hhhhhh
my name is paipai
my name is peiqi
my name is peiqi
my name is hhhh
wow
come here
baby car
bye~
1
2
3
8
9
[root@hcss-ecs-72ce ~]# 
  • 查询不连续的多行中间用封号进行分割
[root@hcss-ecs-72ce ~]# sed -n '1p;4p' /home/H100/test.log 
hhhhhh
my name is hhhh
[root@hcss-ecs-72ce ~]# ```

- **查询连续的多行** 中间用逗号进行分割,注意要少个p😁

```rust
[root@hcss-ecs-72ce ~]# sed -n '1,4p' /home/H100/test.log 
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
[root@hcss-ecs-72ce ~]# 

根据文件内容进行查询

  • 查询单个内容
    📌可以看到,即使是查询单个内容,也会把整行内容给打印出来
[root@hcss-ecs-72ce ~]# sed -n '/paipai/p' /home/H100/test.log 
my name is paipai
[root@hcss-ecs-72ce ~]# 
  • 查询多个连续内容
[root@hcss-ecs-72ce ~]# sed -n '/paipai/,/car/p' /home/H100/test.log 
my name is paipai
my name is peiqi
my name is hhhh
wow
come here
baby car
[root@hcss-ecs-72ce ~]# 
  • 查询多个不连续内容
    📌查询不连续内容的时候每一个查询的内容后面都需要加个p😁
[root@hcss-ecs-72ce ~]# sed -n '/paipai/;/car/p' /home/H100/test.log 
sed:-e 表达式 #1,字符 9:未知的命令:“;[root@hcss-ecs-72ce ~]# sed -n '/paipai/p;/car/p' /home/H100/test.log 
my name is paipai
baby car
[root@hcss-ecs-72ce ~]# 

2. 替换(-i)

📌语法格式
sed -i “s#需要替换的内容#替换后的内容#g” 目标操作文件路径

[root@hcss-ecs-72ce ~]# cat /home/H100/test.log 
hhhhhh
my name is paipai
my name is peiqi
my name is hhhh
[root@hcss-ecs-72ce ~]# sed -i "s#name#hello#g" /home/H100/test.log 
[root@hcss-ecs-72ce ~]# cat /home/H100/test.log 
hhhhhh
my hello is paipai
my hello is peiqi
my hello is hhhh

3.删除(d)

  • 删除单行内容
    ⚠️执行删除命令时,如果要彻底在文件中删除,需要再执行命令前面加上一个参数-i 否则只是在打印结果中删除,实际上并没有删除。
[root@hcss-ecs-72ce ~]# sed '11d' /home/H100/test.log 
hhhhhh
my hello is paipai
my hello is hhhh
wow
come here
baby car
bye~
1
2
3
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	hhhhhh
     2	my hello is paipai
     3	my hello is hhhh
     4	wow
     5	come here
     6	baby car
     7	bye~
     8	1
     9	2
    10	3
    11	8
[root@hcss-ecs-72ce ~]# sed -i '11d' /home/H100/test.log 
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	hhhhhh
     2	my hello is paipai
     3	my hello is hhhh
     4	wow
     5	come here
     6	baby car
     7	bye~
     8	1
     9	2
    10	3
[root@hcss-ecs-72ce ~]# 
  • 删除多行不连续信息
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	hhhhhh
     2	my hello is paipai
     3	my hello is hhhh
     4	wow
     5	come here
     6	baby car
     7	bye~
     8	1
     9	2
    10	3
[root@hcss-ecs-72ce ~]# sed -i '1d;10d' /home/H100/test.log 
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	my hello is paipai
     2	my hello is hhhh
     3	wow
     4	come here
     5	baby car
     6	bye~
     7	1
     8	2
[root@hcss-ecs-72ce ~]# 
  • 删除多行连续信息
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	my hello is paipai
     2	my hello is hhhh
     3	wow
     4	come here
     5	baby car
     6	bye~
     7	1
     8	2
[root@hcss-ecs-72ce ~]# sed -i '6,8d' /home/H100/test.log 
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	my hello is paipai
     2	my hello is hhhh
     3	wow
     4	come here
     5	baby car
[root@hcss-ecs-72ce ~]# 

增 (插入和追加)

⚠️有关参数,前面🙅🏻‍♀️加-,直接跟需要操作的行

参数添加位置
aafter, 在指定行后面添加一行新的内容
iinitial ,在指定行后面添加一行新的内容
  • 在指定行后增加新的一行内容如果需要写入到内存中得加参数 -i
[root@hcss-ecs-72ce ~]# sed '1a here is a new line of text' /home/H100/test.log 
my hello is paipai
here is a new line of text
my hello is hhhh
wow
come here
baby car
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	my hello is paipai
     2	my hello is hhhh
     3	wow
     4	come here
     5	baby car
[root@hcss-ecs-72ce ~]# sed -i '1a here is a new line of text' /home/H100/test.log 
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	my hello is paipai
     2	here is a new line of text
     3	my hello is hhhh
     4	wow
     5	come here
     6	baby car
[root@hcss-ecs-72ce ~]# 
  • 在指定行前增加一行内容,直接写入磁盘内存中
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	my hello is paipai
     2	here is a new line of text
     3	my hello is hhhh
     4	wow
     5	come here
     6	baby car
[root@hcss-ecs-72ce ~]# sed -i '4i we are friends' /home/H100/test.log 
[root@hcss-ecs-72ce ~]# cat -n /home/H100/test.log 
     1	my hello is paipai
     2	here is a new line of text
     3	my hello is hhhh
     4	we are friends
     5	wow
     6	come here
     7	baby car
[root@hcss-ecs-72ce ~]# 

三、awk

awk支持包括数组在内的复杂数据结构,以及自定义函数,以及可以用来编写复杂多少数据处理程序。
awk是一种编程语言,下面的只是基础特性。它还支持更多的先进技术,如循环、条件语句、用户自定义函数等。后续再视情况看是否要深入学习了解。

查询信息

利用awk查询出文件的内容

[root@hcss-ecs-72ce ~]# awk '{print}' /home/paipai/awk_practice.txt 
00	ben	18	60
01	ken	23	66
02	pen	14	30
03	kei	43	70

按照行号进行查询

  • 查询单行信息
[root@hcss-ecs-72ce ~]# #查询单行信息
[root@hcss-ecs-72ce ~]# awk 'NR==2' /home/paipai/awk_practice.txt 
00	ben	18	60
  • 查询不连续的多行信息
[root@hcss-ecs-72ce ~]# #查询不连续的多行
[root@hcss-ecs-72ce ~]# awk 'NR==1;NR==4' /home/paipai/awk_practice.txt 
编号	姓名	年龄	体重
02	pen	14	30
  • 查询连续的多行信息
[root@hcss-ecs-72ce ~]# #查询连续多行
[root@hcss-ecs-72ce ~]# awk 'NR==1,NR==5' /home/paipai/awk_practice.txt 
编号	姓名	年龄	体重
00	ben	18	60
01	ken	23	66
02	pen	14	30
03	kei	43	70
[root@hcss-ecs-72ce ~]# 

替换信息(改)

先查出行信息,再指定要修改的信息,再指定展示列
📌 整体框架

 awk '/xxx/{gsub(/需要修改的信息/,"修改成什么信息","$n将那列的信息进行修改");print$n}'  要操作的文件

🔆命令比较复杂可以拆成两部分来看
主体框架:

awk '/要查询的字符信息/{print $需要输出的列号}'  要操作的文件

修改需要输出的内容

gsub(/需要修改的信息/,"要修改成的信息",$要修改的列号);
[root@hcss-ecs-72ce ~]# awk '/ben/{gsub(/:/,"%",$NF);print}' /home/paipai/awk_practice.txt 
ben%18%60%yunnan
[root@hcss-ecs-72ce ~]# cat /home/paipai/awk_practice.txt 
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
[root@hcss-ecs-72ce ~]# 

排除信息

比如:将空行信息,和#开头的信息进行排除

[root@hcss-ecs-72ce ~]#  awk "{print}" /home/paipai/awk_practice.txt 
编号	姓名	年龄	体重
00	ben	18	60
01	ken	23	66
02	pen	14	30
03	kei	43	70

#05	lily	19	50
06	baby	23	69

08	zooc	32	45
[root@hcss-ecs-72ce ~]#  awk '!/^#|^$/' /home/paipai/awk_practice.txt 
编号	姓名	年龄	体重
00	ben	18	60
01	ken	23	66
02	pen	14	30
03	kei	43	70
06	baby	23	69
08	zooc	32	45
[root@hcss-ecs-72ce ~]# 

三剑客共通点

1.都能对文件进行过滤筛选

  • sed执行过来筛选操作时需要加上参数-n(sed执行原理导致的结果)
  • sed、awk目标字符要放在/ xxxx/中。且sed在目标字符执行后要加上一个参数p。而awk不能加p,否则反而会无法执行出结果
☀️ grep
[root@hcss-ecs-72ce ~]# grep "dew" /home/paipai/awk_practice.txt 
dew:19:65:beijing
[root@hcss-ecs-72ce ~]# 
☀️ sed
[root@hcss-ecs-72ce ~]# sed -n "/dew/p" /home/paipai/awk_practice.txt 
dew:19:65:beijing
☀️ awk
[root@hcss-ecs-72ce ~]# awk "/dew/" /home/paipai/awk_practice.txt 
dew:19:65:beijing

排除空行展示

一般排除空行展示时。需要搭配正则符号一起使用。

☀️ grep

grep -E 与egrep 作用相同,是为了使grep可以识别正则表示
在grep中 -v表示取反;
两种写法分别如下:

[root@hcss-ecs-72ce ~]# cat /home/paipai/awk_practice.txt 
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing

pai:15:98:hangzhou

kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]# egrep  -v "^$" /home/paipai/awk_practice.txt 
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]# grep -E  -v "^$" /home/paipai/awk_practice.txt 
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
☀️ sed

注意:使用sed命令进行过滤查询时,一定要在sed后面加参数-n。否则根据sed查询发原理,会把查询的整个过程进行打印(也就是会打印原文)
sed查询🙆🏻‍♀️施一公单引号

[root@hcss-ecs-72ce ~]# cat /home/paipai/awk_practice.txt 
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing

pai:15:98:hangzhou

kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]# sed -n '/^$/!p' /home/paipai/awk_practice.txt 
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]# 
☀️ awk

awk跟 sed不太一样,表示取反的!需要加在对应字符串前面,而且不能加p,这也是一直反复强调的,加p反而没有输出结果;
按行查询出来也可以指定到具体的列。也可以替换信息

[root@hcss-ecs-72ce ~]# awk '!/^$/' /home/paipai/awk_practice.txt 
ben:18:60:yunnan
kai:17:80:shandong
dew:19:65:beijing
pai:15:98:hangzhou
kel:12:35:jiangsu
[root@hcss-ecs-72ce ~]# awk '/^$/!' /home/paipai/awk_practice.txt 
awk: cmd. line:2: /^$/!
awk: cmd. line:2:      ^ unexpected newline or end of string
[root@hcss-ecs-72ce ~]# 

使用三剑客的注意事项

1. 操作目标字符

目标字符用双引号跟单引号都可以吗

符号区别
grep双引号、双引号都可以
sed部分指令双引号、双引号都可以,取反命令必须用单引号
awk单引号

📢:实在记不住,那就所有的场景都用单引号

2. 输出字符加p

sed命令需要再目标输出字符’/xxxxxx/p’,需要在引号里面加个p,grep与awk不需要
具体的原理如下:

符号执行原理
sed如果不加p,查询语句会报错。且默认会把整个查询过程打印出来,所以需要加上参数 -n取消查询展示。
awk加p,反而导致默认会把整个查询过程打印出来。
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值