Grep tips
From Noah.org
Jump to navigationJump to searchsearch multiple patterns at once
You can grep for multiple patterns simultaneously without using extended regex sytax (--extended-regexp). Instead you can use multiple -e options. Each -e specifies a separate expression to match and `grep` will match each expression individually -- like an "or" subpattern.
For example the following greps for "ssh" in a `ps` listing, but also includes the column header which would otherwise be lost if you only grepped for "ssh":
# ps axwwo pid,ppid,etime,euser,cmd | grep -i -e ^\\s*PID -e ssh PID PPID ELAPSED EUSER CMD 5167 1 5-21:11:30 root /usr/sbin/sshd 6339 6291 5-21:11:06 noah /usr/bin/ssh-agent x-session-manager 18562 1 4-01:16:17 noah xterm -e ssh noah@web10 18563 18562 4-01:16:17 noah ssh noah@web10
This is equivalent to:
# ps axwwo pid,ppid,etime,euser,cmd | grep -i --extended-regexp ^\\s*PID\|ssh
The first form using multiple -e options makes it easier to add on extra patterns without having to append it to an existing extended regex pattern. This makes it easy to make a nice 'psg' alias (ps grep):
alias psg='ps axwwo pid,ppid,pcpu,pmem,stat,etime,euser,cmd | grep -i -e ^\\s*PID -e '