查找文本
以grep程序查找文本,传统上有三种程序:
1.grep:使用基本正则表达式(Basic Regular Expression,BRE)
2.egrep:扩展grep使用扩展正则表达式(Extended Regular Expression),不过会消耗更多的资源。
3.fgrep:快速grep匹配固定字符串而非正则表达式,grep与egrep只能匹配单个正则表达式,而fgrep使用不同的算法,却能匹配多个字符串。
简单的grep
[root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename
选项与参数:
-a :将 binary 文件以 text 文件的方式搜寻数据
-c :计算找到 '搜寻字符串' 的次数
-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!
--color=auto :可以将找到的关键词部分加上颜色的显示喔!
练习
将/etc/passwd,有出现 root 的行取出来
[root@izwz9b4wamau0tb8da491vz ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@izwz9b4wamau0tb8da491vz ~]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号
[root@izwz9b4wamau0tb8da491vz ~]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
将/etc/passwd,将没有出现 root 的行取出来
grep -v root /etc/passwd //用的少
由于之前没有系统的接触过Linux相关命令,所以看到自己不懂就搬过来了,有点乱,但希望能学得更好
内容补充 dmesg 命令的使用范例
‘dmesg’命令设备故障的诊断是非常重要的。在‘dmesg’命令的帮助下进行硬件的连接或断开连接操作时,我们可以看到硬件的检测或者断开连接的信息。‘dmesg’命令在多数基于Linux和Unix的操作系统中都可以使用。
dmesg [options…]
1. 列出加载到内核中的所有驱动
我们可以使用如‘more’。 ‘tail’, ‘less ’或者‘grep’文字处理工具来处理‘dmesg’命令的输出。由于dmesg日志的输出不适合在一页中完全显示,因此我们使用管道(pipe)将其输出送到more或者less命令单页显示。
[root@tecmint.com ~]# dmesg | more
[root@tecmint.com ~]# dmesg | less
用 dmesg 列出核心信息,再以 grep 找出内含 brk 那行,要将捉到的关键字显色,且加上行号来表示:
[root@izwz9b4wamau0tb8da491vz ~]# dmesg | grep -n --color=auto 'BRK'
39:[ 0.000000] BRK [0x01f9e000, 0x01f9efff] PGTABLE
40:[ 0.000000] BRK [0x01f9f000, 0x01f9ffff] PGTABLE
41:[ 0.000000] BRK [0x01fa0000, 0x01fa0fff] PGTABLE
42:[ 0.000000] BRK [0x01fa1000, 0x01fa1fff] PGTABLE
43:[ 0.000000] BRK [0x01fa2000, 0x01fa2fff] PGTABLE
在关键字的显示方面,grep 可以使用 –color=auto 来将关键字部分使用颜色显示。 这可是个很不错的功能啊!但是如果每次使用 grep 都得要自行加上 –color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你可以在 ~/.bashrc 内加上这行:『alias grep=’grep –color=auto’』再以『 source ~/.bashrc 』来立即生效即可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦(一般系统都自带)
grep与正规表达式
结合实例来练习,使用下面的文本来练习(regular_express.txt):
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.^M
GNU is free air not free beer.^M
Her hair is very beauty.^M
I can't finish the test.^M
Oh! The soup taste good.^M
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.^M
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird
字符类
字符类的搜索:如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 ‘t?st’ 存在~这个时候,我可以这样来搜寻:
[root@izwz9b4wamau0tb8da491vz shell]# grep -n 't[ea]st' regular_express.txt
8:I can't finish the test.^M
9:Oh! The soup taste good.^M
其实 [] 里面不论有几个字节,他都谨代表某『一个』字节, 所以,上面的例子说明了,我需要的字串是『tast』或『test』两个字串而已!
字符类的反向选择 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下
[root@izwz9b4wamau0tb8da491vz shell]# grep -n '[^g]oo' regular_express.txt
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!
字符类的连续:再来,假设我 oo 前面不想要有小写字节,所以,我可以这样写 [^abcd….z]oo , 但是这样似乎不怎么方便,由於小写字节的 ASCII 上编码的顺序是连续的, 因此,我们可以将之简化为底下这样:
[root@www ~]# grep -n '[^a-z]oo' regular_express.txt
3:Football game is not use feet only.
也就是说,当我们在一组集合字节中,如果该字节组是连续的,例如大写英文/小写英文/数字等等, 就可以使用[a-z],[A-Z],[0-9]等方式来书写,那么如果我们的要求字串是数字与英文呢? 呵呵!就将他全部写在一起,变成:[a-zA-Z0-9]。
我们要取得有数字的那一行,就这样:
[root@www ~]# grep -n '[0-9]' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
行首与行尾字节 ^ $
行首字符:如果我想要让 the 只在行首列出呢? 这个时候就得要使用定位字节了!我们可以这样做:
[root@www ~]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start.
如果我不想要开头是英文字母,则可以是这样:
[root@izwz9b4wamau0tb8da491vz shell]# grep -n "^[^a-zA-Z]" regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird
任意一个字节 . 与重复字节 *
这两个符号在正则表达式的意义如下:
假设我需要找出 g??d 的字串,亦即共有四个字节, 起头是 g 而结束是 d ,我可以这样做:
[root@izwz9b4wamau0tb8da491vz shell]# grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.^M
16:The world <Happy> is the same with "glad".
因为强调 g 与 d 之间一定要存在两个字节,因此,第 13 行的 god 与第 14 行的 gd 就不会被列出来啦!
当我们需要『至少两个 o 以上的字串』时,就需要 ooo* ,亦即是:
[root@izwz9b4wamau0tb8da491vz shell]# grep -n 'ooo*' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.^M
18:google is the best tools for search keyword.
19:goooooogle yes!
限定连续 RE 字符范围 {}
我们可以利用 . 与 RE 字符及 * 来配置 0 个到无限多个重复字节, 那如果我想要限制一个范围区间内的重复字节数呢?
举例来说,我想要找出两个到五个 o 的连续字串,该如何作?这时候就得要使用到限定范围的字符 {} 了。 但因为 { 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用字符 \ 来让他失去特殊意义才行。 至於 {} 的语法是这样的,假设我要找到两个 o 的字串,可以是:
[root@izwz9b4wamau0tb8da491vz shell]# grep -n 'o\{2\}' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.^M
18:google is the best tools for search keyword.
19:goooooogle yes!
假设我们要找出 g 后面接 2 到 5 个 o ,然后再接一个 g 的字串,他会是这样:
[root@izwz9b4wamau0tb8da491vz shell]# grep -n 'go\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.
参考博客:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html