[20170828]grep过滤技巧.txt
--//经常使用grep过滤显示信息.
# ps -ef |grep oraagent
oracle 13416 1 0 2016 ? 1-20:48:04 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 17423 1 0 2014 ? 2-03:32:14 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 21718 1 0 2014 ? 1-16:45:43 /u01/app/11.2.0.4/grid/bin/oraagent.bin
root 87599 66070 0 09:38 pts/0 00:00:00 grep oraagent
--//这样有一个小小的遗憾,把执行grep oraagent信息也带了出来,许多人常用的方法是
# ps -ef |grep oraagent | grep -v grep
oracle 13416 1 0 2016 ? 1-20:48:05 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 17423 1 0 2014 ? 2-03:32:15 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 21718 1 0 2014 ? 1-16:45:43 /u01/app/11.2.0.4/grid/bin/oraagent.bin
--//实际上解决很简单.修改执行如下:
# ps -ef |grep oraagen[t]
oracle 13416 1 0 2016 ? 1-20:48:04 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 17423 1 0 2014 ? 2-03:32:15 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 21718 1 0 2014 ? 1-16:45:43 /u01/app/11.2.0.4/grid/bin/oraagent.bin
--//实际grep过滤显示信息还是oraagent.而这样grep 命令的执行是grep oraagen[t].这样不满足过滤条件oraagent,就能显示需要的信
--//息.
--//以下别人问的问题,加入想显示ps -ef 的头.一般改用egrep.
# ps -ef |egrep "oraagent|UI[D]"
UID PID PPID C STIME TTY TIME CMD
oracle 13416 1 0 2016 ? 1-20:48:08 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 17423 1 0 2014 ? 2-03:32:16 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 21718 1 0 2014 ? 1-16:45:45 /u01/app/11.2.0.4/grid/bin/oraagent.bin
root 111809 66070 0 09:51 pts/0 00:00:00 egrep oraagent|UI[D]
--//但是这样这样又把egrep oraagent|UI[D]给带了出来.实际上如果你定义grep相关环境变量,看看显示的颜色就明白问题在那里了.
# env | grep -i grep_
GREP_COLOR=01;32
GREP_OPTIONS=--color=auto
--//这样在显示上oraagent显示绿色.修改如下:
# ps -ef |egrep "oraagen[t]|UI[D]"
UID PID PPID C STIME TTY TIME CMD
oracle 13416 1 0 2016 ? 1-20:48:09 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 17423 1 0 2014 ? 2-03:32:17 /u01/app/11.2.0.4/grid/bin/oraagent.bin
grid 21718 1 0 2014 ? 1-16:45:45 /u01/app/11.2.0.4/grid/bin/oraagent.bin
--//这样就ok了,也许还有许多方法可以做到!^_^.`