grep命令的使用
时间:2014-11-25 10:48 来源:linux.it.net.cn 作者:IT
grep命令执行后,终端上输出显示颜色可以加“--color=auto”的参数。
另外的两个办法是:
1.设置环境变量:
export GREP_OPTIONS="--color=auto"
2.修改.bashrc
给grep起别名,用alias设置。
alias grep='grep --color=auto'
#alias fgrep='fgrep --color=auto'
#alias egrep='egrep --color=auto'
这样,在grep执行后的标准输出就会将匹配的文件标识为彩色。
例子
grep huanxiangwu huanxiangwu.txt
//显示包含huanxiangwu的文本行
grep 404 /var/log/httpd/access_log
//搜索access_log文件里的404
grep -R VirtualHost /etc/httpd/conf*
//递归查找字符串VirtualHost
grep -Rn VirtualHost /etc/httpd/conf*
//递归查找字符串VirtualHost并找出搜索目标具体所在行
ps auwx | grep init
//显示包含init的ps命令输出行
ps auwx | grep “/[*/]”
//显示ps命令输出里方括号包含的命令,可以找出ps无法显示其选项的命令
dmesg | grep “[ ]ata/|^ata”
//检查内核环形输出链表,查找硬盘和光驱等ATA设备信息
grep –color -Rn VirtualHost /etc/httpd/conf*
//递归查找字符串VirtualHost并找出搜索目标具体所在行并显示为彩色
grep -h sshd /var/log/secure*
//不显示文件名
grep -i selinux /var/log/messages
//搜索selinux不区分大小写
grep -Rl VirtualHost /etc/httpd/conf*
//只显示包含检索字符串的文件名
grep -v “200″ /var/log/httpd/access_log*
//搜索access_log文件不显示与搜索字符串不匹配的所有行
grep 192.168.56.1 /var/log/httpd/access_log | wc -l
//搜索Apache日志文件并统计来自192.168.56.1ip的访问量 (责任编辑:IT)
grep命令执行后,终端上输出显示颜色可以加“--color=auto”的参数。 另外的两个办法是: 1.设置环境变量: export GREP_OPTIONS="--color=auto" 2.修改.bashrc 给grep起别名,用alias设置。 alias grep='grep --color=auto' #alias fgrep='fgrep --color=auto' #alias egrep='egrep --color=auto' 这样,在grep执行后的标准输出就会将匹配的文件标识为彩色。 例子 grep huanxiangwu huanxiangwu.txt //显示包含huanxiangwu的文本行 grep 404 /var/log/httpd/access_log //搜索access_log文件里的404 grep -R VirtualHost /etc/httpd/conf* //递归查找字符串VirtualHost grep -Rn VirtualHost /etc/httpd/conf* //递归查找字符串VirtualHost并找出搜索目标具体所在行 ps auwx | grep init //显示包含init的ps命令输出行 ps auwx | grep “/[*/]” //显示ps命令输出里方括号包含的命令,可以找出ps无法显示其选项的命令 dmesg | grep “[ ]ata/|^ata” //检查内核环形输出链表,查找硬盘和光驱等ATA设备信息 grep –color -Rn VirtualHost /etc/httpd/conf* //递归查找字符串VirtualHost并找出搜索目标具体所在行并显示为彩色 grep -h sshd /var/log/secure* //不显示文件名 grep -i selinux /var/log/messages //搜索selinux不区分大小写 grep -Rl VirtualHost /etc/httpd/conf* //只显示包含检索字符串的文件名 grep -v “200″ /var/log/httpd/access_log* //搜索access_log文件不显示与搜索字符串不匹配的所有行 grep 192.168.56.1 /var/log/httpd/access_log | wc -l //搜索Apache日志文件并统计来自192.168.56.1ip的访问量 (责任编辑:IT) |