Chapter 1. grep Pocket Reference
Introduction
Chances are that if youve worked for any length of time on a Linux system, either as a system administrator or as a developer, youve used the grep command. The tool is installed by default on almost every installation of Linux, BSD, and Unix, regardless of distribution, and is even available for Windows (with wingrep or via Cygwin).
GNU and the Free Software Foundation distribute grep as part of their suite of open source tools. Other versions of grep are distributed for other operating systems, but this book focuses primarily on the GNU version, as it is the most prevalent at this point.
The grep command lets the user find text in a given file or output quickly and easily. By giving grep a string to search for, it will print out only lines that contain that string and can print the corresponding line numbers for that text. The simple use of the command is well-known, but there are a variety of more advanced uses that make grep a powerful search tool.
The purpose of this book is to pack all the information an administrator or developer could ever want into a small guide that can be carried around. Although the simple uses of grep do not require much education, the advanced applications and the use of regular expressions can become quite complicated. The name of the tool is actually an acronym for Global Regular-Expression Print, which gives an indication of its purpose.
GNU grep is actually a combination of four different tools, each with its unique style of finding text: basic regular expressions, extended regular expressions, fixed strings, and Perl-style regular expression. There are other implementations of grep -like programs such as agrep, zipgrep, and grep-like functions in .NET, PHP, and SQL. This guide will describe the particular options and strengths of each style.
The official website for grep is http://www.gnu.org/software/grep/. It contains information about the project and some brief documentation. The source code for grep is only 712 KB, and the current version at the time of this writing is 2.5.3. This pocket reference is current to that version, but the information will be generally valid for earlier and later versions.
As an important note, the current version of grep that ships with Mac OS X 10.5.5 is 2.5.1; however, most of the options in this book will still work for that version. There are other grep programs as well, in addition to the one from GNU, and these are typically the ones installed by default under HP-UX, AIX, and older versions of Solaris. For the most part, the regular expression syntax is very similar between these versions, but the options differ. This book deals exclusively with the GNU version because it is more robust and powerful than other versions.
Conventions Used in This Book
The following typographical conventions are used in this book:
ItalicIndicates commands, new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities.
Constant width
Indicates options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands.
Constant width italic
Shows text that should be replaced with user-supplied values.
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: grep Pocket Reference by John Bambenek and Agnieszka Klus. Copyright 2009 John Bambenek and Agnieszka Klus, 978-0-596-15360-1.
If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at .
Safari Books Online
Note
When you see a Safari Books Online icon on the cover of your favorite technology book, that means the book is available online through the OReilly Network Safari Bookshelf.
Safari offers a solution thats better than e-books. Its a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.
Comments and Questions
Please address comments and questions concerning this book to the publisher:
OReilly Media, Inc. |
1005 Gravenstein Highway North |
Sebastopol, CA 95472 |
800-998-9938 (in the United States or Canada) |
707-829-0515 (international or local) |
707-829-0104 (fax) |
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
http://www.oreilly.com/catalog/9780596153601 |
To comment or ask technical questions about this book, send email to:
For more information about our books, conferences, Resource Centers, and the OReilly Network, see our website at:
Acknowledgments
From John Bambenek
I would like to thank Isabel Kunkle and the rest of the OReilly team behind the editing and production of this book. My wife and son deserve thanks for their support and love as I completed this project. My coauthor, Agnieszka, has been invaluable in making an onerous task of writing a book more manageable; she contributed greatly to this project. Brian Krebs of