使用 Linux/Unix 进行文本处理


正则表达式

翻译不乏让人摸不着头脑的词汇,比如“句柄”、“套接字”、“鲁棒性”。当然,“正则表达式”也属于这一类词汇。我刚接触正则表达式的时候,对这个名词感到非常迷惑。深入了解之后,才突然明白,原来所谓的 regular expression, 其实就是“有规律、有模式的字符串”而已。

很少有一门技术,只需要投入少量的学习成本即可获得巨大的回报。正则表达式就属于这一类技术。可惜很多人被它密码般的语法形式当头棒喝,甚至连门都不得而入。

为什么你应该学习正则表达式?其一,在实践中应用这门技术其实不难,只需理解为数不多的几个元字符以及并不复杂的语法,就能够获得强大的文本操控能力;其二,正则表达式往往能提供处理文本的最简单最高效的解决方法(有时也许是唯一的解法)。遇上复杂的情况,如果你不会正则表达式,就只好束手无策、黯然神伤了。

正则表达式入门容易,精通却难。本文并不打算挑战此项任务,如果你希望系统地学习正则表达式,请一定阅读 Jeffrey Friedl 的著作 。

正则表达式

正则表达式

 

文本检索

grep 命令可以完成简单的文本搜索任务。

先来准备一份文本材料,把 grep 的帮助页保存为文本文件:

  1. >mangrep| col -b > grephelp.txt

