Appearance
正则表达式
regexp
sh
# ^word: 匹配在行首的word
$ grep '^word' <path.txt>
# word$: 匹配在行尾的word
$ grep 'word$' <path.txt>
# .: 代表一个任意字符
$ grep 'word.word' <path.txt>
# \: 转移符
$ grep \'word <path.txt>
# --- 字符集合 ---
# [list]: 字符集合,代表其中一个任意字符。
$ grep [a1s2] <path.txt>
# [n-m]: 字符集合,代表其中一个任意字符。- 表示连续。如,[1-9],[a-z],[A-Z],[a-Z]
$ grep [a-Z] <path.txt>
# [^list]: 字符集合,非集合中任意一个字符。
$ grep [^a-Z] <path.txt>
# --- 多次重复 ---
# \{n,m\}: 重复,字符n到m次
$ grep 'c\{1,2}' <path.txt>
# \{n,\}: 重复,字符至少n次
$ grep 'c\{1,\}' <path.txt>
# \{n\}: 重复,字符n次
$ grep 'c\{1\}' <path.txt>
# *: 重复,0到无穷次。(.* 表示任意零或无穷个字符)
$ grep .* <path.txt>
# --- 特别字符 ---
# [:alnum:]: 代表一个,大小写字母及数字
# [:alpha:]: 代表一个,大小写字母
# [:blank:]: 代表一个,空格及tab
# [:cntrl:]: 代表一个,控制按键 CR,LF,Tab,Del
# [:digit:]: 代表一个,数字
# [:xdigit:]: 代表一个,16进制字符
# [:lower:]: 代表一个,小写字母
# [:upper:]: 代表一个,大写字母
# [:print:]: 代表一个,打印的字符
# [:punct:]: 代表一个,标点符号(punctuation symbal)
# [:space:]: 代表一个,空格,Tab,CR
# [:graph:]: 代表一个,除空白字符外的其他所有按键
$ grep -n "[[:lower:]]" <path.txt>extended-regexp
sh
# +: 重复,一或多个字符
$ grep -E 'c+' <path>
$ egrep 'c+' <path>
# ?: 重复,零或一个字符
$ grep -E 'c?' <path>
$ egrep 'c?' <path>
# |: 逻辑或。意为,同时匹配多个
$ egrep 'a|b' <path>
# (|): 分组
$ egrep 'g(oo|la)d' <path> # good,glad
# ()+: 重复组内容
$ egrep '(abc)+' <path> # abcabcabc...grep
sh
# i: ignore case distinctions in patterns and data
# n: print line number with output lines
# A: print NUM lines of trailing context
# B: print NUM lines of leading context
$ dmesg | grep -n -A 3 -B 3 -i eth0
# grep -E <==> egrep, extended-regexp
# grep -F <==> fgrep, fixed string
# grep -r <==> rgrep, like --directory=recurse, -d: how to handle directoriessed
sh
# 匹配行内所有并替换
$ sed 's/old-string/new-string/g' <path>
# 提取指定行,第 n 行
$ sed 'np' <path>
# 提取指定行,n 到 m 行
$ sed 'n,mp' <path>