- 简介
- 第一章 - 关于 CD
- 第二章 - 基本命令
- Hack-7 Grep
- Hack-8 Grep 与正则表达式
- Hack-9 Find 命令
- Hak-10 重定向
- Hack-11 Join 命令
- Hack-12 Tr 命令
- Hack-13 Xargs 命令
- Hack-14 Sort 命令
- Hack-15 Uniq 命令
- Hack-16 Cut 命令
- Hack-17 Stat 命令
- Hack-18 Diff 命令
- Hack-19 Ac 命令
- Hack-20 让命令在后台执行
- Hack-21 Sed 替换基础
- Hack-22 Awk 简介
- Hack-23 VIM 基本入门
- Hack-24 Chmod 命令
- Hack-25 Tail -f -f
- Hack-26 Less 命令
- Hack-27 Wget 下载器
- 第三章 - SSH 技巧
- 第四章 - 日期设置
- 第五章 - PS* 介绍
- 第六章 - 压缩和打包
- 第七章 - 历史命令
- 第八章 - 系统任务管理
- 第九章 - 安装软件
- 第十章 - LAMP 套装
- 第十一章 - Bash 脚本
- 第十二章 - 系统性能监控
- 第十三章 - 额外的技巧
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
Hack-8 Grep 与正则表达式
Grep 与正则表达式
其实学过正则表达式之后这些就不算啥事儿了,建议先学一下正则然后再来看。
不过直接看下面的更功利一些,毕竟看完了就可以直接用了。
下面这个是测试文的内容:
➤ cat -n fortune
1 All generalizations are false, including this one.
2 -- Mark Twain
3 Many pages make a thick book, except for pocket Bibles which are on very
4 very thin paper.
5
6 Remark of Dr. Baldwin's concerning upstarts: We don't care to eat toadstools
7 that think they are truffles.
8 -- Mark Twain, "Pudd'nhead Wilson's Calendar"
9
10 Sheriff Chameleotoptor sighed with an air of weary sadness, and then
11 turned to Doppelgutt and said 'The Senator must really have been on a
12 bender this time -- he left a party in Cleveland, Ohio, at 11:30 last
13 night, and they found his car this morning in the smokestack of a British
14 aircraft carrier in the Formosa Straits.'
15 -- Grand Panjandrum's Special Award, 1985 Bulwer-Lytton
匹配句首 '^'
➤ grep -ni '^A' fortune
1:All generalizations are false, including this one.
14:aircraft carrier in the Formosa Straits.'
-n 的意思是显示行号
匹配句尾 '$'
➤ grep -ni '\.$' fortune
1:All generalizations are false, including this one.
4:very thin paper.
7:that think they are truffles.
这里的 '\.$' 是以 . 字符结尾的意思,中间的反斜杠是逃逸字符,起转义的效果
匹配空行 '^$'
➤ grep '^$' fortune -n
5:
9:
任意单个字符 '.'
➤ grep -n '.re' fortune
1:All generalizations are false, including this one.
3:Many pages make a thick book, except for pocket Bibles which are on very
6:Remark of Dr. Baldwin's concerning upstarts: We don't care to eat toadstools
7:that think they are truffles.
11:turned to Doppelgutt and said 'The Senator must really have been on a
\b 的意思是单词分界线,也许是空格,也许是 tab
➤ grep -n '\b.re\b' fortune
1:All generalizations are false, including this one.
3:Many pages make a thick book, except for pocket Bibles which are on very
7:that think they are truffles.
零次或多次重复 '*'
➤ grep -n 'l*' fortune
1:All generalizations are false, including this one.
2: -- Mark Twain
3:Many pages make a thick book, except for pocket Bibles which are on very
4:very thin paper.
5:
6:Remark of Dr. Baldwin's concerning upstarts: We don't care to eat toadstools
7:that think they are truffles.
8: -- Mark Twain, "Pudd'nhead Wilson's Calendar"
9:
10:Sheriff Chameleotoptor sighed with an air of weary sadness, and then
11:turned to Doppelgutt and said 'The Senator must really have been on a
12:bender this time -- he left a party in Cleveland, Ohio, at 11:30 last
13:night, and they found his car this morning in the smokestack of a British
14:aircraft carrier in the Formosa Straits.'
15: -- Grand Panjandrum's Special Award, 1985 Bulwer-Lytton
显示的不是很好,因为没有高亮什么的。
其实这一部分完全可以当做正则来讲,毕竟正则才是核心, grep 只是一个工具,怎么用,还是看你的能力如何了。
曾经看过一个 30 分钟入门正则表达式,个人感觉挺不错的,因为我也是从那里入的门,所以也推荐看这一部分的人也去搜下那个,作者写的够简单易懂了。
这一段写的有点长,但是,当你学会正则的时候你就会发现,这东西真棒! (这是我 BB 的,跟原作者无关哈...)
扩展阅读
本书简介:
- Linux 进阶技巧
- 巧妙的命令组合
- Bash 某些技巧
- 一共一百零一个(包括充数的)
- 最后有个奖励章(额外技巧)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论