未加星标

Polishing the wegrep Wrapper Script

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

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 "-----" fi 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 = 1 ------------------------------------------------------ 2 3 ALICE'S ADVENTURES IN WONDERLAND 4 5 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: ------------------------------------------------ 2: 3: ALICE'S ADVENTURES IN WONDERLAND 4: 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.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: LinuxWindowsNTUExcel
分页:12
转载请注明
本文标题:Polishing the wegrep Wrapper Script
本站链接:http://www.codesec.net/view/482003.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(24)