|:管道符
# 作用:将管道符左边命令的标准输出,交给管道符右边命令的标准输入来处理
举例:
[root@localhost ~]# head /work6/777.txt | tail -3
www.sina.com
www.baidu.com
www.123.com
# 命令:
grep (过滤输出内容)
^:以...开头
$:以...结尾
*:匹配*前面的内容至少0次或多次
.:匹配任意字符
## 选项
-n:打印行号 number
-A:after 查看过滤内容的后N行
-B:before 查看过滤内容的前N行
-C:center 查看过滤内容的前、后N行
-v:取反
-E:可以过滤多个结果
-o:只打印出过滤内容
-i:不区分大小写
举例:
[root@localhost ~]# cat /work6/hxs
一、《浣溪沙.一曲新词酒一杯》——宋.晏殊
一曲新词酒一杯,
I compose a new song and drink a cup of wine
去年天气旧亭台。
In the bower of last year when weather is as fine.
夕阳西下几时回。
When will you come back like the sun on the decline?
无可奈何花落去,
Deeply I sigh for the fallen flowers in vain.
似曾相识燕归来。
Vaguely I seem to know the swallows come again.
小园香径独徘徊。
Loitering on the garden path, I alone remain.
# ^
[root@localhost ~]# grep '^一' /work6/hxs
一、《浣溪沙.一曲新词酒一杯》——宋.晏殊
一曲新词酒一杯,
[root@localhost ~]# grep '^W' /work6/hxs
When will you come back like the sun on the decline?
# $
[root@localhost ~]# grep 'e$' /work6/hxs
I compose a new song and drink a cup of wine
[root@localhost ~]# grep '.$' /work6/hxs
一、《浣溪沙.一曲新词酒一杯》——宋.晏殊
一曲新词酒一杯,
I compose a new song and drink a cup of wine
去年天气旧亭台。
In the bower of last year when weather is as fine.
夕阳西下几时回。
When will you come back like the sun on the decline?
无可奈何花落去,
Deeply I sigh for the fallen flowers in vain.
似曾相识燕归来。
Vaguely I seem to know the swallows come again.
小园香径独徘徊。
Loitering on the garden path, I alone remain.
[root@localhost ~]# grep '杯,$' /work6/hxs
一曲新词酒一杯,
# 例3:反义符让$失去原本的意义,只是过滤含$的内容
[root@localhost ~]# grep '\$' /work6/hxs
无可奈何$花落去,
Vaguely I seem to know$ the swallows come again.
# . 和 *
举例:'f.' 和 'f'的区别 少了一个27行的内容 这就是 f. 是要f匹配一个字符的才会被过滤下来并且f和匹配的字符都高亮显示
[root@localhost ~]# grep -n 'f.' /work6/hxs5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
17:Deeply I sigh for the fallen flowers in vain.
[root@localhost ~]# grep -n 'f' /work6/hxs5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
17:Deeply I sigh for the fallen flowers in vain.
27:宋.晏殊 f
含f的行出现,不含f的也会出现,只不过含f的会高亮显示,不同于f.和 f
[root@localhost ~]# grep -n 'f*' /work6/hxs1:一、《浣溪沙.一曲新词酒一杯》——宋.晏殊
2:
3:一曲新词酒一杯,
4:
5:I compose a new song and drink a cup of wine
6:
7:去年天气旧亭台。
8:
9:In the bower of last year when weather is as fine.
10:
11:夕阳西下几时回。
12:
13:When will you come back like the sun on the decline?
14:
15:无可奈何$花落去,
16:
17:Deeply I sigh for the fallen flowers in vain.
18:
19:似曾相识燕归来。
20:
21:Vaguely I seem to know$ the swallows come again.
22:
23:小园香径独徘徊。
24:
25:oLoitering on the garden path, I alone remain.
26:
27:宋.晏殊 f
[root@localhost ~]# grep -n '.*' /work6/hxs
这样都会高亮显示
# -A -B -C
举例:
[root@localhost ~]# grep -n '回' /work6/hxs11:夕阳西下几时回。
-A
[root@localhost ~]# grep -A 3 -n '回' /work6/hxs11:夕阳西下几时回。
12-
13-When will you come back like the sun on the decline?
14-
-B
[root@localhost ~]# grep -B 2 -n '回' /work6/hxs9-In the bower of last year when weather is as fine.
10-
11:夕阳西下几时回。
-C
[root@localhost ~]# grep -A 4 -B 2 -n '回' /work6/hxs9-In the bower of last year when weather is as fine.
10-
11:夕阳西下几时回。
12-
13-When will you come back like the sun on the decline?
14-
15-无可奈何$花落去,
-A 4 -B 2
[root@localhost ~]# grep -A 4 -B 2 -n '回' /work6/hxs9-In the bower of last year when weather is as fine.
10-
11:夕阳西下几时回。
12-
13-When will you come back like the sun on the decline?
14-
15-无可奈何$花落去,
# -v
[root@localhost ~]# grep -v '回' /work6/hxs
一、《浣溪沙.一曲新词酒一杯》——宋.晏殊
一曲新词酒一杯,
I compose a new song and drink a cup of wine
去年天气旧亭台。
In the bower of last year when weather is as fine.
When will you come back like the sun on the decline?
无可奈何$花落去,
Deeply I sigh for the fallen flowers in vain.
似曾相识燕归来。
Vaguely I seem to know$ the swallows come again.
小园香径独徘徊。
oLoitering on the garden path, I alone remain.
宋.晏殊 f
# -E## 在grep命令中'|' 是 “或” 的意思
[root@localhost ~]# grep -E 'a|回' /work6/hxs
I compose a new song and drink a cup of wine
In the bower of last year when weather is as fine.
夕阳西下几时回。
When will you come back like the sun on the decline?
Deeply I sigh for the fallen flowers in vain.
Vaguely I seem to know$ the swallows come again.
oLoitering on the garden path, I alone remain.
[root@localhost ~]# grep -E 'a|回|a|来' /work6/hxs
I compose a new song and drink a cup of wine
In the bower of last year when weather is as fine.
夕阳西下几时回。
When will you come back like the sun on the decline?
Deeply I sigh for the fallen flowers in vain.
似曾相识燕归来。
Vaguely I seem to know$ the swallows come again.
oLoitering on the garden path, I alone remain.
# -o
[root@localhost ~]# grep -o '来' /work6/hxs
来
来
来
来
来
[root@localhost ~]# grep '来' /work6/hxs
似曾相识燕归来。
来一个
来两个
来来
# -i
[root@localhost ~]# grep -i 'w' /work6/hxs
I compose a new song and drink a cup of wine
In the bower of last year when weather is as fine.
When will you come back like the sun on the decline?
Deeply I sigh for the fallen flowers in vain.
Vaguely I seem to know$ the swallows come again.
[root@localhost ~]# grep 'W' /work6/hxs
When will you come back like the sun on the decline?
# [a-zA-Z] []是一个范围的意思
[root@localhost ~]# grep -n '[a-z]' /work6/hxs5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.
27:宋.晏殊 f
[root@localhost ~]# grep -n -E 'a|z' /work6/hxs5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.
[root@localhost ~]# grep -n '[A-Z]' /work6/hxs5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.
[root@localhost ~]# grep -n '[a-zA-Z]' /work6/hxs5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.
27:宋.晏殊 f
[root@localhost ~]# grep -n -E '[a-c]' /work6/hxs5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.
[root@localhost ~]# grep -n -E '[a-zA-Z]' /work6/hxs5:I compose a new song and drink a cup of wine
9:In the bower of last year when weather is as fine.
13:When will you come back like the sun on the decline?
17:Deeply I sigh for the fallen flowers in vain.
21:Vaguely I seem to know$ the swallows come again.
25:oLoitering on the garden path, I alone remain.
[root@localhost ~]# grep '^[a-zA-Z]' /work6/hxs
I compose a new song and drink a cup of wine
In the bower of last year when weather is as fine.
When will you come back like the sun on the decline?
Deeply I sigh for the fallen flowers in vain.
Vaguely I seem to know$ the swallows come again.
oLoitering on the garden path, I alone remain.
[root@localhost ~]# grep '^[a-z]' /work6/hxs
oLoitering on the garden path, I alone remain.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?