Shell正则四剑客 | grep命令

Shell编程四剑客之GREP简介

 

       grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。php

 

       Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不一样。egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把全部的字母都看做单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,再也不特殊。linux使用GNU版本的grep。它功能更强,能够经过-G、-E、-F命令行选项来使用egrep和fgrep的功能。html

        

        全面搜索正则表达式(Global search regular expression(RE) ,GREP)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。mysql

       Unix/Linux的grep家族包括grep、egrep和fgrep,其中egrep和fgrep的命令跟grep有细微的区别,egrep是grep的扩展,支持更多的re元字符, fgrep是fixed grep或fast grep简写,它们把全部的字母都看做单词,正则表达式中的元字符表示其自身的字面意义,再也不有其余特殊的含义,通常使用比较少。linux

       目前Linux操做系统默认使用GNU版本的grep。它功能更强,能够经过-G、-E、-F命令行选项来使用egrep和fgrep的功能。其语法格式及经常使用参数详解以下:nginx

grep -[acinv]   ‘word’   Filenamegit

 

grep命令

参数详解以下:正则表达式

经常使用选项

  • -c                行数spring

  • -i                不区分大小写sql

  • -n             显示行号express

  • -v                取反

  • -r             遍历全部子目录

  • -A             后面跟数字,过滤出符合要求的行以及下面的n行

  • -B                同上,过滤出符合要求的行以及上面的n行

  • -C                同上,过滤出符合要求的行以及上下各n行

  • -a                  以文本文件方式搜索;

  • -c                  计算找到的符合行的次数;

  • -i                   忽略大小写;

  • -n                  顺便输出行号;

学习Grep时,须要了解通配符、正则表达式两个概念,不少读者容易把彼此搞混淆,通配符主要用在Linux的Shell命令中,经常使用于文件或者文件名称的操做,而正则表达式用于文本内容中的字符串搜索和替换,经常使用在AWK、GREP、SED、VIM工具中对文本的操做。

正则表达式元字符

元字符 功能
^ 以什么开头
$ 以什么结尾
. 匹配一个字符
* 匹配0个或多个
[] 匹配集合中的
[x-y] 匹配集合范围内的
[^ ] 匹配不在集合中的
\ 转义

特殊的元字符

元字符 功能 实例 怎么匹配
\< 以什么开头 '\<love' 匹配以love开头的全部行
\> 以什么结尾 'love\>' 匹配love结尾的全部行
\(..\) 标签匹配之后使用的字符 '\(love\)able \1er' 用位置\1\2引导前面作好的标签,最大支持9个
x\{m\} or x\{m,\} or x\{m,n\} 重复字符x,m次,至少m次,至少m且不超过n次 o\{5,10\} o字符重复5到10次的行

扩展的正则表达式

  • 元字符 说明
    + 重复前一个字符一个或一个以上
    0个或者一个字符
    | 表示或,查找多个字符串
    () 分组过滤匹配

通配符类型详解:

*                  0个或者多个字符、数字;

?                  匹配任意一个字符;

#                 表示注解;

|                  管道符号;

                多个命令连续执行;

正则表达式详解:

*                                  前一个字符匹配0次或屡次;

.                                   匹配除了换行符之外任意一个字符;

.*                                 表明任意字符;

^                                 匹配行首,即以某个字符开头;

$                                 匹配行尾,即以某个字符结尾;

\(..\)                           标记匹配字符;

[]                                匹配中括号里的任意指定字符,但只匹配一个字符;

[^]                             匹配除中括号之外的任意一个字符;

 

基本正则表达式元字符 :

字符匹配 : 

 .  : 匹配任意单个字符;

[] : 匹配指定范围内的任意单个字符;

 [^] : 匹配指定范围外的任意单个字符;

[:digit:]、[:lower:]、[:upper:]、[:alpha:]、[:alnum:]、[:punct:]、[:space:]

匹配次数 : 用在要指定其出现的次数的字符的后面,用于限制其前面字符出现的次数;默认工做于贪婪模式;

*  : 匹配其前面的字符任意次;0,1,屡次;

         例如 : grep "x*y"

                          abcd

                         acbd

*  :   匹配任意长度的任意字符

\?  :  匹配其前面的字符0次或1次;即其前面的字符是无关紧要的;

