Meet the Grepplies (The Search Monsters with Attitude)
AnthonyEnglish 270000RKFN Visits (4792)
That's what it is. Good ol' fashioned F-E-A-R. That's what makes those young whippersnappers keep away from the Unix command line. Oh yes, they think they're so cool with their i-gadgets ("it's all about ME!") so they can send their heartfelt "r u ok" tweets to their countless followers. But when it comes down to a shell prompt, the only command they're really comfortable with is
If they have to search for something in a file, they just do a more on it and they hope there aren't too many big words to read. If only there was an easier way to find a word that matches. Well, that's what grep is for.
So for the benefit of those poor little popkins who are reading this without the aid of multifocals, let's introduce the grep command without frightening them back into their virtual world of flash mobs and education by youtube. The best way of doing this is to make grep seem powerful but friendly, a kind of gentle monster, good for searching for things but with a bit of attitude. So, in the vain hope that anyone who has never heard of the Berlin Wall has actually read this far without detouring to Twitter, Facebook or reading a literary classic (just kidding), here we go!
Hey Boys and girls!
Meet The Grepplies!
They are four friendly monsters that will help you find things on the
big bad command line
There's Para Grep, Subtle Grep, Bouncer Grep, and last (and least), Sheepy grep.
Para grep (grep -p)
Let me introduce Para Grep. He's also known as
He'll let you jump in to the middle of a file, survey the surrounds and jump right out again, just like a paratrooper. You'd use Para Grep (he goes by the codename grep -p) to find the matching paragraph in a file. Let's see him in action:
Find the stanza for /usr/local in /etc/filesystems:
grep -p /usr/local /etc/filesystems
Get the idea?
Let's watch PG get the disk information from the ODM (What's ODM? Don't worry, you can look it up later). There are much better ways of doing this if you were to look up the odmget command, but that would take time, and you're young, and the one thing young people don't have is time, isn't it?
CuAt | grep -p hdisk3
Next in the lineup is Subtle Grep (grep -i). This guy gets his nickname because he is legendary for his insensitivity. Just the sort of guy you'd like your daughter not to bring home. Even on Unix which is case sensitive, he just tramples his way through. He can sniff out a pattern anywhere, and won't let CASE stand in his way. Shift and CAPS LOCK are not in his vocabulary. Watch Subtle Grep find every 'os' in this file:
grep -i os lati
MOSES SUPPOSES HIS TOESES ARE ROSES
BUT MOSES SUPPOSES ERRONEOUSLY
and Moses, he knowses his toeses aren't roses
AS MOSES SUPPOSES HIS TOESES TO BE.
If Subtle wasn't around to do his
work, we would have only matched line 3, which wouldn't be very
enlightening, would it?
Find all the java processes but exclude any owned by root:
ps -ef|grep java|grep -v root
In fact, that will find any process with the word 'java' in the output of 'ps -ef', even if it's somewhere in a variable or a parameter. And it will strip out any processes with the word 'root' in them, even if it's in the middle of a long word ('beetroot', 'root beer', 'Froot Loops').
This Bouncer Grep fellow comes in handy. You can eliminate strings which mess up your own search results. Like your own grep command. Let's get rid of it from the list using grep -v grep:
ps -ef | grep tivoli | grep -v grep # Bouncer Grep tries to be self-effacing
The first grep searches for 'tivoli', the second one elimates the pattern following '-v', and that pattern happens to be 'grep', which might just show up in our list of processes.
Or you can eliminate lines which start with unwelcome comments:
-v "^#" /hom
Bouncer Grep can even combine with Mr Insensitive (grep -i) to ensure that unwelcome guests don't get in:
grep -vi "taxman" /home/guest/party # No TAXMAN, or TaxMan either
grep (grep -E)
Looks like you're wearing
But not your
It didn't make the list because you only have a single purple shoe and unfortunately you searched for 'shoes', not 'shoe'.
The monsters that play together stay together
You can mix and match your grep options:
-Eiv 'taxman|burglar' /hom
Use the tools
Hey, mate, why waste your time editing a file when you only want to look at it? With grep you can window shop for a single item without having to wander inside. There are plenty of text processing commands, tools and programming languages out there, and grep itself has plenty of other options and combinations for searching (or excluding) strings, but I find these four are the ones I keep coming back to.
grep -p Paragraph match
grep -i Case insensitive
grep -v exclude pattern
grep -E either pattern
The grepplies. They're powerful monsters but if you get on the right side of them, they're pretty good friends.
Now, isn't that something to tweet about?
Incidentally: AIX Down Under is on