学习啦>学习电脑>操作系统>Linux教程>

linux中的grep命令的用法详解

佳洲分享

  linxu下的grep命令是经常使用到的命令之一。下面由学习啦小编为大家整理了linux的grep命令的用法详解的相关知识,希望对大家有帮助!

  一、linux中的grep命令的用法详解

  作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很有必要的。

  首先谈一下grep命令的常用格式为:【grep [选项] ”模式“ [文件]】

  常用选项:

  -E :开启扩展(Extend)的正则表达式。

  -i :忽略大小写(ignore case)。

  -v :反过来(invert),只打印没有匹配的,而匹配的反而不打印。

  -n :显示行号

  -w :被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker

  -c :显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项是显示有多少行没有被匹配到。

  --color :将匹配到的内容以颜色高亮显示。

  模式部分:

  1、直接输入要匹配的字符串,这个可以用fgrep(fast grep)代替来提高查找速度,比如我要匹配一下hello.c文件中printf的个数:grep -c "printf" hello.c

  2、使用基本正则表达式,下面谈关于基本正则表达式的使用:

  匹配字符:

  . :任意一个字符。

  [abc] :表示匹配一个字符,这个字符必须是abc中的一个。

  [a-zA-Z] :表示匹配一个字符,这个字符必须是a-z或A-Z这52个字母中的一个。

  [^123] :匹配一个字符,这个字符是除了1、2、3以外的所有字符。

  对于一些常用的字符集,系统做了定义:

  [A-Za-z]等价于[[:alpha:]]

  [0-9]等价于[[:digit:]]

  [A-Za-z0-9]等价于[[:alnum:]]

  tab,space等空白字符[[:space:]]

  [A-Z]等价于[[:upper:]]

  [a-z]等价于[[:lower:]]

  标点符号[[:punct:]]

  eg1:我想在hello.c文件中匹配printf但是要求其后面紧跟的不是数字

  grep "printf[^[:digit:]]" hello.c

  匹配次数:

  \{m,n\} :匹配其前面出现的字符至少m次,至多n次。

  \? :匹配其前面出现的内容0次或1次,等价于\{0,1\}。

  * :匹配其前面出现的内容任意次,等价于\{0,\},所以 ".*" 表述任意字符任意次,即无论什么内容全部匹配。

  eg2:我想在hello.c文件中匹配print和printf

  grep "printf\?" hello.c

  位置锚定:

  ^ :锚定行首

  $ :锚定行尾。技巧:"^$"用于匹配空白行。

  \b或\<:锚定单词的词首。如"\blike"不会匹配alike,但是会匹配liker

  \b或\>:锚定单词的词尾。如"\blike\b"不会匹配alike和liker,只会匹配like

  \B :与\b作用相反。

  eg3:我想在hello.c文件中匹配以 h 开头以 o 结尾的字符串。

  grep "\<h.*o\>" hello.c

  eg4:我想在hello.c中匹配行首为数字,行尾为字母的行

  grep "^[[:digit:]].*[[:alpha:]]$" hello.c

  分组及引用:

  \(string\) :将string作为一个整体方便后面引用

   class="con">

linux中的grep命令的用法详解