下面,我想检索 grephelp.txt 文件中所有包含 “find” 这个单词的文本行:

  1. >grep"find" grephelp.txt
  2. Tofind all occurrences of the word `patricia' in a file:
  3. To find all occurrences of the pattern `.Pp' at the beginning of a line:
  4. To find all lines in a file which do not contain the words `foo'or

我希望匹配到的文本使用不同的颜色显示,可以添加 --color 选项,默认的颜色是红色。

  1. >grep--color "find" grephelp.txt

我希望在匹配结果中显示文件名和行号,使用 -H 选项可以显示文件名,使用 -n 选项可以显示行号:

  1. >grep-H -n --color "find" grephelp.txt
  2. grephelp.txt:252:Tofind all occurrences of the word `patricia' in a file:
  3. grephelp.txt:256: To find all occurrences of the pattern `.Pp' at the beginning of a line:
  4. 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 行。现在,我们在匹配行的前后分别额外显示两行:

  1. >grep-A 2-B 2-H -n --color "find" grephelp.txt
  2. grephelp.txt-250-
  3. grephelp.txt-251-EXAMPLES
  4. grephelp.txt:252:Tofind all occurrences of the word `patricia' in a file:
  5. grephelp.txt-253-
  6. grephelp.txt-254- $ grep 'patricia' myfile
  7. --
  8. --
  9. grephelp.txt-254- $ grep 'patricia' myfile
  10. grephelp.txt-255-
  11. grephelp.txt:256: To find all occurrences of the pattern `.Pp' at the beginning of a line:
  12. grephelp.txt-257-
  13. grephelp.txt-258- $ grep '^\.Pp' myfile
  14. --
  15. --
  16. grephelp.txt-263- match any character.
  17. grephelp.txt-264-
  18. grephelp.txt:265: To find all lines in a file which do not contain the words `foo'or
  19. grephelp.txt-266-`bar':
  20. grephelp.txt-267-

如果需要查找所有不包含 “find” 的文本行,该怎么做呢?很简单,使用 -v 选项即可。

grep 还有两个变体,egrep 和 fgrep。相对于仅支持基本正则模式(BREs)的 grep 来说,egrep 支持扩展正则模式(EREs),因而检索能力更为强大;fgrep 是所有三个工具中速度最快的一个,因为它完全不支持正则模式。

其实,我更喜欢一个叫做  的工具。

ack

ack

 

文本替换

tr 命令可以完成简单的字符转换任务。例如,可以通过 tr 把 grephelp.txt 文件转换为全文大写:

  1. >cat grephelp.txt |tr'[:lower:]''[:upper:]'

简而言之,tr 的工作就是把第一个集合中的字符转换为第二个集合中的相应的字符。常用的字符集合有下面这些:

  • [:alnum:]:字母数字
  • [:alpha:]:字母
  • [:cntrl:] :控制字符
  • [:digit:]:数字
  • [:graph:]: 图形字符
  • [:lower:]:小写字母
  • [:print:]:可打印字符
  • [:punct:]:标点符号
  • [:space:]:空白字符
  • [:upper:]:大写字母
  • [:xdigit:]:十六进制数字

tr 命令的应用场景非常受限,如果希望进行更加灵活的模式替换,我们还有 sed(也就是 stream editor,流编辑器)。

把文件中所有的 “find” 文本替换为 “search”:

  1. >sed"s/find/search/g" grephelp.txt

这条命令中,s 表示执行“替换操作”,/find/search/ 表示把 “find” 替换为 “search”,g 表示对一行中所有的匹配进行替换。sed 默认把处理结果打印到标准输出,我们可以通过重定向把处理结果转储到一个新文件中,或者使用选项 -i 把结果直接写回原文件(有风险,需谨慎):

  1. >sed-i "s/find/search/g" grephelp.txt

把文件中所有的数字 n 替换为 “–n–” 的形式:

  1. >sed-E "s/([0-9]+)/--\1--/g" grephelp.txt

选项 -E 表示在处理过程中使用扩展的正则模式(EREs),替换命令中的 \1 表示引用正则表达式的第一个捕获分组。请注意,-E 这个选项只在 Mac OS X 系统和 FreeBSD 系统上有效,其他 Unix 系统需要使用另一个等效的选项 -r

sed 的功能远不止这一些,篇幅所限,不可能详细讲解 sed 的用法。如果希望学习更多,请移步。

 

文本去重

  1. >cat-n sonnet116.txt
  2. 1Let me not to the marriage of true minds
  3. 2Admit impediments.Loveisnot love
  4. 3Which alters when it alteration finds,
  5. 4Or bends with the remover to remove:
  6. 5 O,no! it is an ever-fix`ed mark,
  7. 6 O, no! it is an ever-fix`ed mark,
  8. 7That looks on tempests andis never shaken;
  9. 8Itis the star to every wand'ring bark,
  10. 9 Whose worth's unknown, although his heighth be taken.
  11. 10Love's not Time's fool, though rosy lips and cheeks
  12. 11Love's not Time's fool, though rosy lips and cheeks
  13. 12Love's not Time's fool, though rosy lips and cheeks
  14. 13Within his bending sickle's compass come;
  15. 14 Love alters not with his brief hours and weeks,
  16. 15 But bears it out even to the edge of doom:
  17. 16 If this be error and upon me proved,
  18. 17 I never writ, nor no man ever loved.

这是莎士比亚的一首十四行诗,只可惜第5行和第10行有重复(而且第10行重复了3次)。怎么查看文本中重复的行呢?uniq 命令可以帮助你。

  1. >uniq-d sonnet116.txt
  2. O,no! it is an ever-fix`ed mark,
  3. Love's not Time's fool, though rosy lips and cheeks

选项 -d 表示仅输出重复的行。如果需要去重,使用不带选项的 uniq 命令就可以了:

  1. >uniq sonnet116.txt
  2. Let me not to the marriage of true minds
  3. Admit impediments.Loveisnot love
  4. Which alters when it alteration finds,
  5. Or bends with the remover to remove:
  6. O,no! it is an ever-fix`ed mark,
  7. That looks on tempests and is never shaken;
  8. It is the star to every wand'ring bark,
  9. Whose worth's unknown, although his heighth be taken.
  10. Love's not Time's fool, though rosy lips and cheeks
  11. Within his bending sickle's compass come;
  12. Love alters not with his brief hours and weeks,
  13. But bears it out even to the edge of doom:
  14. If this be error and upon me proved,
  15. I never writ, nor no man ever loved.

想要查看每一行究竟重复了多少次?没,使用选项 -c

  1. >uniq-c sonnet116.txt
  2. 1Let me not to the marriage of true minds
  3. 1Admit impediments.Loveisnot love
  4. 1Which alters when it alteration finds,
  5. 1Or bends with the remover to remove:
  6. 2 O,no! it is an ever-fix`ed mark,
  7. 1 That looks on tempests and is never shaken;
  8. 1 It is the star to every wand'ring bark,
  9. 1 Whose worth's unknown, although his heighth be taken.
  10. 3 Love's not Time's fool, though rosy lips and cheeks
  11. 1 Within his bending sickle's compass come;
  12. 1 Love alters not with his brief hours and weeks,
  13. 1 But bears it out even to the edge of doom:
  14. 1 If this be error and upon me proved,
  15. 1 I never writ, nor no man ever loved.

 

