Grep by example: Interactive guide

saikatsg2 pts0 comments

Grep by example: Interactive guide

Anton Zhiyanov<br>projects<br>books<br>blog<br>about

grep is the ultimate text search tool available on virtually all Linux machines. While there are now better alternatives (such as ripgrep), you will still often find yourself on a server where grep is the only search tool available. So it's nice to have a working knowledge of it.<br>That's why is I've created this interactive step-by-step guide to grep operations. You can read it from start to finish to (hopefully) learn more about grep, or jump to a specific use case that interests you.<br>Feel free to experiment with the examples by changing the commands and clicking Run.<br>Basics ·<br>Recursive search ·<br>Search options ·<br>Output options ·<br>Final thoughts<br>This guide is also available in other formats:<br>PDF minibook<br>Playground

Basics<br>Basically, grep works like this:<br>You give it a search pattern and a file.<br>grep reads the file line by line, printing the lines that match the pattern and ignoring others.<br>Let's look at an example. We'll search the httpurr source code, which I've already downloaded to the /opt/httpurr directory like this:<br>cd /opt<br>curl -OL https://github.com/rednafi/httpurr/archive/refs/tags/v0.1.2.tar.gz<br>tar xvzf v0.1.2.tar.gz<br>mv httpurr-0.1.2 httpurr<br>cd httpurr

Search in file ·<br>Matches ·<br>Regular expressions ·<br>Fixed strings ·<br>Multiple patterns<br>Search in file<br>Let's find all occurrences of the word codes in README.md:<br>grep -n codes README.md

3: >> HTTP status codes on speed dial<br>30:* List the HTTP status codes:<br>54:* Filter the status codes by categories:<br>124: Print HTTP status codes by category with --list;<br>131: Print HTTP status codes<br>grep read the contents of README.md, and for each line that contained codes, grep printed it to the terminal.<br>grep also included the line number for each line, thanks to the -n (--line-number) option.<br>Not all grep versions support the long option syntax (e.g. --line-number). If you get an error using the long version, try the short one (e.g. -n) — it may work fine.

Matches<br>grep uses partial matches by default:<br>grep -n descr README.md

81:* Display the description of a status code:<br>127: Print the description of an HTTP status code<br>The word description matches the descr search pattern.<br>To search for whole words instead, use the -w (--word-regexp) option:<br>grep -n --word-regexp code README.md

81:* Display the description of a status code:<br>84: httpurr --code 410<br>94: The HyperText Transfer Protocol (HTTP) 410 Gone client error response code<br>99: code should be used instead.<br>126: -c, --code [status code]<br>127: Print the description of an HTTP status code<br>grep found strings containing the word code, but not codes. Try removing --word-regexp and see how the results change.<br>When using multiple short options, you can combine them like this: grep -nw code README.md. This gives exactly the same result as using the separate options (-n -w).

To search for whole lines instead of partial matches of whole words, use the -x (--line-regexp) option:<br>grep -n --line-regexp end httpurr.rb

47:end<br>Try removing --line-regexp and see how the results change.<br>Regular expressions<br>To make grep use regular expressions (Perl-compatible regular expressions in grep terminology), use the -P (--perl-regexp) option.<br>Let's find all lines with a word that contains res followed by other letters:<br>grep -Pn 'res\w+' README.md

94: The HyperText Transfer Protocol (HTTP) 410 Gone client error response code<br>95: indicates that access to the target resource is no longer available at the<br>152:of the rest.<br>\w+ means "one or more word-like characters" (e.g. letters like p or o, but not punctuation like . or !), so response, resource, and rest all match.<br>Regular expression dialects in grep<br>Without --perl-regexp, grep treats the search pattern as something called a basic regular expression. While regular expressions are quite common in the software world, the basic dialect is really weird, so it's better not to use it at all.<br>Another dialect supported by grep is extended regular expressions. You can use the -E (--extended-regexp) option to enable them. Extended regular expressions are almost like normal regular expressions, but not quite. So I wouldn't use them either.<br>Some grep versions do not support --perl-regexp. For those, --extended-regexp is the best you can get.

Suppose we are only interested in 4 letter words starting with res:<br>grep -Pn 'res\w\b' README.md

152:of the rest.<br>\b means "word boundary" (e.g. a space, a punctuation character, or the end of a line), so rest matches, but response and resource don't.<br>Finally, let's search for 3-digit numbers (showing first 10 matches with head):<br>grep -Pn '\d\d\d' README.md | head

45: 100 Continue<br>46: 101 Switching Protocols<br>47: 102 Processing<br>48: 103 Early Hints<br>69: 200 OK<br>70: 201 Created<br>71: 202 Accepted<br>72: 203 Non-Authoritative Information<br>73: 204 No Content<br>74: 205 Reset Content<br>A full tutorial on regular expressions is beyond the scope of this guide, but grep's "Perl-compatible" syntax is...

grep code search line regular regexp

Related Articles