grep 详解
linux中的grep命令主要用于全局搜索,grep家族有三个:grep,egrep,fgerp其中fgerp不支持正则表达式。如果需要搜素字符串,不需要使用元字符,可以用fgerp比较快,grep 文本过滤工具或文本匹配工具,能够实现根据指定的“模式“逐行搜索文件内容,并将匹配到的行显示出来。并不一定是全部匹配。可以与正则表达式的元字符,其他字符组成的匹配条件。
1、命令格式:
grep [option] … ‘PATTERN’ 文件名……………. [option]选项:--color :显示颜色。-v :显示没有被模式匹配到的行,取反。-o :只显示被模式匹配到的字符串。-i :不考虑大小写。-n :顺便输出行号。
2、基本应用
[[email protected] ~]# grep -o "root" /etc/passwd [[email protected] ~]# grep ‘dwzhang‘ /etc/passwd [[email protected] ~]# grep "\$?" ypbind [[email protected] ~]# grep ‘dwzhang‘ /etc/passwd[[email protected] ~]# grep ‘cpu‘ /proc/cpuinfo //只显示包含cpu的字符[[email protected] ~]# grep --color=auto ‘cpu‘ /proc/cpuinfo [[email protected] ~]# alias grep=‘grep --color‘ [[email protected] ~]# grep ‘dwzhang‘ /etc/passwd[[email protected] ~]# ifconfig | grep ‘inet addr‘ //只显示inet addr项3、结合cut搜索
[[email protected] ~]# grep ‘dwzhang‘ /etc/passwd | cut -d: -f1[[email protected] ~]# grep ‘dwzhang‘ /etc/passwd | cut -d: -f2[[email protected] ~]# ifconfig | grep ‘inet addr‘ | cut -d: -f1[[email protected] ~]# ifconfig | grep ‘inet addr‘ | cut -d: -f2[[email protected] ~]# ifconfig | grep ‘inet addr‘ | cut -d: -f2 | cut -d‘ ‘ -f1
4、结合正则表达式元字符搜索[[email protected] ~]# grep ‘c.u‘ /proc/cpuinfo //匹配c什么p,单个字符[[email protected] ~]# grep --color ‘c..‘ /proc/cpuinfo [[email protected] ~]# grep ‘r..t‘ /etc/passwd[[email protected] ~]# grep ‘c..[a-z]‘ /proc/cpuinfo //跟了任意2个字符,后面跟了a-z小写字母的[[email protected] ~]# grep ‘^i‘ /etc/inittab //以i开头的行[[email protected] ~]# grep ‘w$‘ /etc/inittab //行尾为w的行[[email protected] ~]# grep ‘b..h$‘ /etc/passwd //b什么h结尾的[[email protected] ~]# grep ‘\([0-9]\).*\1$‘ inittab[[email protected] ~]# grep ‘^\([0-9]\).*\1$‘ inittab [[email protected] ~]# grep ‘^fpu‘ /proc/cpuinfo //显示匹配的fpu开头的行[[email protected] ~]# grep -A 2 ‘^fpu‘ /proc/cpuinfo //显示匹配的行和下面的2行[[email protected] ~]# grep --color -B 2 ‘^fpu‘ /proc/cpuinfo //显示匹配的行和上面的2行[[email protected] ~]# grep --color -C 2 ‘^fpu‘ /proc/cpuinfo //显示匹配的行和上下面的2行 linux中的grep命令主要用于全局搜索,grep家族有三个:grep,egrep,fgerp其中fgerp不支持正则表达式。如果需要搜素字符串,不需要使用元字符,可以用fgerp比较快,grep 文本过滤工具或文本匹配工具,能够实现根据指定的“模式“逐行搜索文件内容,并将匹配到的行显示出来。并不一定是全部匹配。可以与正则表达式的元字符,其他字符组成的匹配条件。
1、命令格式:
grep [option] … ‘PATTERN’ 文件名……………. [option]选项:--color :显示颜色。-v :显示没有被模式匹配到的行,取反。-o :只显示被模式匹配到的字符串。-i :不考虑大小写。-n :顺便输出行号。
2、基本应用
[[email protected] ~]# grep -o "root" /etc/passwd [[email protected] ~]# grep ‘dwzhang‘ /etc/passwd [[email protected] ~]# grep "\$?" ypbind [[email protected] ~]# grep ‘dwzhang‘ /etc/passwd[[email protected] ~]# grep ‘cpu‘ /proc/cpuinfo //只显示包含cpu的字符[[email protected] ~]# grep --color=auto ‘cpu‘ /proc/cpuinfo [[email protected] ~]# alias grep=‘grep --color‘ [[email protected] ~]# grep ‘dwzhang‘ /etc/passwd[[email protected] ~]# ifconfig | grep ‘inet addr‘ //只显示inet addr项3、结合cut搜索
[[email protected] ~]# grep ‘dwzhang‘ /etc/passwd | cut -d: -f1[[email protected] ~]# grep ‘dwzhang‘ /etc/passwd | cut -d: -f2[[email protected] ~]# ifconfig | grep ‘inet addr‘ | cut -d: -f1[[email protected] ~]# ifconfig | grep ‘inet addr‘ | cut -d: -f2[[email protected] ~]# ifconfig | grep ‘inet addr‘ | cut -d: -f2 | cut -d‘ ‘ -f1
4、结合正则表达式元字符搜索[[email protected] ~]# grep ‘c.u‘ /proc/cpuinfo //匹配c什么p,单个字符[[email protected] ~]# grep --color ‘c..‘ /proc/cpuinfo [[email protected] ~]# grep ‘r..t‘ /etc/passwd[[email protected] ~]# grep ‘c..[a-z]‘ /proc/cpuinfo //跟了任意2个字符,后面跟了a-z小写字母的[[email protected] ~]# grep ‘^i‘ /etc/inittab //以i开头的行[[email protected] ~]# grep ‘w$‘ /etc/inittab //行尾为w的行[[email protected] ~]# grep ‘b..h$‘ /etc/passwd //b什么h结尾的[[email protected] ~]# grep ‘\([0-9]\).*\1$‘ inittab[[email protected] ~]# grep ‘^\([0-9]\).*\1$‘ inittab [[email protected] ~]# grep ‘^fpu‘ /proc/cpuinfo //显示匹配的fpu开头的行[[email protected] ~]# grep -A 2 ‘^fpu‘ /proc/cpuinfo //显示匹配的行和下面的2行[[email protected] ~]# grep --color -B 2 ‘^fpu‘ /proc/cpuinfo //显示匹配的行和上面的2行[[email protected] ~]# grep --color -C 2 ‘^fpu‘ /proc/cpuinfo //显示匹配的行和上下面的2行
例子:1、显示文件去除掉空白行:[[email protected] ~]# grep -v "^$" /etc/httpd/conf/httpd.conf2、显示文件去除掉#注释行:[[email protected] ~]# grep -v "#" /etc/httpd/conf/httpd.conf3、显示去除掉注释行和空白行:[[email protected] ~]# grep -v "#" /etc/httpd/conf/httpd.conf | grep -v "^$"
例子:1、显示文件去除掉空白行:[[email protected] ~]# grep -v "^$" /etc/httpd/conf/httpd.conf2、显示文件去除掉#注释行:[[email protected] ~]# grep -v "#" /etc/httpd/conf/httpd.conf3、显示去除掉注释行和空白行:[[email protected] ~]# grep -v "#" /etc/httpd/conf/httpd.conf | grep -v "^$"
来源: http://www.bubuko.com/infodetail-2032371.html