Loading
Home ›
Get the grep Out!
Oct 24, 2007 By admin
While using grep to search for a running process from a ps ax output, it also includes the grep command, but in some cases (for example in a script) this is undesirable. To have a 'clean' output you can use brackets in this way for the grep command:
$ ps ax | grep s[s]h 4920 ? Ss 0:00 /usr/sbin/sshd $
This Tech Tip was brought to us by Alessandro in Italy. Thanks, Alessandro!
Instant fame is easy at Linux Journal. Just send us your useful Tech Tips to share with the Linux Community, and we'll send you a cool t-shirt for your efforts!
Please note: Tech Tips featured in this specific section of LinuxJournal.com are kindly brought to us by readers and are not necessarily tested by LinuxJournal.com editors.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Parallel Programming with NVIDIA CUDA
- Readers' Choice Awards 2011
- 100% disappointed with the decision to go all digital.
- Linux-Based X Terminals with XDMCP
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- The Linux powered LAN Gaming House
- Why Python?
- Python for Android
- Employment Posters
4 hours 41 min ago - Sure the best distro is
6 hours 1 min ago - BeOS was the best
8 hours 45 min ago - I use Wireshark on a daily
13 hours 16 min ago - buena información
18 hours 22 min ago - One important "bucket" that I didn't note (désolé si qqun deja d
19 hours 23 min ago - Gnome3 is such a POS. No one
1 day 4 hours ago - Gnome 3 is the biggest POS
1 day 5 hours ago - I didn't knew this thing by
1 day 11 hours ago - Author's reply
1 day 14 hours ago





Comments
pgrep
I find pgrep to be really useful for identifying processes,
pgrep ssh
3161
3815
3957
4098
4168
4320
7089
9647
19727
and then you can do fun stuff like,
for p in $(pgrep ssh) ; do
ps -p $p -f --no-headers
done
and this eliminates the need to do any tweeks to avoid grepping grep.
:wq
How does this work? I type
How does this work?
I type "ps -ef | grep g[e]tty" and get this:
So it seems to work.
Now, if I type "ps -ef | grep getty >xxx" and then "cat xxx" I get this:
As you would expect, xxx contains the grep command. Now, I type "cat xxx | grep g[e]tty" and find that it gives me:
Why does the same trick not work?
Frankly, I cannot see how the square brackets trick works. Can you enlighten me please.
Dumb Me
Sorry, I am dumb. I now see how the grep 'trick' works.