Preface
You are likely to be familiar with using a search dialog (usually invoked with the Ctrl+F
shortcut) to locate the occurrences of a particular string. Graphical User Interface (GUI) tools such as a text editor, word processor, web browser and programming IDE usually support such a search feature. The grep
command is a versatile and feature-rich version of that search functionality usable from the command line. An important feature that GUI applications may lack is regular expressions, a mini-programming language to precisely define a matching criteria.
Modern requirements have given rise to tools like ripgrep
that provide out-of-box features such as recursive search while respecting the ignore rules of a version controlled directory.
This book heavily leans on examples to present features one by one. In addition to command options, regular expressions will also be discussed in detail. It is recommended that you manually type each example. Make an effort to understand the sample input as well as the solution presented and check if the output changes (or not!) when you alter some part of the input and the command. As an analogy, consider learning to drive a car — no matter how much you read about them or listen to explanations, you'd need practical experience to become proficient.
Prerequisites
You should be familiar with command line usage in a Unix-like environment. You should also be comfortable with concepts like file redirection and command pipelines.
You are also expected to get comfortable with reading manuals, searching online, visiting external links provided for further reading, tinkering with illustrated examples, asking for help when you are stuck and so on. In other words, be proactive and curious instead of just consuming the content passively.
If you are new to the world of the command line, check out my Computing from the Command Line ebook and curated resources on Linux CLI and Shell scripting before starting this book.
Conventions
- Code snippets are copy pasted from the Bash shell (version 5.0.17) for GNU grep 3.10 and ripgrep 13.0.0. Such snippets have been modified for presentation purposes — some commands are preceded by comments to provide context and explanations, blank lines have been added to improve readability and so on.
- Unless otherwise noted, all examples and explanations are meant for ASCII input.
- External links are provided throughout the book for you to explore certain topics in more depth.
- The learn_gnugrep_ripgrep repo has all the example files and scripts used in the book. The repo also includes code snippets and exercises used in the book. Solutions file is also provided. If you are not familiar with the
git
command, click the Code button on the webpage to get the files.
Acknowledgements
- GNU grep documentation — manual and examples
- ripgrep — user guide and examples
- stackoverflow and unix.stackexchange — for getting answers to pertinent questions on
grep
and related commands - tex.stackexchange — for help on pandoc and
tex
related questions - /r/commandline/, /r/linux4noobs/, /r/linuxquestions/ and /r/linux/ — helpful forums
- canva — cover image
- oxipng, pngquant and svgcleaner — optimizing images
- Warning and Info icons by Amada44 under public domain
- carbon — creating terminal screenshots with highlighted text
- Andrew Gallant (author of
ripgrep
), mikeblas and Pound-Hash for critical feedback - mdBook — for web version of the book that you are currently reading
- mdBook-pagetoc — for adding table of contents for each chapter
- minify-html — for minifying html files
Special thanks to all my friends and online acquaintances for their help, support and encouragement, especially during difficult times.
Feedback and Errata
I would highly appreciate it if you'd let me know how you felt about this book. It could be anything from a simple thank you, pointing out a typo, mistakes in code snippets, which aspects of the book worked for you (or didn't!) and so on. Reader feedback is essential and especially so for self-published authors.
You can reach me via:
- Issue Manager: https://github.com/learnbyexample/learn_gnugrep_ripgrep/issues
- E-mail: learnbyexample.net@gmail.com
- Twitter: https://twitter.com/learn_byexample
Author info
Sundeep Agarwal is a lazy being who prefers to work just enough to support his modest lifestyle. He accumulated vast wealth working as a Design Engineer at Analog Devices and retired from the corporate world at the ripe age of twenty-eight. Unfortunately, he squandered his savings within a few years and had to scramble trying to earn a living. Against all odds, selling programming ebooks saved his lazy self from having to look for a job again. He can now afford all the fantasy ebooks he wants to read and spends unhealthy amount of time browsing the internet.
When the creative muse strikes, he can be found working on yet another programming ebook (which invariably ends up having at least one example with regular expressions). Researching materials for his ebooks and everyday social media usage drowned his bookmarks, so he maintains curated resource lists for sanity sake. He is thankful for free learning resources and open source tools. His own contributions can be found at https://github.com/learnbyexample.
List of books: https://learnbyexample.github.io/books/
License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Code snippets are available under MIT License.
Resources mentioned in Acknowledgements section above are available under original licenses.
Book version
2.0
See Version_changes.md to track changes across book versions.