(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



Around

November 1999



Note: modern "grep" has flags for printing lines before and after the match. Search for "context" in the man/info pages.

This is just a simple Perl/sed ( get Perl from Skunkware if you don't already have it) program that prints the text "around" a pattern. I call it "around", and you'd use it like this:


# around tony /etc/passwd
audit:x:79:17:Audit administrator:/tcb/files/audit:
apl:x:201:50::/usr/books:/bin/sh
ftp:x:20:50:anonymous ftp account:/usr/internet/ip/0.0.0.0/sco_ftp:/bin/sh
kate:x:202:50::/usr/kate:/bin/sh
tony:x:203:50::/usr/tony:/bin/sh
books:x:204:50::/usr/books:/bin/sh
testuv:x:205:50::/usr/testuv:/bin/sh
fred:x:206:50::/usr/fred:/bin/sh
john:x:207:50::/usr/john:/bin/sh
# 
 

As you can see, it prints the 4 lines before and the 4 lines after "tony". If you understand regex patterns, you can also use it that way. As presented here, this only searches one file; it wouldn't be very hard to change this to read from any number of files listed on the command line, but it would be more complicated, and I wanted to leave this very easy to understand for non-programmers. It could also easily take an argument that would specify how many lines to show before and after; again, I left that out to keep it simple.

I think it's written simply enough that anyone should be able to modify it if they need it to print more or less. Note that the very first line must point at wherever your Perl binary lives.

This isn't particularly fast, efficient or elegant. If you understand that by looking at it, then you are surely capable of writing a better version yourself, and wouldn't need this. This is for people who don't write their own programs or are just starting to: it's intended to be easy to understand, not great programming.

Non-programmers should note that using patterns that match more than one line can easily produce duplicate lines of output. For example, "around 'tony|kate' /etc/passwd" will list lines before and after "kate", and then the lines before and after "tony". That could be confusing if you aren't expecting it.

#!/usr/local/bin/perl
$search=shift;
$file=shift;
$line=0;
open(FILE,$file) or die "Can't open $file";
while (<FILE>) {
        $line++;
        if (/$search/o) {
                $start= $line -4;
                $start=1 if $start < 1;
                $end = $line +4;
                $cmd="/bin/sed -n $start,${end}p $file";
                print qx/$cmd/;
        }
}




Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

cartoon
Versatile Site Map Generator $59.00
A1 Sitemap Generator


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Programming
       - Code


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress