open.itworld.com
  Search  
Security Home Page Security Webcasts Security White Papers Security Newsletters Security News Open Topics Careers ITworld Voices ITwhirled The Security site of ITworld.com

Unix Tip: Finding text in context -- again

ITworld.com 5/2/2007

Sandra Henry-Stocker, ITworld.com

Send in your Unix questions today! | See additional Unix tips and tricks

A reader recently asked me about me about finding text in context by posing this question:

"I work on a solaris system where often I have to view outputs of certain log files. I use 'grep' to search for a particular string and that solves the purpose most of the times. However sometimes I need to see a few lines above and below the matching line of grep. That is when the problem comes. With a certain sed command I am able to see lines after the matching line but that seems too cumbersome. e.g. "sed -n '/<match text>/{p;n;p;n;q;/' <filename>

Is there a more graceful way to do this?"

Since you're reading this column, you probably already suspect that the answer to this reader's question was "yes". What you might not suspect, however, is that there are now at least two very nice tools to help you pull text from files while retaining enough of the context to make good use of the text you've located.

One such tool is one that I wrote about nearly two years ago -- the GNU grep command. The other is a Perl script available from BigAdmin named "cgrep" for "context grep". The two tools provide similar options for finding the text you want while configuring the amount of context you want to see around it.

The GNU grep command allows you to select how many lines you want to see above and below your located text with the use of -B (before), -A (after) or -C (context) options. The cgrep script uses -b, -a and -c to do the same thing. You can also use --before, --after and --context if you prefer.

Both tools provide a -i option to make the search case insensitive.

The GNU tool can colorize your search terms to make them stand out in your output while cgrep displays the lines containing your search terms in a different color. In my PuTTY window, this is my bold foreground color. While colorization is the default, it can be turned off if you don't want your search text colored with the -nov (no vt100) option. This is especially useful if you want to redirect your text to a plain text file and don't want control sequences for color changes in your text.

GNU grep colorizes search terms only if you request the option, but then colorizes the search terms within the lines of text rather than colorizing entire lines. This makes it easier for you to pick out the terms within the lines of text.

The GNU grep command can be downloaded as a package from Sunfreeware.com, but requires a number of other packages to be installed as well -- libiconv, pcre, libgcc and gcc. The cgrep tool, which is, after all, a Perl script requires only that you have an installation of Perl and that you have the Getopt::Long module installed to handle command line options, but that's it.

The GNU grep command offers help with the --help option and then displays a list of several dozen options, many of which allow you to customize your search and many more which allow you to further customize your output -- such as adding line numbers, stopping after a select number of matches have been found and printing the file names along with the matches.

Cgrep's help option is invoked with -help. While it doesn't have nearly as many options as the GNU grep command, it's dreadfully easy to download, install and use and as customizable as you are handy with Perl code.

Solaris packages for GNU grep can be downloaded here.

The cgrep Perl script can be downloaded here.

On this topic

 

Sandra Henry-Stocker has been administering Unix systems for more than 18 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She currently works for TeleCommunication Systems, a wireless communications company, in Annapolis, Maryland, where no one else necessarily shares any of her opinions. She lives with her second family on a small farm on Maryland's Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.




Sponsored Links

Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
Sun Microsystems' - FREE 60 DAY TRIAL OFFER!
Test Sun's Newest Servers BEFORE YOU BUY. Plug Them In With Access To Full Technical Support.
100% Web Based Help Desk Software
Easy to use, customizable to meet your needs, powerful and scalable. Free online demo. Try it today!
Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
Used and Refurbished HP ProCurve Switches
Lifetime Warranties, Professional Testing & Shipping on all HP Equipment Purchases!
» Buy a link now

Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
Bring harmony to your mix of UNIX-Linux-Windows computing environments
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
 Home   Open source  Operating systems  Unix
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.