\+  :  匹配其前面的字符1次或屡次;即其面的字符要出现至少1次;

\{m\}   :  匹配其前面的字符m次;

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

\{0,n\}   :  至多n次;

\{m,\}   :   至少n次;

 

位置锚定 : 

         ^:行首锚定 :  用于模式的最左侧;

         $:行尾锚定 : 用于模式的最右侧;

         ^PATTERN$ :用于PATTERN来匹配整行;

          单词 : 非特殊字符组成的连续字符(字符串)都称为单词;

         \<或\b:词首锚定,用于单词模式的左侧;

         \<或\b:词尾锚定,用于单词模式的右侧;

         \<PATTERN\>:匹配完整单词;

分组及引用 

         \(\) : 将一个或多个字符捆绑在一块儿,看成一个总体进行处理;

                 \(xy\)*ab 

        Note : 分组括号中的模式匹配到的内容会被正则表达式引型自动记录于内部的变量中,这些变量为:

        \1 :  模式从左侧起,第一个左括号以及与之匹配的右括号之间的模式所匹配到的字符;

        \2 :  模式从左侧起,第一个左括号以及与之匹配的右括号之间的模式所匹配到的字符;

        \3 :  

        后向引用 : 引用前面的分组括号中的模式所匹配到的字符;

使用示例     

grep -n 'root' /etc/passwd
grep -nv 'nologin' /etc/passwd
grep '[0-9]' /etc/passwd
grep -v '[0-9]' /etc/passwd
grep -v '^#' /etc/passwd
grep '^[^a-zA-Z]' /etc/passwd
grep 'r.o' /etc/passwd
grep '00*' /etc/passwd
grep '.*' /etc/passwd
grep 'o\{2\}' /etc/passwd
grep -E 'o{2}' /etc/passwd
grep -E 'o+' /etc/passwd
grep -E 'oo?' /etc/passwd
grep -E 'root|nologin' /etc/passwd
grep -E '(oo){2}' /etc/passwd

经常使用GREP工具企业演练案列:

grep -c “test”1.txt                                统计test字符总行数;

  • -n 打印匹配的行号

  • -i 搜索时忽略大小写

  • -l 只打印文件名

grep -i “TEST” 1.txt                               不区分大小写查找TEST全部的行;

grep -n “test” 1.txt                               打印test的行及行号;

grep -v “test”1.txt                                不打印test的行;

grep “test[53]” 1.txt                             以字符test开头,接5或者3的行;

grep “^[^test]” jfedu.txt                           显示输出行首不是test的行;

grep “[Mm]ay” jfedu.txt                           匹配M或m开头的行;

grep “K…D” jfedu.txt                               匹配K,三个任意字符,紧接D的行;

grep -xvf a b | tee c | wc -l                       把文件b中有的,可是文件a中没有的全部行,保存为文件c,并统计c的行数。

 

grep -E 'WARNING|FATAL' a.log | grep -v IGNOR | awk -F ":" '{print $5}'     从a.log文件中提取包含"WARNING"或"FATAL",同时不包含"IGNOR"的行,而后提取以":"分割的第5个字段

 

grep "class" . -R -n                               在多级目录中对文本递归搜索

grep -e "class" -e "vitural" file              匹配多个模式

grep "test" file* -lZ| xargs -0 rm          grep输出以做为结尾符的文件名:(-z)

 

find source_dir/ -type f -name "*.cpp" -print0 |xargs -0 wc -l              统计程序行数

实操

  • 匹配以love开头的全部行
$ grep '^love' re-file
love, how much I adore you. Do you know
  • 匹配love结尾的全部行
$ grep 'love$' re-file
clover. Did you see them?  I can only hope love.
  • 匹配以l开头,中间包含两个字符,结尾是e的全部行
$ grep 'l..e' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
  • 匹配0个或多个空行,后面是love的字符
$ grep ' *love' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
  • 匹配loveLove
$ grep '[Ll]ove' re-file  # 对l不区分大小写
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
  • 匹配A-Z的字母,其次是ove
$ grep '[A-Z]ove' re-file
Lovers were all around us. It is springtime. Oh
  • 匹配不在A-Z范围内的任何字符行,全部的小写字符
