Difference between revisions of "Grep tips"
Line 25: | Line 25: | ||
<pre> | <pre> | ||
alias psg='ps axwwo pid,ppid,pcpu,pmem,stat,etime,euser,cmd | grep -i -e ^\\s*PID -e ' | alias psg='ps axwwo pid,ppid,pcpu,pmem,stat,etime,euser,cmd | grep -i -e ^\\s*PID -e ' | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | == display all lines but highlight matches == | ||
+ | |||
+ | grep highlight match show everything | ||
+ | grep display all but highlight match | ||
+ | |||
+ | Sometimes I want to display a file or output from watching a log with tail -f and highlight pattern matches. I want to actually display all lines. | ||
+ | |||
+ | I have not figured out a way to do this with grep, but there is a similar tool called [http://petdance.com/ack/ ack] or `ack-grep`. It's perl script that works much like grep. The '--passthru' option tells it to print every line even if there is no match. The --color option tells it to color matches even if the output is redirected to a file or pipe. This is useful for piping to a pager. | ||
+ | |||
+ | Highlight all bash shells running: | ||
+ | |||
+ | <pre> | ||
+ | ps auxww --forest | ack-grep --passthru --color bash | ||
+ | </pre> | ||
+ | |||
+ | Highlight requests from 127.0.0.1 in an httpd log: | ||
+ | |||
+ | <pre> | ||
+ | tail -f /var/www/logs/access_log | ack --passthru --color 127.0.0.1 | ||
</pre> | </pre> |
Revision as of 18:30, 13 December 2008
search 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 '
display all lines but highlight matches
grep highlight match show everything grep display all but highlight match
Sometimes I want to display a file or output from watching a log with tail -f and highlight pattern matches. I want to actually display all lines.
I have not figured out a way to do this with grep, but there is a similar tool called ack or `ack-grep`. It's perl script that works much like grep. The '--passthru' option tells it to print every line even if there is no match. The --color option tells it to color matches even if the output is redirected to a file or pipe. This is useful for piping to a pager.
Highlight all bash shells running:
ps auxww --forest | ack-grep --passthru --color bash
Highlight requests from 127.0.0.1 in an httpd log:
tail -f /var/www/logs/access_log | ack --passthru --color 127.0.0.1