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
dev = /dev/fslv03
vfs = jfs2
log = /dev/hd8
mount = true
options = rw
account = false
quota = no
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
name = "hdisk3"
attribute = "unique_id"
value = "483333213600507680192011CF80000000000027504214503IBMfcp05VDASD03AIXvscsi"
type = "R"
generic = ""
rep = "n"
nls_index = 0
name = "hdisk3"
attribute = "pv"
value = "yes"
type = "R"
generic = "U"
rep = "sl"
nls_index = 0
name = "hdisk3"
attribute = "pvid"
value = "00c5a47e8a083a270000000000000000"
type = "R"
generic = "D"
rep = "s"
nls_index = 11
Subtle grep (grep -i)
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 latin_translation_practice.txt
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?
Bouncer grep (grep -v)
Then we get to Bouncer Grep. (grep -v) He's got a hit list of patterns that he won't let in the door. If your name is on that list, it could get ugly. Don't expect to get invited in for tea and biscuits.
Find all the java processes but exclude any owned by root:
ps -ef|grep java|grep -v root
pconsole 5898344 3145940 0 Oct 11 - 1:47 /usr/java5/bin/java -Xmx512m -Xms20m -Xscmx10m -Xshareclasses -Dfile.encoding=UTF-8 -Xbootclasspath/a:/pconsole/lwi/runtime/core/eclipse/plugins/com.ibm.rcp.base_18.104.22.16891117-1800/rcpbootcp.jar:/pconsole/lwi/lib/ISCJaasModule.jar:/pconsole/lwi/lib/com.ibm.logging.icl_1.1.1.jar:/pconsole/lwi/lib/jaas2zos.jar:/pconsole/lwi/lib/jaasmodule.jar:/pconsole/lwi/lib/lwinative.jar:/pconsole/lwi/lib/lwinl.jar:/pconsole/lwi/lib/lwirolemap.jar:/pconsole/lwi/lib/lwisecurity.jar:/pconsole/lwi/lib/lwitools.jar:/pconsole/lwi/lib/passutils.jar -Xverify:none -cp eclipse/launch.jar:eclipse/startup.jar:/pconsole/lwi/runtime/core/eclipse/plugins/com.ibm.rcp.base_22.214.171.12491117-1800/launcher.jar com.ibm.lwi.LaunchLWI
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:
grep -v "^#" /home/guest/complaints_book
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)
Last (and least) is Sheepy grep (grep -E). He can't make up his mind what he wants, so he just follows the crowd. Ever find you're getting dressed and can't choose between green or blue? That's Sheepy for you. He'll find anything that matches this or that or something else.
Watch Sheepy Grep search through your list of clothes to find out what you should wear today to make a truly positive fashion statement. Who said Unix wasn't user friendly?
grep -E 'green|blue|orange|belt|shoes' /home/wardrobe
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:
grep -Eiv 'taxman|burglar' /home/guest/invitees # Be selective
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