$ grep '[^A-Z]' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
  • 匹配love.
$ grep 'love\.' re-file
clover. Did you see them?  I can only hope love.
  • 匹配空格
$ grep '^$' re-file
  • 匹配任意字符
$ grep '.*' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
  • 前面o字符重复2到4次
$ grep 'o\{2,4\}' re-file
groove.
  • 重复o字符至少2次
$ grep 'o\{2,\}' re-file
groove.
  • 重复0字符最多2次
$ grep 'o\{,2\}' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
  • 重复前一个字符一个或一个以
$ egrep "go+d" linux.txt
Linux is a good
god assdxw bcvnbvbjk
gooodfs awrerdxxhkl
good
  • 0个或者一个字符
ansheng@Ubuntu:/tmp$ egrep "go?d" linux.txt
god assdxw bcvnbvbjk
gdsystem awxxxx
  • 或,查找多个字符串
$ egrep "gd|good" linux.txt
Linux is a good
gdsystem awxxxx
good
  • 分组过滤匹配
$ egrep "g(la|oo)d" linux.txt
Linux is a good
glad
good

 

使用案例 : 

      一、显示/etc/passwd文件中不以/bin/bash结尾的行;

          # grep -v "/bin/bash$" /etc/passwd

      二、找出/etc/passwd文件中的两位数或三位数;

       #grep "\]<[0-9\{2,3\}\>" /etc/passwd

      三、找出/etc/rc.d/rc.sysinit或/etc/grub2.cfg文件中,以致少一个空白字符开头,且后面非空白字符的行;

         # grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg

      四、找出“netstat -tan” 命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行;

        # netstat -tan | grep "LISTEN[[:space:]]*$" 

      五、找出以root开头 、root结尾的行;

        # grep "^root$" /etc/passwd

      六、找出文档中包含l..e.*l..e的在love.txt中

         # grep "l..e.*l..e" love.txt

         # grep "\(l..e).*\1" love.txt

      七、搜索含有root,hadoop的行

grep -E “root|hadoop” /etc/passwd

 

      八、只搜索行首含有root,hadoop的行

grep -E “^(root|hadoop)\>” /etc/passwd

 

      九、只搜索行首含有root,hadoop的行,并重定向到/mnt/sysroot/etc/passwd

grep -E “^(root|hadoop)\>” /etc/passwd > /mnt/sysroot/etc/passwd

 

      十、查看磁盘大小,并查找以“Disk /dev/[sh]d[a-z]”开头,只显示第一行。

答 : disk -l 2> /dev/null | grep "^Disk /dev/[sh]d[a-z]”" | awk -F:'(print $1)'

 

十一、在用户目录下查找文件名后缀为.yml的文件

find ~ -name '*.yml' | grep '\.yml' --color=always

 

十二、查找当前目录下权限为644的全部文件

find . -perm 644

 

1三、整个语句是在当前目录下查找名为feed.xml的文件,同时须要忽略./_site*路径的文件。-a -o 实际为逻辑与逻辑或,当路径匹配时将执行-prune,那么将不会查找匹配路径中的文件,当路径不匹配时则不执行-prune,-o后的语句始终执行。

find . -path './_site*' -a -prune -o -name 'feed.xml' -print

 

1四、控制查找的深度

find .-maxdepth 2 -size 3 

 

补充:Linux的权限模式为三元组“owner”,“group”,“other”,权限对应表以下

 

1五、tail文本查看命令,能够看文本的最后几行。tail命令的优势在于其内容可以与输入同步更新,很是适用于查看实时日志。

基本格式 tail [option] [filename]

  • -n number 定位参数,+5表示从第五行开始显示,10或-10表示显示最后10行

  • -f 监控文本变化,更新内容

  • -k number 从number所指的KB处开始读

范例一:tail -n -5 catalina.out 输出最后5行

 

1六、范例二:tail -f catalina.out 监听catalina.out最后行的变化并显示

 

1七、范例三:du -ah --max-depth=1显示递归的层次为1,显示全部文件和文件夹大小

 

