grep root /etc/passwd #单个文件查找
grep root /etc/passwd /etc/shadow /etc/gshadow #多个文件查找
grep -l oracle /etc/passwd /etc/shadow /etc/fstab /etc/mtab #-l 参数:在 4 个文件中查找 oracle 字符
grep -n root /etc/passwd #查找文件匹配字符的行号
grep -v root /etc/passwd #输出不包含字符串的行
grep ^root /etc/passwd #^ 输出以字符串开头的行
grep bash$ /etc/passwd #$ 输出以字符串结尾的行
grep -r root /etc/ #-r 递归查找指定字符串
grep ^$ ./.bash_profile #^$ 查找文件中的空行,如果没有空行,将不输出任何内容
grep -i roOT /etc/passwd #忽略查找字符串的大小写
grep -e "root" -e "oracle" /etc/passwd #查找多个字符串
grep -f grep_pattern /etc/passwd #用文件指定待查找的模式
创建 grep_pattern 文件,输入以下内容:
linuxtechi
root
false$
grep -c -f grep_pattern /etc/passwd #匹配到计算的数量
grep -B 4 "root" /etc/passwd #使用-B参数输出匹配行的前4行
grep -A 4 "root" /etc/passwd #使用-A参数输出匹配行的后4行
grep -C 4 "root" /etc/passwd #使用-C参数输出匹配行的前后各4行
grep -vE '^#' ./.bash_profile #过滤前面带 # 号的行
grep -vE '^#|^$' ./.bash_profile #过滤前面带 # 号的行,并且 $ 去掉空行