Polishing the wegrep Wrapper Script

When last I discussed shell scripts, I was presenting a shell script that offered an alternative to the -C context flag in GNU grep. Although most modern Linux systems have the more capable grep command, older systems likely don't have this particular feature, and it's also a good excuse to dig into working with wrapper scripts too.

"Wait. What's a wrapper script?" I can hear you ask, and some of you also are now trying to think of a famous rapper whose name you can reference for a punny response. I've already beat you there: "Can't touch that!"

A wrapper is a script that replaces a command on the Linux system but secretly calls the command, just offering more and better capabilities and features. When you have an alias set up so that every invocation of ls is really ls -F, that's the same basic idea.

Linux and its grizzled father UNIX are really powerful because they offer these sorts of capabilities; it's hard to write a wrapper for Microsoft Excel on a Windows 10 system, by contrast.

A command with multiple versions in the wild is a perfect example of where a wrapper can be so beneficial too. Imagine you're deploying a few hundred servers and want to run a bare-bones Linux on them to maximize available cycles. Problem is, your admin scripts rely on the very latest-and-greatest versions of sed, grep and find. Solution? Point the scripts at your wrapper versions of those commands, and make sure every flag you need is implemented, either in the base command (as would be the case on the newer systems) or through the wrapper code itself.

So, back to wegrep. When last I left this script, it offered up the base -C functionality of giving one or more lines of context before and after each match to a grep search. Left on the to-do list were to make it smarter about when to add the "- - - - - -" divider line, to add line numbers and to highlight the actual match.

Let's start with making the script smarter with the divider line, because that's by far the easiest. Like any script that tries to separate multiple blocks of output neatly, the key is really to count how many times the output has been sent. Here's the solution:

if [ $matches -eq 0 ] ; then
  echo "-----"
matches=$(( $matches + 1 ))

This appears prior to each block of output. The very first time it produces the top divider line, and otherwise it's skipped. After the matching line or lines, however, there's another divider line that is included each and every time.

Adding line numbers can be accomplished a number of ways, but I'm going to exploit an interesting capability of the sed command itself, the "=" expression. Let me demonstrate with the wonderland.txt data file that contains the first couple paragraphs of Alice in Wonderland:

$ head -5 wonderland.txt | sed =


Lewis Carroll

You can see what it does, I hope? It adds line numbers, but by having the number actually show up on a line prior to the actual matching line. It's a bit funky, but a second sed invocation fixes the problem and gives output that makes a lot more sense:

$ head -5 wonderland.txt | sed = | sed 'N;s/\n/:   /'
1:      ------------------------------------------------
5:      Lewis Carroll

In the above, the replacement sequence is a colon followed by the Tab character itself, which can be entered by typing Ctrl-V followed by the Tab itself—easily done in scripts.


Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.