1八、sort -t - -k 1.7 -nk 3,3 sort_k.txt
-k start,end中end能够省略,上面的1.7表示分割后第一个域的第7个字符,因为没有end,则表示对第一个域中第7字符及其以后的字符排序。而3,3则表示在前面排序的基础上,再对第三个域进行排序。

  1. sort -nk 2 -t - sort.txt 以-进行分割,对分割后的第二个域进行排序;

  2. sort -nrk 2 -t - sort.txt 逆序排序

 

1九、more命令用于显示文件的内容,与cat和tail等命令不一样的是,more命令是按页显示文件内容,同时具备搜寻字符串的功能。(因为more具备向前翻页功能,所以该命令会加载整个文件)

基本格式 more [option] [filename]

  • +n 从第n行开始显示

  • -n 定义屏幕大小为n行

  • +/pattern 再显示前按pattern匹配子串并显示

  • -s 把连续的多个空行显示为一行

    经常使用操做命令:

    • Enter 向下n行,默认为1行

    • Ctrl+F 跳过一屏

    • Ctrl+B 返回上一屏

    • 空格键 向下滚动一屏

    • = 输出当前行的行号

    • 在more模式中回车,输入/pattern能够持续向下搜索

  • more +/Deploy catalina.out
    在catalina.out文件中查找“Deploy字符第一次出现的位置”,并从该处的前两行开始显示输出

  • more +10 -10 catalina.out
    从第10行开始,每页10行

 

20、less命令与more命令对应,既能够先后翻看文件,同时还有先后搜索功能,除此以外,less在查看前不会加载整个文件。

基本格式 less [option] [filename]

  • -N 显示每行的行号

  • -i 忽略搜索时的大小写

  • -s 将连续空行显示为一行

  • -m 显示百分比

    经常使用操做命令:

    • /字符串 向下搜索“字符串”功能

    • ?字符串 向上搜索“字符串”功能

    • n 重复前一个搜索

    • 空格键 滚动一页

    • d 滚动半页

    • b 回溯一页

    • y 回溯一行

    • q 退出less命令

  • 范例一:less -Nm catalina.out
    显示行号和百分比

  • 范例二:/detail或者?detail 向前向后搜索”detail”

 

2一、find命令实用实例总结

1、基于name查询文件

# find . -name tecmint.txt

# find /home -name tecmint.txt

# find /home -iname tecmint.txt

# find / -type d -name Tecmint

# find . -type f -name tecmint.php

# find . -type f -name "*.php"                                #按照文件类型查找,后缀为.php的文件

 

2二、2、基于权限查询文件

# find . -type f -perm 0777 -print

# find / -type f ! -perm 777

# find / -perm 2644

# find / -perm 1551

# find / -perm /u=s

# find / -perm /g+s

# find / -perm /u=r

# find / -perm /a=x

# find / -type f -perm 0777 -print -exec chmod 644 {};

# find / -type d -perm 777 -print -exec chmod 755 {};

# find . -type f -name "tecmint.txt" -execrm -f {} ;

# find . -type f -name "*.txt" -exec rm -f{} ;

# find . -type f -name "*.mp3" -exec rm -f{} ;

# find /tmp -type f -empty

# find /tmp -type d -empty

# find /tmp -type f -name ".*"

 

2三、3、基于用户和组查询文件

# find / -user root -name tecmint.txt

# find /home -user tecmint

# find /home -group developer

# find /home -user tecmint -iname "*.txt"

 

2四、基于时间查询文件或目录

# find / -mtime 50

# find / -atime 50

# find / -mtime +50 –mtime -100

# find / -cmin -60

# find / -mmin -60

# find / -amin -60

 

2五、基于大小查询文件或目录

# find / -size 50M

# find / -size +50M -size -100M

# find / -size +100M -exec rm -rf {} ;

# find / -type f -name *.mp3 -size +10M -exec rm {} ;

 

2六、使用mindepth和maxdepth限定搜索指定目录的深度

在root目录及其1层深的子目录中查找passwd. (例如root —level 1, and one sub-directory — level 2)

# find -maxdepth 2-name passwd

 

在root目录下及其最大两层深度的子目录中查找passwd文件. (例如 root — level 1, and two sub-directories — level 2 and 3 )

# find / -maxdepth 3-name passwd

 

在第二层子目录和第四层子目录之间查找passwd文件。

# find -mindepth 3-maxdepth 5 -name passwd

 

