grep
程序员文章站
2024-02-23 23:21:54
...
grep家族
========================================================
grep: 在文件中全局查找指定的正则表达式,并打印所有包含该表达式的行
egrep: 扩展的egrep,支持更多的正则表达式元字符
fgrep: 固定grep(fixed grep),有时也被称作快速(fast grep),它按字面解释所有的字符
一、grep命令格式
grep [选项] PATTERN filename filename ...
# grep 'Tom' /etc/passwd
# grep 'bash shell' /etc/test
找到: grep返回的退出状态为0
没找到: grep返回的退出状态为1
找不到指定文件: grep返回的退出状态为2
grep 程序的输入可以来自标准输入或管道,而不仅仅是文件,例如:
# grep 'tom'
# ps aux |grep 'sshd'
# ll |grep '^d'
# grep 'alice' /etc/passwd /etc/shadow /etc/group
二、grep使用的元字符
grep: 使用基本元字符集 ^, $, ., *, [], [^], \< \>,\(\),\{\}
egrep(或grep -E): 使用扩展元字符集 ?, +, { }, |, ( )
注:grep也可以使用扩展集中的元字符,仅需要对这些元字符前置一个反斜线
\w 所有字母与数字,称为字符[a-zA-Z0-9] 'l[a-zA-Z0-9]*ve' 'l\w*ve'
\W 所有字母与数字之外的字符,称为非字符 'love[^a-zA-Z0-9]+' 'love\W+'
\b 词边界 '\<love\>' '\blove\b'
三、grep 示例
grep -E 或 egrep
# egrep 'NW' datafile
# egrep 'NW' d*
# egrep '^n' datafile
# egrep '4$' datafile
# egrep TB Savage datafile
# egrep 'TB Savage' datafile
# egrep '5\..' datafile
# egrep '\.5' datafile
# egrep '^[we]' datafile
# egrep '[^0-9]' datafile
# egrep '[A-Z][A-Z] [A-Z]' datafile
# egrep 'ss* ' datafile
# egrep '[a-z]{9}' datafile
# egrep '\<north' datafile
# egrep '\<north\>' datafile
# egrep '\<[a-r].*n\>' datafile
# egrep '^n\w*\W' datafile
# egrep '\bnorth\b' datafile
# egrep 'NW|EA' datafile
# egrep '3+' datafile
# egrep '2\.?[0-9]' datafile
# egrep '(no)+' datafile
# egrep 'S(h|u)' datafile
# egrep 'Sh|u' datafile
三、grep选项
-i, --ignore-case 忽略大小写
-l, --files-with-matches 只列出匹配行所在的文件名
-n, --line-number 在每一行前面加上它在文件中的相对行号
-c, --count 显示成功匹配的行数
-s, --no-messages 禁止显示文件不存在或文件不可读的错误信息
-q, --quiet, --silent 静默--quiet, --silent
-v, --invert-match 反向查找,只显示不匹配的行
-R, -r, --recursive 递归针对目录
--color 颜色
-o, --only-matching 只显示匹配的内容
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
示例:
[[email protected] ~]# egrep 'ifcfg' /etc/* 文件
[[email protected] ~]# grep -R 'ifcfg' /etc 目录
[[email protected] ~]# egrep 'root' /etc/passwd /etc/shadow /etc/hosts
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:root:$6$gcO6Vp4t$OX9LmVgpjtur67UQdUYfw7vJW.78.uRXCLIxw4mBk82Z99:7:::
[[email protected] ~]# egrep -l 'root' /etc/passwd /etc/shadow /etc/hosts
/etc/passwd
/etc/shadow
[[email protected] ~]# egrep -n 'root' /etc/passwd /etc/shadow /etc/hosts
/etc/passwd:1:root:x:0:0:root:/root:/bin/bash
/etc/passwd:11:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:1:root:$6$gcO6Vp4t$OX9LmVgpjtur67UQdUy8.M78.uRXCLIxw4mBk82ZrNlxyf54
[[email protected] ~]# egrep '54:04:A6:CE:C2:1F' /etc/sysconfig/*
[[email protected] ~]# egrep -R '54:04:A6:CE:C2:1F' /etc/sysconfig/
[[email protected] ~]# egrep '^IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth0 |egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
192.168.2.254
[[email protected] ~]# egrep '^IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth0 |egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'
192.168.2.254
[[email protected] ~]# grep --help |grep '\-R'
-R, -r, --recursive equivalent to --directories=recurse
[[email protected] ~]# grep --help |egrep -A5 '\-R'
-R, -r, --recursive equivalent to --directories=recurse
--include=FILE_PATTERN search only files that match FILE_PATTERN
--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
-L, --files-without-match print only names of FILEs containing no match
作业:
判断输入的是否是合法的IP的地址
========================================================
[[email protected] ~]# cat /etc/passwd |grep -E '^(root|alice)'
root:x:0:0:root:/root:/bin/bash
alice:x:1000:1000:alice:/home/alice:/bin/bash
[[email protected] ~]# cat /etc/passwd |grep '^(root|alice)'
root:x:0:0:root:/root:/bin/bash
alice:x:1000:1000:alice:/home/alice:/bin/bash