{"id":101,"date":"2022-03-28T21:50:39","date_gmt":"2022-03-29T01:50:39","guid":{"rendered":"http:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=101"},"modified":"2024-10-11T09:30:03","modified_gmt":"2024-10-11T13:30:03","slug":"grep-cheat-sheet","status":"publish","type":"post","link":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=101","title":{"rendered":"grep cheat sheet"},"content":{"rendered":"\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td><strong>Basics<\/strong><\/td><\/tr><tr><td>grep [pattern] FILE<\/td><td><br><\/td><\/tr><tr><td>grep &#8216;^[A,E].*o&#8217; f.txt<\/td><td>Find a string starting with A or E and ending in o<\/td><\/tr><tr><td>grep -f pat.txt f.txt<\/td><td>Scan f.txt, using contents of pat.txt as regex<\/td><\/tr><tr><td>grep -i Gnu f.txt<\/td><td>Find &#8220;gnu&#8221; in f.txt, ignoring capitalization<\/td><\/tr><tr><td>grep -v gnu f.txt<\/td><td>Find all lines not containing &#8220;gnu&#8221; (invert match)<\/td><\/tr><tr><td>grep -w &#8216;a.*o&#8217; f.txt<\/td><td>Find whole word matches only, ignoring substrings<\/td><\/tr><tr><td>grep -x &#8216;a.*o&#8217; f.txt<\/td><td>Find whole line matches only, as in ^(a.*o)$<\/td><\/tr><tr><td><br><strong>Output<\/strong><\/td><\/tr><tr><td>-c<\/td><td>Print only the number of lines containing a match<\/td><\/tr><tr><td>&#8211;colo[u]r<\/td><td>Display matches in color<\/td><\/tr><tr><td>-l<\/td><td>Print the names of files with matches<\/td><\/tr><tr><td>-L<\/td><td>Print the names of files searched that contained no matches<\/td><\/tr><tr><td>-o<\/td><td>Print only the matched part of a line<\/td><\/tr><tr><td>-s<\/td><td>Suppress errors (such as non-existent or unreadable files)<\/td><\/tr><tr><td>-A n<\/td><td>Print n number of lines after a matching line<\/td><\/tr><tr><td>-B n<\/td><td>Print n number of lines before a matching line<\/td><\/tr><tr><td>-C n<\/td><td>Print n number of lines before and after a matching line<\/td><\/tr><tr><td><br><strong>Output prefixes<\/strong><\/td><\/tr><tr><td>-b<\/td><td>Print the byte offset of the match within the input file<\/td><\/tr><tr><td>-H<\/td><td>Print the filename containing a match<\/td><\/tr><tr><td>-h<\/td><td>Do not print the filename containing a match<\/td><\/tr><tr><td>-n<\/td><td>Print the line number of each match<\/td><\/tr><tr><td>-T<\/td><td>Print an initial Tab before matches so that output is neatly aligned<\/td><\/tr><tr><td><br><strong>File and directory selection<\/strong><\/td><\/tr><tr><td>-a<\/td><td>Process a binary file as if it were text<\/td><\/tr><tr><td>-D &lt;skip|read&gt;<\/td><td>Skip or read a FIFO, device, or socket<\/td><\/tr><tr><td>-d &lt;skip|read|recurse&gt;<\/td><td>Skip, read, or recurse through a directory<\/td><\/tr><tr><td>&#8211;exclude &#8216;*.sh&#8217;<\/td><td>Do not search any file with the .sh suffix<\/td><\/tr><tr><td>&#8211;exclude-from FILE<\/td><td>Skip any file listed in FILE<\/td><\/tr><tr><td>&#8211;exclude-dir *foo<\/td><td>Skip any directory ending in foo<\/td><\/tr><tr><td>-r<\/td><td>When a directory is encountered, search files in it<\/td><\/tr><tr><td>-R<\/td><td>Search directories and follow symlinks<\/td><\/tr><tr><td><br><strong>Variants<\/strong><\/td><\/tr><tr><td>-G<\/td><td>Use basic regex (this is the default)<\/td><\/tr><tr><td>-E<\/td><td>Extended regex<\/td><\/tr><tr><td>-F<\/td><td>Interpret the search pattern as a fixed string, not regex<\/td><\/tr><tr><td>-P<\/td><td>Use Perl regex (PCRE)<\/td><\/tr><tr><td><br><strong>Regular expression<\/strong><\/td><\/tr><tr><td>.<\/td><td>Any single character<\/td><\/tr><tr><td>?<\/td><td>Match preceding item zero or one time<\/td><\/tr><tr><td>*<\/td><td>Match preceding item zero or more times<\/td><\/tr><tr><td>+<\/td><td>Match preceding item one or more times<\/td><\/tr><tr><td>{2}<\/td><td>Match preceding item two times<\/td><\/tr><tr><td>{3,}<\/td><td>Match preceding item three or more times<\/td><\/tr><tr><td>{,4}<\/td><td>Match preceding item at most four times<\/td><\/tr><tr><td>{1,5}<\/td><td>Match preceding item at least once, but no more than five times<\/td><\/tr><tr><td>[A,B]<\/td><td>Match A or B<\/td><\/tr><tr><td>[3-9]<\/td><td>Match all digits 3 to 9<\/td><\/tr><tr><td>^<\/td><td>Start of a line<\/td><\/tr><tr><td>$<\/td><td>End of a line<\/td><\/tr><tr><td>\\s<\/td><td>Space<\/td><\/tr><tr><td>[:alnum:]<\/td><td>Alphanumeric character<\/td><\/tr><tr><td>[:alpha:]<\/td><td>Alphabetic character<\/td><\/tr><tr><td>[:digit:]<\/td><td>Digits 0 through 9<\/td><\/tr><tr><td>[:punct:]<\/td><td>Punctuation<\/td><\/tr><tr><td>[:space:]<\/td><td>Space<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Basics grep [pattern] FILE grep &#8216;^[A,E].*o&#8217; f.txt Find a string starting with A or E and ending in o grep -f pat.txt f.txt Scan f.txt, using contents of pat.txt as regex grep -i Gnu f.txt Find &#8220;gnu&#8221; in f.txt, ignoring capitalization grep -v gnu f.txt Find all lines not containing &#8220;gnu&#8221; (invert match) grep -w [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-file-management"],"_links":{"self":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=101"}],"version-history":[{"count":4,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":109,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions\/109"}],"wp:attachment":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}