2七、查找5个最大的文件

find . -type f -execls -s {} ; | sort -n -r | head -5

查找5个最小的文件

find . -type f -execls -s {} ; | sort -n  | head -5

find . -not -empty-type f -exec ls -s {} ; | sort -n  |head -5

 

2八、查找全部的隐藏文件

find . -type f -name".*"

查找全部的隐藏目录

find -type d -name".*

 

2九、下面的命令删除大于100M的*.zip文件。

find / -type f -name*.zip -size +100M -exec rm -i {} ;"

 

用别名rm100m删除全部大雨100M的*.tar文件。使用一样的思想能够建立rm1g,rm2g,rm5g的一类别名来删除全部大于1G,2G,5G的文件。

aliasrm100m="find / -type f -name *.tar -size +100M -exec rm -i {} ;"

# aliasrm1g="find / -type f -name *.tar -size +1G -exec rm -i {} ;"

# aliasrm2g="find / -type f -name *.tar -size +2G -exec rm -i {} ;"

# aliasrm5g="find / -type f -name *.tar -size +5G -exec rm -i {} ;"

 

30、将/etc/passwd,有出现 root 的行取出来

# grep root /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

# cat /etc/passwd | grep root 

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

3一、将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号

# grep -n root /etc/passwd1:root:x:0:0:root:/root:/bin/bash30:operator:x:11:0:operator:/root:/sbin/nologin

 

3二、将/etc/passwd,将没有出现 root 和nologin的行取出来

# grep -v root /etc/passwd | grep -v nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

 

3三、用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行,要将捉到的关键字显色,且加上行号来表示:

[root@localhost  ~]# dmesg | grep -n --color=auto 'eth'247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0: Identified 8139 chip type 'RTL-8139C'294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1305:eth0: no IPv6 routers present
# 你会发现除了 eth 会有特殊颜色来表示以外,最前面还有行号喔!

 

3四、根据文件内容递归查找目录

# grep ‘energywise’ *           #在当前目录搜索带'energywise'行的文件

# grep -r ‘energywise’ *        #在当前目录及其子目录下搜索'energywise'行的文件
# grep -l -r ‘energywise’ *     #在当前目录及其子目录下搜索'energywise'行的文件,可是不显示匹配的行,只显示匹配的文件

 

3五、找出空白行:

[root@localhost  ~]# grep -n '^$' regular_express.txt22:

 

3六、不想要开头是英文字母

[root@localhost  ~]# grep -n '^[^a-zA-Z]' regular_express.txt1

 

3七、想要找出来,行尾结束为小数点 (.) 的那一行:

[root@localhost  ~]# grep -n '\.$' regular_express.txt1

 

3八、『至少两个 o 以上的字串』时,就须要 ooo* ,亦便是:

[root@localhost  ~]# grep -n 'ooo*' regular_express.txt1

 

3九、想要字串开头与结尾都是 g,可是两个 g 之间仅能存在至少一个 o ,亦便是 gog, goog, gooog.... 等等,那该如何?

[root@localhost ~]# grep -n 'goo*g' regular_express.txt18

 

40、若是我想要找出 g 开头与 g 结尾的行,当中的字符无关紧要

[root@localhost ~]# grep -n 'g.*g' regular_express.txt1

 

4一、要找出 g 后面接 2 到 5 个 o ,而后再接一个 g 的字串,他会是这样:

[root@localhost  ~]# grep -n 'go\{2,5\}g' regular_express.txt18:google is the best tools for search keyword.

 

4二、想要的是 2 个 o 以上的 goooo....g 呢?除了能够是 gooo*g ,也能够是:

[root@localhost ~]# grep -n 'go\{2,\}g' regular_express.txt18

 

4三、打印全部包含NW或EA的行。若是不是使用egrep,而是grep,将不会有结果查出。

# egrep 'NW|EA' testfile    

 

4四、搜索全部包含一个或多个3的行。

# egrep '3+' testfile
# grep -E '3+' testfile
# grep '3\+' testfile  

 

4五、搜索全部包含0个或1个小数点字符的行。

# egrep '2\.?[0-9]' testfile 
# grep -E '2\.?[0-9]' testfile
# grep '2\.\?[0-9]' testfile

 

