RE字符 | 含义 | 举例 |
^word | ^表示行首,待搜索的word字符串在行首 | grep -n '^#' test.c 搜索行首为#开始的那一行 |
word$ | $表示行尾,待搜索的字符串word在行尾 | grep -n '!$' test.c 搜索行尾为!的那一行 |
. | 代表一定存在一个的任意字符,即其位置一定仅有一个字符 | grep -n 'e.e' test.c 搜索的字符串{eve}{eae}{e e}等 |
\ | 转义符,将特殊符号的特殊意义去掉 | grep -n \' test.c 搜索含有单引号的那一行 |
* | *位置前面的一个字符重复零次或者零次以上 | grep -n ‘ess*’test.c 搜索含有{es}{ess}{esss}等字符串 |
\{n,m\} | 连续重复n到m个前一个字符 若为\{n\}则是连续重复n个前一个字符 若为\{n,\}则是连续重复n个以上的前一个字符 | grep -n ‘go\{2,3\}g’ test.c 在g和g之间有2~3个o存在的字符串,即{goog}{gooog} |
[ ] | []里放的字符表示存在其中任何一个即可 | 1、[list] grep -n ‘g[ld]’ test.c 搜索含有{gl}或{gd}的那一行 2、[ch1-ch2] grep -n '[0-9]' test.c 搜索含有任意数字的行 grep -n '[^0-9]' test.c 搜索不含数字的行 |
用法:grep [参数] '搜索字符串' [filename]
参数:
-a 将二进制文件以文本文件的方式搜索数据
-c 计算找到‘搜索字符串’的次数
-i 忽略大小写的不同
-n 顺便输出行号
-v 选出不含‘搜索字符串’的行
举例:
[root@Ca03 ~]# last | grep -v 'root'
reboot system boot 2.6.18-164.11.1. Mon Feb 20 17:02 (9+04:16)
reboot system boot 2.6.18-164.11.1. Mon Feb 6 09:26 (23+11:53)
reboot system boot 2.6.18-164.11.1. Thu Jan 5 11:39 (14+05:07)
reboot system boot 2.6.18-164.11.1. Mon Dec 12 10:54 (3+22:10)
reboot system boot 2.6.27 Tue Dec 6 16:15 (5+18:35)
[root@linux ~] # printf '\x45\n'
E