grep

2019-06-18  本文已影响0人  pipu

[翻译] grep command in Unix/Linux

grep (gloabally search for regular expression and print out) 过滤器在一个文件中搜索由字符组成的特定的表达式并且展示所有匹配表达式的行。在搜索过程中这个表达式会被处理成正则表达式使用。

语法

grep [options] pattern [files]

参数说明

参考链接

grep command in Unix/Linux

示例

创建示例文件 a.txt,内容:

unix is great os. unix is opensource. unix is free os.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
  1. 忽略大小写

grep -i "UNix" a.txt

结果:

unix is great os. unix is opensource. unix is free os.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
  1. 显示匹配行的数量

grep -c "unix" a.txt

结果:

2
  1. 显示匹配的文件

grep -l "unix" * 或 grep -l "unix" f1.txt f2.txt

结果:

a.txt

  1. 查找单词匹配的

grep -w "unix" a.txt
结果:

unix is great os. unix is opensource. unix is free os.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

  1. 只展示匹配的部分

grep -o "unix" a.txt
结果:

unix
unix
unix
unix
unix
unix

  1. 显示行号

grep -n "unix" a.txt

结果:

1:unix is great os. unix is opensource. unix is free os.
4:uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

  1. 反转搜索

grep -v "unix" a.txt

结果:

learn operating system.
Unix linux which one you choose.

  1. 匹配行首是以某个字符串开头的行 (正则表达式的运用)

grep "^unix" a.txt

结果:

unix is great os. unix is opensource. unix is free os.

  1. 使用-e 参数,能匹配多个

grep -e "unix" -e "Unix" -e "UNix" a.txt

结果:

unix is great os. unix is opensource. unix is free os.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

  1. 匹配特定文件里面的表达式
    创建文件 pattern.txt,内容(其实和上例是一样的):
unix
Unix
UNix

grep -f pattern.txt a.txt

结果:

unix is great os. unix is opensource. unix is free os.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

上一篇下一篇

猜你喜欢

热点阅读