复制代码代码如下: > 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.txt grephelp.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.txt grephelp.txt-250- grephelp.txt-251-EXAMPLES grephelp.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" myfile grephelp.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" or grephelp.txt-266- `bar": grephelp.txt-267-
复制代码代码如下: > sed -E "s/([0-9]+)/--1--/g" grephelp.txt
选项 -E 表示在处理过程中使用扩展的正则模式(EREs),替换命令中的 1 表示引用正则表达式的第一个捕获分组。请注意,-E 这个选项只在 Mac OS X 系统和 FreeBSD 系统上有效,其他 Unix 系统需要使用另一个等效的选项 -r。 sed 的功能远不止这一些,篇幅所限,不可能详细讲解 sed 的用法。如果希望学习更多,请移步这篇文章。 文本去重
复制代码代码如下: > cat -n sonnet116.txt 1Let me not to the marriage of true minds 2Admit impediments. Love is not love 3Which alters when it alteration finds, 4Or bends with the remover to remove: 5O, no! it is an ever-fix`ed mark, 6O, no! it is an ever-fix`ed mark, 7That looks on tempests and is never shaken; 8It is the star to every wand"ring bark, 9Whose worth"s unknown, although his heighth be taken. 10Love"s not Time"s fool, though rosy lips and cheeks 11Love"s not Time"s fool, though rosy lips and cheeks 12Love"s not Time"s fool, though rosy lips and cheeks 13Within his bending sickle"s compass come; 14Love alters not with his brief hours and weeks, 15But bears it out even to the edge of doom: 16If this be error and upon me proved, 17I never writ, nor no man ever loved.
复制代码代码如下: > uniq -d sonnet116.txt O, no! it is an ever-fix`ed mark, Love"s not Time"s fool, though rosy lips and cheeks
选项 -d 表示仅输出重复的行。如果需要去重,使用不带选项的 uniq 命令就可以了:
复制代码代码如下: > uniq sonnet116.txt Let me not to the marriage of true minds Admit impediments. Love is not love Which 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 cheeks Within 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.