正则表达式
Regular Expressions is nothing but a pattern to match for each input line. A pattern is a sequence of characters. Following all are examples of pattern
^w1
w1|w2
[^ ]
使用正则表达式
/etc/passwd中检索vivek
grep vivek /etc/passwd
示例输出
vivek:x:1000:1000:Vivek Gite,:/home/vivek:/bin/bash
vivekgite:x:1001:1001::/home/vivekgite:/bin/sh
gitevivek:x:1002:1002::/home/gitevivek:/bin/sh
忽略大小写
grep -i -w vivek /etc/passwd
检索vivek或raj
grep -E -i -w ‘vivek|raj’ /etc/passwd
限定
通过^或者$来检索以特定字符开始或者结束的字串,比如:检索以vivek开始的字串写法如下:
grep ^vivek /etc/passwd
示例输出
vivek:x:1000:1000:Vivek Gite,:/home/vivek:/bin/bash
vivekgite:x:1001:1001::/home/vivekgite:/bin/sh
通过参数关键字-w实现完全匹配
grep -w ^vivek /etc/passwd
以foo结尾
grep ‘foo$’ filename
仅包含foo
grep ‘^foo$’ filename
下面的命令可以检索空行
grep ‘^$’ filename
字符组、集
grep ‘[vV]ivek’ filename
grep ‘[vV][iI][Vv][Ee][kK]’ filename
grep -w ‘[vV]ivek[0-9]’ filename
grep ‘foo[0-9][0-9]’ filename
grep ‘[A-Za-z]’ filename
grep [wn] filename
中括号表达式
- [:alnum:] – 字母数字
- [:alpha:] – 英文字符
- [:blank:] – 空字符: space and tab.
- [:digit:] – 阿拉伯数字: ‘0 1 2 3 4 5 6 7 8 9’.
- [:lower:] – 小写字符: ‘a b c d e f g h i j k l m n o p q r s t u v w x y z’.
- [:space:] – 空格字符: tab,newline,vertical tab,form Feed,carriage return,and space.
- [:upper:] – 大写字符: ‘A B C D E F G H I J K L M N O P Q R S T U V W X Y Z’.
grep ‘[:upper:]’ filename
通配符
.匹配单个字符
grep '\<b.t\>' filename
- \< 匹配词首空串
- > 匹配词尾空串
检索仅包含两个字母的字串
grep ‘^..$’ filename
检索以.和数字开头的字串
grep ‘^.[0-9]’ filename
转义.号
检索ip地址 192.168.1.254的命令无效的,
grep ‘192.168.1.254’ /etc/hosts
三个.号均需要转义
grep ‘192.168.1.254’ /etc/hosts
检索匹配通用的IP地址
egrep ‘[[:digit:]]{1,3}.[[:digit:]]{1,3}’ filename
检索包含破折号的字符
使用关键字-e,比如检索-test-
grep -e ‘–test–’ filename
使用OR选项
grep -E ‘word1|word2’ filename
egrep ‘word1|word2’ filename
或者
grep 'word1\|word2' filename
使用AND选项
grep 'word1' filename | grep 'word2'
字符重复次数
{N}
{N,}
{min,max}
egrep "v{2}" filename
egrep 'co{1,2}l' filename
egrep 'c{3,}' filename
检索通用手机号码,格式: 91-1234567890
grep “[[:digit:]]{2}[ -]\?[[:digit:]]{10}” filename
突出显示检索结果
grep --color regex filename
grep -o regex filename
正则表达式
Regex operator | Meaning |
---|---|
. | Matches any single character. |
? | The preceding item is optional and will be matched,at most,once. |
* | The preceding item will be matched zero or more times. |
+ | The preceding item will be matched one or more times. |
{N} | The preceding item is matched exactly N times. |
{N,} | The preceding item is matched N or more times. |
{N,M} | The preceding item is matched at least N times,but not more than M times. |
– | Represents the range if it’s not first or last in a list or the ending point of a range in a list. |
^ | Matches the empty string at the beginning of a line; also represents the characters not in the range of a list. |
$ | Matches the empty string at the end of a line. |
\b | Matches the empty string at the edge of a word. |
\B | Matches the empty string provided it’s not at the edge of a word. |
\< | Match the empty string at the beginning of word. |
> | Match the empty string at the end of word. |
grep vs egrep
egrep与grep -E等价,其会将PATTERN翻译成扩展的正则表达式
https://www.cyberciti.biz/faq/grep-regular-expressions/