4六、搜索一个或者多个连续的no的行。

# egrep '(no)+' testfile
# grep -E '(no)+' testfile
# grep '\(no\)\+' testfile   #3个命令返回相同结果,

 

4七、若是你想在一个文件或者输出中找到包含星号字符的行

fgrep  '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

或
grep -F '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

 

4八、将/etc/passwd,有出现root的行取出来

# grep root /etc/passwd

# cat /etc/passwd | grep root 

4九、将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号

# grep -n root /etc/passwd1:root:x:0:0:root:/root:/bin/bash30:operator:x:11:0:operator:/root:/sbin/nologin

 

50、将/etc/passwd,将没有出现 root 的行取出来

# grep -v root /etc/passwdroot:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

将/etc/passwd,将没有出现 root 和nologin的行取出来

# grep -v root /etc/passwd | grep -v nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行,要将捉到的关键字显色,且加上行号来表示:

[root@localhost ~]# dmesg | grep -n --color=auto 'eth'247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0: Identified 8139 chip type 'RTL-8139C'294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1305:eth0: no IPv6 routers present
# 你会发现除了 eth 会有特殊颜色来表示以外,最前面还有行号喔!

用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行,在关键字所在行的前两行与后三行也一块儿捉出来显示

[root@localhost ~]# dmesg | grep -n -A3 -B2 --color=auto 'eth'245-PCI: setting IRQ 10 as level-triggered246-ACPI: PCI Interrupt 0000:00:0e.0[A] -> Link [LNKB] ...247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0: Identified 8139 chip type 'RTL-8139C'249-input: PC Speaker as /class/input/input2250-ACPI: PCI Interrupt 0000:00:01.4[B] -> Link [LNKB] ...251-hdb: ATAPI 48X DVD-ROM DVD-R-RAM CD-R/RW drive, 2048kB Cache, UDMA(66)
# 如上所示,你会发现关键字 247 所在的前两行及 248 后三行也都被显示出来!
# 这样可让你将关键字先后数据捉出来进行分析啦!

5一、根据文件内容递归查找目录

# grep ‘energywise’ *           #在当前目录搜索带'energywise'行的文件

# grep -r ‘energywise’ *        #在当前目录及其子目录下搜索'energywise'行的文件
# grep -l -r ‘energywise’ *     #在当前目录及其子目录下搜索'energywise'行的文件,可是不显示匹配的行,只显示匹配的文件

 

5二、字符类

符类的搜索:若是我想要搜寻 test 或 taste 这两个单字时,能够发现到,其实她们有共通的 't?st' 存在~这个时候,我能够这样来搜寻:

[root@localhost~]# grep -n 't[ae]st' regular_express.txt8:I can't finish the test.9:Oh! The soup taste good.

字符类的反向选择 [^] :若是想要搜索到有 oo 的行,但不想要 oo 前面有 g,以下 : 

[root@localhost ~]# grep -n '[^g]oo' regular_express.txt2: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!

 

5三、行首与行尾字节 ^ $

行首字符:若是我想要让 the 只在行首列出呢? 这个时候就得要使用定位字节了!咱们能够这样作:

[root@ocalhost ~]# grep -n '^the' regular_express.txt12:the symbol '*' is represented as start.

想要开头是小写字节的那一行就列出呢?能够这样:

[root@ocalhost ~]# grep -n '^[a-z]' regular_express.txt

2:apple is my favorite food.

4:this dress doesn't fit me.

10:motorcycle is cheap than car.

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

18:google is the best tools for search keyword.

19:goooooogle yes!

20:go! go! Let's go.

 

不想要开头是英文字母,则能够是这样:

[root@localhost ~]# grep -n '^[^a-zA-Z]' regular_express.txt1:"Open Source" is a good mechanism to develop programs.21:# I am VBird

想要找出来,行尾结束为小数点 (.) 的那一行:

[root@www ~]# grep -n '\.$' regular_express.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.4:this dress doesn't fit

找出空白行:

[root@www ~]# grep -n '^$' regular_express.txt22:

由于只有行首跟行尾 (^$),因此,这样就能够找出空白行啦!

须要找出 g??d 的字串,亦即共有四个字节, 起头是 g 而结束是 d ,我能够这样作:

[root@localhost ~]# grep -n 'g..d' regular_express.txt1:"Open Source" is a good mechanism to develop programs.9:Oh! The soup taste good.16:The world <Happy> is the same with "glad".

须要『至少两个 o 以上的字串』时,就须要 ooo* ,亦便是:

[root@www ~]# grep -n 'ooo*' regular_express.txt1:"Open Source" is a good mechanism to develop

想要字串开头与结尾都是 g,可是两个 g 之间仅能存在至少一个 o ,亦便是 gog, goog, gooog.... 等等,那该如何?

[root@localhost ~]# grep -n 'goo*g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!

想要找出 g 开头与 g 结尾的行,当中的字符无关紧要

[root@localhost ~]# grep -n 'g.*g' regular_express.txt1:"Open Source" is a good mechanism to develop programs.14:The gd software is a library for drafting programs.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.

想要找出『任意数字』的行?由于仅有数字,因此就成为:

[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.

假设要找到两个 o 的字串,能够是:

[root@www ~]# grep -n 'o\{2\}' regular_express.txt1:"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 ke19:goooooogle yes!

 

假设要找出 g 后面接 2 到 5 个 o ,而后再接一个 g 的字串,他会是这样:

[root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt18:google is the best tools for search keyword.

若是想要的是 2 个 o 以上的 goooo....g 呢?除了能够是 gooo*g ,也能够是:

[root@www ~]# grep -n 'go\{2,\}g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!

 

5四、扩展grep(grep -E 或者 egrep):

打印全部包含NW或EA的行。若是不是使用egrep,而是grep,将不会有结果查出。

# egrep 'NW|EA' testfile    
    northwest       NW      Charles Main        3.0     .98     3       34
    eastern         EA      TB Savage           4.4     .84     5       20

 

对于标准grep,若是在扩展元字符前面加\,grep会自动启用扩展选项-E。

#grep 'NW\|EA' testfile
northwest       NW      Charles Main        3.0     .98     3       34eastern         EA      TB Savage           4.4     .84     5       20

搜索全部包含一个或多个3的行。

# egrep '3+' testfile
# grep -E '3+' testfile
# grep '3\+' testfile        
#这3条命令将会
northwest       NW      Charles Main          3.0     .98     3       34western         WE      Sharon Gray          

搜索全部包含0个或1个小数点字符的行。

# egrep '2\.?[0-9]' testfile
# grep -E '2\.?[0-9]' testfile
# grep '2\.\?[0-9]' testfile
#首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字。
western         WE       Sharon Gray          5.3     .97

搜索一个或者多个连续的no的行。

# egrep '(no)+' testfile
# grep -E '(no)+' testfile
# grep '\(no\)\+' testfile   #3个命令返回相同结果,
northwest       NW      Charles Main        3.0     .98

若是你想在一个文件或者输出中找到包含星号字符的行

fgrep  '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

或
grep -F '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

 

5五、查找特定进程

[root@localhost ~]# ps -aux | grep mysql

###查找含有mysql关键字的进程

 

5六、搜索含有关键字的行并高亮

[root@localhost /search/nginx/html]# cat index.php | grep -n test --color=always

 

5七、从多个文件的内容中查找含有关键字的文件

[root@localhost /search/nginx/html]# grep -r p3p.sogou.com /usr/local/

 

5八、动态查看文件更新含有特定关键字的内容并高亮

[root@localhost /search/nginx/html]# tail -f /search/nginx/logs/access_log | grep favicon --color=always

 

5九、日志筛选处理 
如下命令实现了在当前目录下对access1.log文件进行查找,找到那些不包含404的行,把它们放到access2.log中,后面去掉’v’,便是把有404的行放入access2.log

[root@localhost /search/nginx/logs]# cat access_log | grep -v "404" >access_no404.log

 

60、

 

6一、

 

6二、

 

6三、

 

6四、

 

6五、

 

6六、

 

6七、

 

6八、

 

6九、

 

70、

 

7一、

 

7二、

 

 

参考连接   :  https://mp.weixin.qq.com/s/6VYDEf8Wbd28T2QMVEMp1g

Linux正则表达式范例 : https://blog.ansheng.me/article/examples-of-linux-regular-expressions/

相关文章
相关标签/搜索