佳洲分享

  

  linxu下的grep命令是经常使用到的命令之一。下面由学习啦小编为大家整理了linux的grep命令的用法详解的相关知识,希望对大家有帮助!

  一、linux中的grep命令的用法详解

  作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很有必要的。

  首先谈一下grep命令的常用格式为:【grep [选项] ”模式“ [文件]】

  常用选项:

  -E :开启扩展(Extend)的正则表达式。

  -i :忽略大小写(ignore case)。

  -v :反过来(invert),只打印没有匹配的,而匹配的反而不打印。

  -n :显示行号

  -w :被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker

  -c :显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项是显示有多少行没有被匹配到。

  --color :将匹配到的内容以颜色高亮显示。

  模式部分:

  1、直接输入要匹配的字符串,这个可以用fgrep(fast grep)代替来提高查找速度,比如我要匹配一下hello.c文件中printf的个数:grep -c "printf" hello.c

  2、使用基本正则表达式,下面谈关于基本正则表达式的使用:

  匹配字符:

  . :任意一个字符。

  [abc] :表示匹配一个字符,这个字符必须是abc中的一个。

  [a-zA-Z] :表示匹配一个字符,这个字符必须是a-z或A-Z这52个字母中的一个。

  [^123] :匹配一个字符,这个字符是除了1、2、3以外的所有字符。

  对于一些常用的字符集,系统做了定义:

  [A-Za-z]等价于[[:alpha:]]

  [0-9]等价于[[:digit:]]

  [A-Za-z0-9]等价于[[:alnum:]]

  tab,space等空白字符[[:space:]]

  [A-Z]等价于[[:upper:]]

  [a-z]等价于[[:lower:]]

  标点符号[[:punct:]]

  eg1:我想在hello.c文件中匹配printf但是要求其后面紧跟的不是数字

  grep "printf[^[:digit:]]" hello.c

  匹配次数:

  \{m,n\} :匹配其前面出现的字符至少m次,至多n次。

  \? :匹配其前面出现的内容0次或1次,等价于\{0,1\}。

  * :匹配其前面出现的内容任意次,等价于\{0,\},所以 ".*" 表述任意字符任意次,即无论什么内容全部匹配。

  eg2:我想在hello.c文件中匹配print和printf

  grep "printf\?" hello.c

  位置锚定:

  ^ :锚定行首

  $ :锚定行尾。技巧:"^$"用于匹配空白行。

  \b或\<:锚定单词的词首。如"\blike"不会匹配alike,但是会匹配liker

  \b或\>:锚定单词的词尾。如"\blike\b"不会匹配alike和liker,只会匹配like

  \B :与\b作用相反。

  eg3:我想在hello.c文件中匹配以 h 开头以 o 结尾的字符串。

  grep "\<h.*o\>" hello.c

  eg4:我想在hello.c中匹配行首为数字,行尾为字母的行

  grep "^[[:digit:]].*[[:alpha:]]$" hello.c

  分组及引用:

  \(string\) :将string作为一个整体方便后面引用

  \1 :引用第一个左括号及其对应的右括号所匹配的内容。

  \2 :引用第二个左括号及其对应的右括号所匹配的内容。

  eg5:我想在hello.c文件中匹配行首以 l 开头 e 结尾的单词(比如 like,love等),行尾以相同的单词结尾。(比如这种行:large dog is a dog that is so large)

  grep "^\(l.*e\b\).*\b\1$" hello.c

  二、linux的grep命令的用法大全

  查找特定字符串并颜色显示

  [root@ www.linuxidc.com]# grep -n 'the' regular_express.txt --color=auto

  8:I can't finish the test.

  12:the symbol '*' is represented as start.

  15:You are the best is mean you are the no. 1.

  16:The world <Happy> is the same with "glad".

  18:google is the best tools for search keyword.

  反向选择查找特定字符串并颜色显示

  [root@ www.linuxidc.com]# grep -vn 'the' regular_express.txt --color=auto

  1:"Open Source" is a good mechanism to develop programs.

  2:apple is my favorite food.

  ......

  忽略大小写查找特定的字符

  [root@ www.linuxidc.com]# grep -in 'the' regular_express.txt

  使用 [] 来查找集合字符:

  [root@ www.linuxidc.com]# grep -n 't[ae]st' regular_express.txt

  8:I can't finish the test.

  9:Oh! The soup taste good.

  查找有‘oo’字符串

  [root@ www.linuxidc.com]# grep -n 'oo' regular_express.txt

  1:"Open Source" is a good mechanism to develop programs.

  2:apple is my favorite food.

  3:Football game is not use feet only.

  9:Oh! The soup taste good.

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  查找有‘oo’字符串,但是不要前面有g的,即剔除goo

  [root@ www.linuxidc.com]# grep -n '[^g]oo' regular_express.txt

  2:apple is my favorite food.

  3:Football game is not use feet only.

  18:google is the best tools for search keyword.

  19:goooooogle yes! :引用第二个左括号及其对应的右括号所匹配的内容。

  eg5:我想在hello.c文件中匹配行首以 l 开头 e 结尾的单词(比如 like,love等),行尾以相同的单词结尾。(比如这种行:large dog is a dog that is so large)

  grep "^\(l.*e\b\).*\b class="con">

linux中的grep命令的用法详解

佳洲分享

  二、linux的grep命令的用法大全

  查找特定字符串并颜色显示

  [root@ www.linuxidc.com]# grep -n 'the' regular_express.txt --color=auto

  8:I can't finish the test.

  12:the symbol '*' is represented as start.

  15:You are the best is mean you are the no. 1.

  16:The world <Happy> is the same with "glad".

  18:google is the best tools for search keyword.

  反向选择查找特定字符串并颜色显示

  [root@ www.linuxidc.com]# grep -vn 'the' regular_express.txt --color=auto

  1:"Open Source" is a good mechanism to develop programs.

  2:apple is my favorite food.

  ......

  忽略大小写查找特定的字符

  [root@ www.linuxidc.com]# grep -in 'the' regular_express.txt

  使用 [] 来查找集合字符:

  [root@ www.linuxidc.com]# grep -n 't[ae]st' regular_express.txt

  8:I can't finish the test.

  9:Oh! The soup taste good.

  查找有‘oo’字符串

  [root@ www.linuxidc.com]# grep -n 'oo' regular_express.txt

  1:"Open Source" is a good mechanism to develop programs.

  2:apple is my favorite food.

  3:Football game is not use feet only.

  9:Oh! The soup taste good.

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  查找有‘oo’字符串,但是不要前面有g的,即剔除goo

  [root@ www.linuxidc.com]# grep -n '[^g]oo' regular_express.txt

  2:apple is my favorite food.

  3:Football game is not use feet only.

  18:google is the best tools for search keyword.

  19:goooooogle yes!

3635303