linux shell grep -c,linux shell 之grep 命令

grep 示例:

[OPTIONS] PATTERN [FILE...]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和默认shellroot@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 ^[sS] /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号最大的用户的用户名[root@localhost ~]# cut -d: -f1,3 /etc/passwd | sort -t: -k2 -n | tail -1

nfsnobody:65534

[root@localhost ~]# 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值