Shell V grep sed awk

管道

Shell中 提供了“|” 的管道实现,来帮助用户更快捷的处理过程流

当运行 a | b | c 后:

1
2
3
4
5
6
7
Parent Process
├── fork() --> Child 1 runs `a`
│ stdout --> pipe1[1]
├── fork() --> Child 2 runs `b`
│ stdin <-- pipe1[0], stdout --> pipe2[1]
├── fork() --> Child 3 runs `c`
stdin <-- pipe2[0]

当a命令在向b进行输出后,b就已经开始执行了。

通过管道的连接,可以让我们更加灵活的获取目标值

grep

grep 支持全局的文本查询和检索

grep 支持定义搜索内容 + 文件进行筛选

1
grep "partten"  file

也可以通过其他命令得到stdout 使用管道进行检索

1
cat file | grep "partten"

常用的命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -i  忽略查询的大小写
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | grep -i a
aAbBcCdDeEfF
# -o 只返回匹配规则的值
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | grep -o a
a
# -v 反向筛选
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | grep -v a
gG hH iI jJ kK lL
mM nN
# 基本正则匹配
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | grep -G [A-Z]
aA bB cC dD eE fF
gG hH iI jJ kK lL
mM nN
# 扩展正则匹配
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | grep -E [a-zA-Z]{2}
aA bB cC dD eE fF
gG hH iI jJ kK lL
mM nN

sed

sed 是一个文本替换工具

sed也可以设置规则 替换文件内容

1
2
3
4
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# sed "s/a/A/" hello.txt
AA bB cC dD eE fF
gG hH iI jJ kK lL
mM nN

也可以接收stdout,修改流中的数据

1
2
3
4
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt  | sed  's/a/A/'
AA bB cC dD eE fF
gG hH iI jJ kK lL
mM nN

注意直接使用 sed 进行替换并不会修改原文本内容,需要修改文本内容需要加”-i”

1
2
3
4
5
'[num]s/old/new/[g]'  进行全局/某一行替换 
'[num]d' 不显示/删除某一行
'[num][i|a]\[text]' 在某行前/后 追加文本
'-n "[num],[num]p"' 打印行区间文本
'-e' 多个执行文本规则
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#	sed 's/old/new/'	只替换每行第一个匹配
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed 's/a/A/'
AA bB cC dD eE fF
AA hH iI jJ kK lL
mM nN
# sed 's/old/new/g' 替换每行所有匹配
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed 's/b/B/g'
aA BB cC dD eE fF
aA hH iI jJ kK lL
mM nN BB BBB
# sed '2s/old/new/' 只替换第3行的匹配项
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed '3s/b/B/g'
aA bB cC dD eE fF
aA hH iI jJ kK lL
mM nN BB BBB
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed '3s/b/B/'
aA bB cC dD eE fF
aA hH iI jJ kK lL
mM nN BB bbb
# sed '2d' 删除第2行
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed '2d'
aA bB cC dD eE fF
mM nN bB bbb
# sed '/^$/d' 删除所有空行
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt
aA bB cC dD eE fF

aA hH iI jJ kK lL
mM nN bB bbb

[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed '/^$/d'
aA bB cC dD eE fF
aA hH iI jJ kK lL
mM nN bB bbb
# sed -n '5p' 只输出第3行
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed -n 3p
aA hH iI jJ kK lL
# sed -n '5,10p' 输出第1到3行
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed -n '1,3p'
aA bB cC dD eE fF

aA hH iI jJ kK lL
# sed '1i\This is a new line' 在第1行前插入
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed '1i\hello'
hello
aA bB cC dD eE fF

aA hH iI jJ kK lL
mM nN bB bbb
# sed '1a\This is after line 3' 在第1行后追加
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed '1a\hello'
aA bB cC dD eE fF
hello

aA hH iI jJ kK lL
mM nN bB bbb
# sed -e '1d' -e 's/foo/bar/g' 删除第1行并替换所有 aA为 zZ
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# cat hello.txt | sed -e '1d' -e 's/aA/zZ/g'

zZ hH iI jJ kK lL
mM nN bB bbb

awk

awk是一种 用于文本处理的脚本语言,它接收每一行输入,并按规则匹配,最终打印

1
awk 'pattern { action }'  filename 

或者

1
cat filename | awk 'pattern {action}'

变量 :

1
2
3
4
5
6
$0	当前行内容
$[num] 第num列(字段)
NF 当前行的字段数量
NR 当前是第几行
FS 输入字段分隔符(默认是空格)
OFS 输出字段分隔符(默认也是空格)
1
2
3
4
5
6
# awk 可以分为多段执行
awk 'BEGIN {} {} END {}'
# BEGIN 开始时执行一次的代码段, END 结束时执行一次的代码段
# 行信息放在外面,列信息在{}中操作
# 'BEGIN','END' 只能进行 分隔符定义,变量赋值
# 在中间{} 外面可以设置 NR== 定义行号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
打印第九列
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# ls -l | awk '{print $9}'
a
ate
b
c
d
e
f
g
h
hello.txt
i
j
k
l
m
n
打印第一行
[root@iZbp19tqlmjz1dmnm8w43uZ hello]# ls -l | awk 'NR==2{print }'
-rw-r--r-- 1 root root 0 Apr 10 13:58 a

混合查询

查询一下redis的安装时间

1
2
3
[root@iZbp19tqlmjz1dmnm8w43uZ ~]# ps -aux | grep -v "grep" | grep redis | sed 's/redis/myredis/' | grep -v "sed" | awk '{print $9}'
Apr08


Shell V grep sed awk
http://gadoid.io/2025/04/09/Shell-V-grep-sed-awk/
作者
Codfish
发布于
2025年4月9日
许可协议