These days, you can’t throw a USB stick without hitting something that’s running Linux. It might be a phone, an embedded device, or your TV. Either way, it’s running Linux, and somewhere along the line of the development of whatever your USB stick smacked into, somebody used the Global Regular Expression Print utility- better known as Grep. But what is Grep, and why do you need it? [Anton Zhiyanov] not only answers those questions but provides Grep by example: Interactive Guide to help you along.
To understand Linux, one must understand its commercial predecessor, Unix. One of the things that made Unix (and then Linux) unique was its philosophy: Write programs that work together, do one thing well, and handle text streams. This philosophy describes a huge number of programs, and one of these programs is Grep. It’s installed everywhere there’s a *nix installed, and once one becomes familiar with it, their command-line-fu reaches an all new level.
At its core, Grep is simply a bloodhound. It’s scent? A magical incantation called Regular Expressions. Regular Expressions (aka Regex) are simply a way of describing what a stream of text should look like. So when you feed Grep a bit of Regular Expression, it Prints only the text that matches that expression. Neat, right?
The trouble is that Regex can be kind of hard, and Grep has various versions and capabilities that need to be learned. And this is where the article shines- it covers both in an excellent interactive tutorial that’ll help you become a Grep Guru in no time. And if you want to do a deeper dive, check out what it takes to make your own Regex Engine from scratch!
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
– Jamie Zawinski
The same joke is made about AWK. (Awk!!) 😁
The G in grep does not stand for GNU; it stands for Global. Grep existed in Unix before the GNU project started.
Right you are; Article updated accordingly. Thanks for the comment!
wait… in unix’ v6 or v7 book there is a description of all these commands… isn’t that enough? just saying…
grep was created to fit an observed need. Users of the Unix ed(1) command very frequently issued the g/re/p command to print off all lines that contained the soecified regular expression.
Someone xtracted that codepath and made it into its own command, because of this use case.
[quote]Someone xtracted that codepath[/quote]
Apparently, that “someone” was Ken Thompson.
About 25 or so years ago(maybe) someone wrote or ported grep to MSDOS, along with a half-dozen other Unix utilities, which offhand I cannot remember, as a training aid for those wishing to learn *nix. I found it to be a highly useful tool, and used it as a poor man’s primitive data base tool to make a simple address book. I simply typed up a text file of the entire address book clients info in one file, and named it rdex.txt. When I wanted to look up a customer I i simply grepped a term(name, address, or other) into the file and it would bring up the entire listing for that client. To make changes to the base, I would simply text-edit the file. Not sophisticated but it did what I needed to do, was cross-platform, and was a nice little exercise. Best if all, it was a product of my own limited imagination, though I’m sure it’s been done before and since.
So the whole tutorial uses Perl regular expressions?
Sorry, I’m old skool, and learned it proper.
When I get close to failing at Wordle grep saves me.
Yet another thing that can be simply done in a PERL 5 script – if you live long enough to become proficient in PERL 5 that is.
I garrulously gripe as I get a grip and grind away on your great and gracious story about grep.