文本排序

假设有这样一个报表文件,第一列是月份,第二列是当月的销售个数:

  1. >cat report.txt
  2. March,19
  3. June,50
  4. February,17
  5. May,18
  6. August,16
  7. April,31
  8. May,18
  9. July,26
  10. January,24
  11. August,16

这个文件的内容不仅顺序是乱的,而且还有重复。我希望按字母表顺序排序,可以下面这个命令:

  1. >sort report.txt
  2. April,31
  3. August,16
  4. August,16
  5. February,17
  6. January,24
  7. July,26
  8. June,50
  9. March,19
  10. May,18
  11. May,18

选项 -u (表示 unique)可以在排序结果中去除重复行:

  1. >sort-u report.txt
  2. April,31
  3. August,16
  4. February,17
  5. January,24
  6. July,26
  7. June,50
  8. March,19
  9. May,18

能不能按照月份排序呢?选项 -M (表示 month-sort)可以帮助我们:

  1. >sort-u -M report.txt
  2. January,24
  3. February,17
  4. March,19
  5. April,31
  6. May,18
  7. June,50
  8. July,26
  9. August,16

按照第二列的数字进行排序也是很简单的:

  1. >sort-u -t','-k2 report.txt
  2. August,16
  3. February,17
  4. May,18
  5. March,19
  6. January,24
  7. July,26
  8. April,31
  9. June,50

上面的例子中,选项 -t',' 表示以逗号为分隔符对文本进行列分割;-k2 表示对第2列进行排序。

当然了,把结果逆序排列也并非不可能:

  1. >sort-u -r -t','-k2 report.txt
  2. June,50
  3. April,31
  4. July,26
  5. January,24
  6. March,19
  7. May,18
  8. February,17
  9. August,16

 

文本统计

wc 命令用来完成文本统计工作,通过使用不同的选项,它可以统计文件中的字节数(-c),字符数(-m),单词数(-w)与行数(-l)。

例如,查看 grephelp.txt 这个文件总共有多少个单词:

  1. >wc-w grephelp.txt
  2. 1571 grephelp.txt

查看 sonnet116.txt 这个文件总共有多少不重复的行(废话,十四行诗当然是有14行):

  1. >uniq sonnet116.tx6 |wc-l
  2. 14

 

你还应该试试 Awk 与 Perl

如果上面介绍的工具仍然不能满足你,也许你需要火力更强的武器。试试 Awk 与 Perl 吧。

Awk 也是一款上古神器,它的年龄可能和 sed 不相上下。Awk 可谓是专门为了文本处理而生,它的语法和特性非常适合用于操纵文本和生成报表。如需学习,请参考 ,你会喜欢上它的。

AWK 简介和例子

Shell脚本之AWK文本编辑器语法

正则表达式中AWK的学习和使用

文本数据处理之AWK 图解

如何在中使用awk命令

文本分析工具-awk 

长久以来,Perl 背负了“只写语言”的恶名。实际上,只要处理得当,用 Perl 一样可以写出模块清晰的、容易阅读和理解的代码。根据我的经验,使用 Perl 的场合 80% 以上与文本处理有关。Perl 内置的正则表达式支持可能是所有语言中最好的,再加上简洁紧凑的语法以及便利的操作符,这些特性帮助 Perl 成了文本处理当仁不让的霸主。

转载自:https://www.linuxidc.com/Linux/2015-11/125416.htm

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《使用 Linux/Unix 进行文本处理
本文地址:https://www.zhiletu.com/archives-5317.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微