APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Around


© November 1999 Tony Lawrence
November 1999

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

Basic usage is as simple as "grep pattern -A 3 -B 3 file", which will print 3 lines before and 3 lines after.

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/;
        }
}


Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Around


Inexpensive and informative Apple related e-books:

Take Control of Preview

Take Control of Parallels Desktop 12

Sierra: A Take Control Crash Course

Take control of Apple TV, Second Edition

Take Control of Numbers




More Articles by © Tony Lawrence




Printer Friendly Version

Have you tried Searching this site?

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.

Contact us


Printer Friendly Version





An atheist is just somebody who feels about Yahweh the way any decent Christian feels about Thor or Baal or the golden calf. As has been said before, we are all atheists about most of the gods that humanity has ever believed in. Some of us just go one god further. (Richard Dawkins)




Linux posts

Troubleshooting posts


This post tagged:

Code

Programming



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode





SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)
www.SCOsales.com