翻译领域不乏让人摸不着头脑的词汇,比如“句柄”、“套接字”、“鲁棒性”。当然,“正则表达式”也属于这一类词汇。我刚接触正则表达式的时候,对这个名词感到非常迷惑。深入了解之后,才突然明白,原来所谓的 regular expression, 其实就是“有规律、有模式的字符串”而已。
很少有一门技术,只需要投入少量的学习成本即可获得巨大的价值回报。正则表达式就属于这一类技术。可惜很多人被它密码般的语法形式当头棒喝,甚至连门都不得而入。
为什么你应该学习正则表达式?其一,在实践中应用这门技术其实不难,只需理解为数不多的几个元字符以及并不复杂的语法,就能够获得强大的文本操控能力;其二,正则表达式往往能提供处理文本的最简单最高效的解决方法(有时也许是唯一的解法)。遇上复杂的情况,如果你不会正则表达式,就只好束手无策、黯然神伤了。
正则表达式入门容易,精通却难。本文并不打算挑战此项任务,如果你希望系统地学习正则表达式,请一定阅读 Jeffrey Friedl 的著作 精通正则表达式。
正则表达式
grep 命令可以完成简单的文本搜索任务。
先来准备一份文本材料,把 grep 的帮助页保存为文本文件:
> man grep | col -b > grephelp.txt下面,我想检索 grephelp.txt 文件中所有包含 "find" 这个单词的文本行:
> grep "find" grephelp.txt To find all occurrences of the word `patricia' in a file: To find all occurrences of the pattern `.Pp' at the beginning of a line: To find all lines in a file which do not contain the words `foo' or我希望匹配到的文本使用不同的颜色显示,可以添加 --color 选项,默认的颜色是红色。
> grep --color "find" grephelp.txt我希望在匹配结果中显示文件名和行号,使用 -H 选项可以显示文件名,使用 -n 选项可以显示行号:
> grep -H -n --color "find" grephelp.txtgrephelp.txt:252: To find all occurrences of the word `patricia' in a file:grephelp.txt:256: To find all occurrences of the pattern `.Pp' at the beginning of a line:grephelp.txt:265: To find all lines in a file which do not contain the words `foo' or很多时候,我们需要知道匹配行前后的上下文。-A 和 -B 这两个选项会是你的好朋友。-A n 表示显示匹配行以及其后的 n 行;-B n 表示显示匹配行以及之前的 n 行。现在,我们在匹配行的前后分别额外显示两行:
> grep -A 2 -B 2 -H -n --color "find" grephelp.txtgrephelp.txt-250-grephelp.txt-251-EXAMPLESgrephelp.txt:252: To find all occurrences of the word `patricia' in a file:grephelp.txt-253-grephelp.txt-254- $ grep 'patricia' myfile----grephelp.txt-254- $ grep 'patricia' myfilegrephelp.txt-255-grephelp.txt:256: To find all occurrences of the pattern `.Pp' at the beginning of a line:grephelp.txt-257-grephelp.txt-258- $ grep '^\.Pp' myfile----grephelp.txt-263- match any character.grephelp.txt-264-grephelp.txt:265: To find all lines in a file which do not contain the words `foo' orgrephelp.txt-266- `bar':grephelp.txt-267-如果需要查找所有不包含 "find" 的文本行,该怎么做呢?很简单,使用 -v 选项即可。
grep 还有两个变体,egrep 和 fgrep。相对于仅支持基本正则模式(BREs)的 grep 来说,egrep 支持扩展正则模式(EREs),因而检索能力更为强大;fgrep 是所有三个工具中速度最快的一个,因为它完全不支持正则模式。
其实,我更喜欢一个叫做 ack 的工具。
ack
tr 命令可以完成简单的字符转换任务。例如,可以通过 tr 把 grephelp.txt 文件转换为全文大写:
> cat grephelp.txt | tr '[:lower:]' '[:upper:]'简而言之,tr 的工作就是把第一个集合中的字符转换为第二个集合中的相应的字符。常用的字符集合有下面这些:
[:alnum:]:字母数字 [:alpha:]:字母 [:cntrl:] :控制字符 [:digit:]:数字 [:graph:]: 图形字符 [:lower:]:小写字母 [:print:]:可打印字符 [:punct:]:标点符号 [:space:]:空白字符 [:upper:]:大写字母 [:xdigit:]:十六进制数字tr 命令的应用场景非常受限,如果希望进行更加灵活的模式替换,我们还有 sed(也就是 stream editor,流编辑器)。
把文件中所有的 "find" 文本替换为 "search":
> sed "s/find/search/g" grephelp.txt这条命令中,s 表示执行“替换操作”,/find/search/ 表示把 "find" 替换为 "search",g 表示对一行中所有的匹配进行替换。sed 默认把处理结果打印到标准输出,我们可以通过重定向把处理结果转储到一个新文件中,或者使用选项 -i 把结果直接写回原文件(有风险,需谨慎):
> sed -i "s/find/search/g" grephelp.txt把文件中所有的数字 n 替换为 "--n--" 的形式:
> sed -E "s/([0-9]+)/--\1--/g" grephelp.txt选项 -E 表示在处理过程中使用扩展的正则模式(EREs),替换命令中的 \1 表示引用正则表达式的第一个捕获分组。请注意,-E 这个选项只在 Mac OS X 系统和 FreeBSD 系统上有效,其他 Unix 系统需要使用另一个等效的选项 -r。
sed 的功能远不止这一些,篇幅所限,不可能详细讲解 sed 的用法。如果希望学习更多,请移步这篇文章。
这是莎士比亚的一首十四行诗,只可惜第5行和第10行有重复(而且第10行重复了3次)。怎么查看文本中重复的行呢?uniq 命令可以帮助你。
> uniq -d sonnet116.txtO, no! it is an ever-fix`ed mark,Love's not Time's fool, though rosy lips and cheeks选项 -d 表示仅输出重复的行。如果需要去重,使用不带选项的 uniq 命令就可以了:
> uniq sonnet116.txtLet me not to the marriage of true mindsAdmit impediments. Love is not loveWhich alters when it alteration finds,Or bends with the remover to remove:O, no! it is an ever-fix`ed mark,That looks on tempests and is never shaken;It is the star to every wand'ring bark,Whose worth's unknown, although his heighth be taken.Love's not Time's fool, though rosy lips and cheeksWithin his bending sickle's compass come;Love alters not with his brief hours and weeks,But bears it out even to the edge of doom:If this be error and upon me proved,I never writ, nor no man ever loved.想要查看每一行究竟重复了多少次?没问题,使用选项 -c:
> uniq -c sonnet116.txt 1 Let me not to the marriage of true minds 1 Admit impediments. Love is not love 1 Which alters when it alteration finds, 1 Or bends with the remover to remove: 2 O, no! it is an ever-fix`ed mark, 1 That looks on tempests and is never shaken; 1 It is the star to every wand'ring bark, 1 Whose worth's unknown, although his heighth be taken. 3 Love's not Time's fool, though rosy lips and cheeks 1 Within his bending sickle's compass come; 1 Love alters not with his brief hours and weeks, 1 But bears it out even to the edge of doom: 1 If this be error and upon me proved, 1 I never writ, nor no man ever loved.假设有这样一个报表文件,第一列是月份,第二列是当月的销售个数:
> cat report.txtMarch,19June,50February,17May,18August,16April,31May,18July,26January,24August,16这个文件的内容不仅顺序是乱的,而且还有重复。我希望按字母表顺序排序,可以下面这个命令:
> sort report.txtApril,31August,16August,16February,17January,24July,26June,50March,19May,18May,18选项 -u (表示 unique)可以在排序结果中去除重复行:
> sort -u report.txtApril,31August,16February,17January,24July,26June,50March,19May,18能不能按照月份排序呢?选项 -M (表示 month-sort)可以帮助我们:
> sort -u -M report.txtJanuary,24February,17March,19April,31May,18June,50July,26August,16按照第二列的数字进行排序也是很简单的:
> sort -u -t',' -k2 report.txtAugust,16February,17May,18March,19January,24July,26April,31June,50上面的例子中,选项 -t',' 表示以逗号为分隔符对文本进行列分割;-k2 表示对第2列进行排序。
当然了,把结果逆序排列也并非不可能:
> sort -u -r -t',' -k2 report.txtJune,50April,31July,26January,24March,19May,18February,17August,16wc 命令用来完成文本统计工作,通过使用不同的选项,它可以统计文件中的字节数(-c),字符数(-m),单词数(-w)与行数(-l)。
例如,查看 grephelp.txt 这个文件总共有多少个单词:
> wc -w grephelp.txt 1571 grephelp.txt查看 sonnet116.txt 这个文件总共有多少不重复的行(废话,十四行诗当然是有14行):
> uniq sonnet116.tx6 | wc -l 14如果上面介绍的工具仍然不能满足你,也许你需要火力更强的武器。试试 Awk 与 Perl 吧。
Awk 也是一款上古神器,它的年龄可能和 sed 不相上下。Awk 可谓是专门为了文本处理而生,它的语法和特性非常适合用于操纵文本和生成报表。如需学习,请参考 这篇文章,你会喜欢上它的。
长久以来,Perl 背负了“只写语言”的恶名。实际上,只要处理得当,用 Perl 一样可以写出模块清晰的、容易阅读和理解的代码。根据我的经验,使用 Perl 的场合 80% 以上与文本处理有关。Perl 内置的正则表达式支持可能是所有语言中最好的,再加上简洁紧凑的语法以及便利的操作符,这些特性帮助 Perl 成了文本处理领域当仁不让的霸主。
本文来自云栖社区合作伙伴“Linux中国”,原文发表于2013-04-02.