Writing /share/Web/wiki/data/cache/5/5ac565da43712cbb130d435f11194a15.metadata failed
linux:正则表达式
差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
| linux:正则表达式 [2022/12/05 15:21] – [2. ERE增强] zhangguo | linux:正则表达式 [2023/05/19 14:46] (当前版本) – [3. sed命令] zhangguo | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== - 正则表达式 ====== | ====== - 正则表达式 ====== | ||
| + | 参考book: The Linux Command Line | ||
| 这里主要是指POSIX Basic Vs. Extended Regular Expressions,不是指perl用的那一套正则表达式。 | 这里主要是指POSIX Basic Vs. Extended Regular Expressions,不是指perl用的那一套正则表达式。 | ||
| 行 72: | 行 73: | ||
| <code bash> | <code bash> | ||
| - | $ echo "haha [i8899" | + | $ echo "haha [i8899" |
| haha [i8899 | haha [i8899 | ||
| + | |||
| + | 像这种情况下,匹配到的是haha [i, 当前行还是被匹配到的。如果不想匹配带89的行,需要在grep的参数加上-v选项。 | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== - grep选中不匹配的行 ==== | ||
| + | grep -v选项的解释 | ||
| + | < | ||
| + | -v, --invert-match | ||
| + | Invert the sense of matching, to select non-matching lines. | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | $ echo "haha [i8899" | ||
| + | |||
| </ | </ | ||
| 行 103: | 行 119: | ||
| </ | </ | ||
| + | |||
| + | < | ||
| + | {n} Match the preceding element if it occurs exactly n times. | ||
| + | {n,m} Match the preceding element if it occurs at least n times, but no | ||
| + | more than m times. | ||
| + | {n,} Match the preceding element if it occurs n or more times. | ||
| + | {,m} Match the preceding element if it occurs no more than m times. | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== - sed命令 ===== | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | sed ' | ||
| + | |||
| + | # regexp expand方式的匹配,可以理解为ERE | ||
| + | sed -r ' | ||
| + | |||
| + | # 和上面效果一样,是普通的regexp, | ||
| + | sed ' | ||
| + | |||
| + | |||
| + | # -i, 将替换后的结果写回原文件中 | ||
| + | sed -i ' | ||
| + | </ | ||
linux/正则表达式.1670224893.txt.gz · 最后更改: 2023/03/17 10:12 (外部编辑)