styxmx 发表于 2018-8-25 12:39:26

linux shell 之grep 命令

  grep 示例:
   PATTERN
    root@runingday:~# grep 'root' /etc/passwd  
    root:x:0:0:root:/root:/bin/bash
  
    root@runingday:~# grep "$USER" /etc/passwd
  
    root:x:0:0:root:/root:/bin/bash
  
    root@runingday:~# grep `whoami` /etc/passwd
  
    root:x:0:0:root:/root:/bin/bash
  
    root@runingday:~# grep '$USER' /etc/passwd
  参数:
  --color=auto:颜色显示搜索到的内容
  -i:    忽略字符大小写
  -n: 显示匹配的行号   grep -n root /etc/passwd
  -o: 仅显示匹配到的字符串 grep -o 'root' /etc/passwd
  -q: 静默模式,不输出任何信息,通过echo $?来判断是否找到
  -A #:after, 后#行,匹配到的行,及匹配到的行的后#行
      root@runingday:~# grep -n -A 3 root /etc/passwd  
      1:root:x:0:0:root:/root:/bin/bash
  
      2-daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  
      3-bin:x:2:2:bin:/bin:/usr/sbin/nologin
  
      4-sys:x:3:3:sys:/dev:/usr/sbin/nologin
  -B #:before前#行,匹配到的行,及匹配到的行的前三行
      root@runingday:~# grep -n -B 4 mysql /etc/passwd  
      33-hplip:x:114:7:HPLIP system user,,,:/var/run/hplip:/bin/false
  
      34-pulse:x:115:122:PulseAudio daemon,,,:/var/run/pulse:/bin/false
  
      35-runingday:x:1000:1000:runingday,,,:/home/runingday:/bin/bash
  
      36-sshd:x:116:65534::/var/run/sshd:/usr/sbin/nologin
  
      37:mysql:x:117:125:MySQL Server,,,:/nonexistent:/bin/false
  -C #: context前后各#行
      root@runingday:~# grep -n -C 2 syslog /etc/passwd  
      18-nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
  
      19-libuuid:x:100:101::/var/lib/libuuid:
  
      20:syslog:x:101:104::/home/syslog:/bin/false
  
      21-messagebus:x:102:106::/var/run/dbus:/bin/false
  
      22-usbmux:x:103:46:usbmux daemon,,,:/home/usbmux:/bin/false
  -e: 实现各个选项间的逻辑or关系
      root@runingday:~# grep -e 'root' -e 'syslog' /etc/passwd  
      root:x:0:0:root:/root:/bin/bash
  
      syslog:x:101:104::/home/syslog:/bin/false
  -w: 整行匹配整个单词
      root@runingday:~# grep -w oo /etc/passwd  
      root@runingday:~# grep -w root /etc/passwd
  
      root:x:0:0:root:/root:/bin/bash
  -E: 使用ERE 扩展正则表达式

[*]  元字符分类
  字符匹配

[*]

[*]  . 匹配任意单个字符
[*]  [] 匹配指定范围内的任意单个字符
[*]  [^]匹配指定范围外的任意单个字符
[*]  [:digit:] 所有数字
[*]  [:lower:] 所有小写字母
[*]  [:upper:] 所有大写字母
[*]  [:alpha:] 所有字母,包括大小写
[*]  [:alnum:] 所有字母和数字
[*]  [:punct:] 所有标点符号
[*]  [:space:] 空格和Tab

  匹配次数

[*]

[*]  * 匹配前面的字符任意次,包括0次
[*]  .* 任意长度的任意字符
[*]  \?匹配前面的字符0或1次
[*]  \+匹配其前面的字符至少1次
[*]  \{m\}匹配前面的字符m次
[*]  \{m,n\}匹配前面的字符至少m次,至多n次
[*]  \{,n\}匹配前面的字符至多n次
[*]  \{m,\}匹配前面的字符至少m次

  行首锚定

[*]

[*]  ^$匹配空行

[*]

[*]  ^[[:space:]]*$ 空白行
[*]  ^行首锚定,用于模式的最左侧
[*]  $行尾锚定,用于模式的最右侧
[*]  \或\b词尾锚定,用于单词模式的右侧
[*]  \匹配整个单词
[*]  ^PATTERN$用于模式匹配整行

  练习:

[*]  显示当前系统root、runingday或syslog用户的UID和默认shell
root@runingday:~# grep "^\" /etc/passwd  
root:x:0:0:root:/root:/bin/bash
  
syslog:x:101:104::/home/syslog:/bin/false
  
runingday:x:1000:1000:runingday,,,:/home/runingday:/bin/bash
  

  
通过egrep
  
root@runingday:~# egrep "^\" /etc/passwd
  
root:x:0:0:root:/root:/bin/bash
  
syslog:x:101:104::/home/syslog:/bin/false
  
runingday:x:1000:1000:runingday,,,:/home/runingday:/bin/bash
  

  
root@runingday:~# grep "^\" /etc/passwd | cut -d: -f1,3,7
  
root:0:/bin/bash
  
syslog:101:/bin/false
  
runingday:1000:/bin/bash
  找出/etc/rc.d/init.d/functions 文件中某单词(包括下划线)后面跟一个小括号的行
grep -o "[[:alnum:]_]\+\>[[:space:]]*()" /etc/rc.d/init.d/functions  
egrep -o "[[:alnum:]_]+\>[[:space:]]*\(\)" /etc/rc.d/init.d/functions
  使用egrep取出/etc/rc.d/init.d/functions中某基名
echo "/etc/rc.d/init.d/functions"| egrep -o "[[:alnum:]]+$"  
echo "/etc/rc.d/init.d/functions/" | egrep -o "[^/]+/?$"
  使用egrep取出上面路径的目录名
echo "/etc/rc.d/init.d/functions" | egrep -o .*[^/] | grep -o ".*\/"  找出ifconfig命令结果中本机的IPV4地址
ifconfig enp0s3 | grep "inet\>" | tr -s " " | cut -d" " -f3  显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方式)
grep -i ^s /proc/meminfo  
grep ^ /proc/meminfo
  
grep -e ^s -e ^S /proc/meminfo
  
grep -E "^(s|S)" /proc/meminfo
  显示/etc/passwd文件中不以/bin/bash结尾的行
grep -v /bin/bash$ /etc/passwd  显示/etc/passwd文件中ID号最大的用户的用户名
# cut -d: -f1,3 /etc/passwd | sort -t: -k2 -n | tail -1  
nfsnobody:65534
  
# cut -d: -f1,3 /etc/passwd | sort -t: -k2 -n | tail -1 | cut -d: -f1
  
nfsnobody
  显示用户rpc默认的shell程序
grep "\" /etc/passwd | cut -d: -f7  找出/etc/passwd中两位或三位数
grep-o "\" /etc/passwd  显示/etc/grub2.cfg文件中,至少以一个空白符开头且后面存非空白字符的行
grep "^[[:space:]]\+[^[:space:]]\+" /etc/grub2.cfg  找出netstat -tan 命令结果中以LISTEN后跟0、1或多个空白字符结尾的行netstat -tan | grep -i "listen[[:space:]]*$"添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行
grep '^\([[:alnum:]]\+\>\).*\1$' /etc/passwd  
egrep "^([[:alnum:]]+\>).*\1$" /etc/passwd


页: [1]
查看完整版本: linux shell 之grep 命令