grep is a very useful tool for finding all the lines in a file (or series of files) that contain a particular string or match some other pattern / criteria. For example, I have a file that contains data for 52 staff members and I want to find who knows Lua ...
[trainee@melksham Download]$ grep Lua requests.xyz
lisa PHP Python Lua ASP Perl Java
graham Lua Perl Tcl/Tk
[trainee@melksham Download]$
But what if I want to see the context of my matches - to be given back not only the matched lines, but a few lines before and after the match to see what it relates too - "other nearby employees" in my example, but more commonly "the rest of the paragraph / example" in the case of text files. On Linux's
grep, you can use the
-A and
-B options to specify the number of lines to display before and after each match, and the
-C option if you want to show the same number before and after. Let's see that:
[trainee@melksham Download]$ grep -A2 -B1 Lua requests.xyz
kerry Perl Tcl/Tk Ruby MySQL
lisa PHP Python Lua ASP Perl Java
margaret XML Perl Ruby MySQL Tcl/Tk
nina Tcl/Tk Perl ASP Ruby
--
fred MySQL Perl Java XML
graham Lua Perl Tcl/Tk
harry PHP Python Java
ivan Ruby Java Perl Tcl/Tk MySQL
[trainee@melksham Download]$ grep -C3 Lua requests.xyz
iris Perl MySQL Java Tcl/Tk
jenny XML Perl Ruby ASP
kerry Perl Tcl/Tk Ruby MySQL
lisa PHP Python Lua ASP Perl Java
margaret XML Perl Ruby MySQL Tcl/Tk
nina Tcl/Tk Perl ASP Ruby
olivia MySQL Python ASP PHP
--
david Perl Tcl/Tk Java
ed Ruby Perl Java PHP
fred MySQL Perl Java XML
graham Lua Perl Tcl/Tk
harry PHP Python Java
ivan Ruby Java Perl Tcl/Tk MySQL
john PHP XML Java Perl
[trainee@melksham Download]$
If you're on a system that's got a version of
grep that doesn't support these options, what can you do? Well - you could do worse that write a Perl script - in fact we've used the requirement for a contextual grep for an
example on our
Perl Programming course.
(written 2009-10-30)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P208 - Perl - Lists [28] Perl for breakfast - (2004-08-25)
[140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
[230] Course sizes - beware of marketing statistics - (2005-02-27)
[240] Conventional restraints removed - (2005-03-09)
[355] Context in Perl - (2005-06-22)
[463] Splitting the difference - (2005-10-13)
[560] The fencepost problem - (2006-01-10)
[622] Queues and barrel rolls in Perl - (2006-02-24)
[762] Huge data files - what happened earlier? - (2006-06-15)
[773] Breaking bread - (2006-06-22)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[968] Perl - a list or a hash? - (2006-12-06)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
[1828] Perl - map to process every member of a list (array) - (2008-10-09)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[1918] Perl Socket Programming Examples - (2008-12-02)
[2067] Perl - lists do so much more than arrays - (2009-03-05)
[2226] Revision / Summary of lists - Perl - (2009-06-10)
[2295] The dog is not in trouble - (2009-07-17)
[2813] Iterating over a Perl list and changing all items - (2010-06-15)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
[3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
[4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
A166 - Web Application Deployment - Linux Utilities [63] Almost like old times - (2004-09-26)
[71] Comparators in Linux and Unix - (2004-10-03)
[1361] Korn shell course - (2007-09-22)
[1366] awk - a powerful data extraction and manipulation tool - (2007-09-25)
[1690] Conversion of c/r line ends to l/f line ends - (2008-06-28)
[2145] Using the internet to remotely check for power failure at home (PHP) - (2009-04-29)
[2320] Helping new arrivals find out about source code examples - (2009-08-03)
[2638] Finding what has changed - Linux / Unix - (2010-02-17)
[3446] Awk v Perl - (2011-09-18)
[3764] Shell, Awk, Perl of Python? - (2012-06-14)
[3902] Shell - Grep - Sed - Awk - Perl - Python - which to use when? - (2012-10-22)
[4586] Extending your bash shell with aliases, functions and extra commands - (2015-11-28)
[4682] One line scripts - Awk, Perl and Ruby - (2016-05-20)
Some other Articles
A variable number of arguments in a PHP functionMelksham ForumSanta Special - Trowbridge and Melksham to SwindonHow do I set up a constant in Python?Finding text and what surrounds it - contextual grepClustering on TomcatLoad balancing with sticky sessions (httpd / Tomcat)Sample code with errors in it on our web sitePantomimes around Melksham - 2009/2010 seasonAccidentally typed ci rather than vi?