{"id":1769,"date":"2019-11-27T22:46:19","date_gmt":"2019-11-27T22:46:19","guid":{"rendered":"http:\/\/linuxdigest.com\/?p=1769"},"modified":"2020-11-14T18:29:44","modified_gmt":"2020-11-14T18:29:44","slug":"grep-command-examples-you-must-know","status":"publish","type":"post","link":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/","title":{"rendered":"8 grep command examples you must know"},"content":{"rendered":"\n

If I had to rank Linux commands by how often I use them. I think grep would easily rank in the top 5-10. <\/p>\n\n\n\n

Grep simply takes a file or files and searches for a given pattern. It then prints out the line the pattern was found on.<\/p>\n\n\n\n

Grep can come in handy in so many situations. Need to find in which configuration file a certain parameter is? Need to find a particular IP address in your apache logs? Grep can solve those problems easily.<\/p>\n\n\n\n

I hope you find these grep examples useful.<\/p>\n\n\n\n

\"Test<\/figure><\/div>\n\n\n\n

Before we start we need a text file to search. I have created one that you can see in the attached image.<\/p>\n\n\n\n

We will use this file to text our grep command examples.<\/p>\n\n\n\n

<\/p>\n\n\n\n

Simple grep example<\/h2>\n\n\n\n

The simplest grep example is just to search for a string of characters in a file: grep <pattern> <file><\/p>\n\n\n\n

grep This test-file.txt<\/code><\/pre>\n\n\n\n
\"grep<\/figure><\/div>\n\n\n\n

Grep with regex<\/h2>\n\n\n\n

Finding complex patterns of text can be done using regular expressions. Let’s try finding any text that is between quote mark. If you are not familiar with regular expressions, there are tons of resources that can be found on Google. I like Regex Tutorials<\/a>.<\/p>\n\n\n\n

grep -E '\".*\"' test-file.txt<\/code><\/pre>\n\n\n\n
\"grep<\/figure><\/div>\n\n\n\n

You can also shorten this command by using egrep. Egrep is basically the same as writing grep -E.<\/p>\n\n\n\n

egrep '\".*\"' test-file.txt<\/code><\/pre>\n\n\n\n

Grep all files in a directory<\/h2>\n\n\n\n

Looking through all files in a directory is simply a matter of replacing the filename with an asterisk (*). To make grep print the filename before each match use the -H option. If there are more than one files you can skip the -H option.<\/p>\n\n\n\n

grep -H This my-directory\/*<\/code><\/pre>\n\n\n\n
\"grep<\/figure><\/div>\n\n\n\n

If you want to search all directories and subdirectories, you can use the -r option. This will instruct grep to recursively go through all directories and files in the path specified.<\/p>\n\n\n\n

grep -r This my-directory\/*<\/code><\/pre>\n\n\n\n

Grep in reverse<\/h2>\n\n\n\n

Want to have grep do the exact opposite of what it is supposed to do and print all the lines that don’t match and not the ones that match? No problem. Just use the -v option for invert match.<\/p>\n\n\n\n

grep -v This test-file.txt<\/code><\/pre>\n\n\n\n
\"grep<\/figure><\/div>\n\n\n\n

Print only the number of lines that match<\/h2>\n\n\n\n

If you only want to know how many lines contain your string you can use the -c option. Note that the -c option only counts the number of lines that match. Not how many times the pattern matches.<\/p>\n\n\n\n

\"grep<\/figure><\/div>\n\n\n\n

Case insensitive grep<\/h2>\n\n\n\n

Normally grep is case sensitive. Some times you want to grep for a pattern where it doesn’t matter if the characters are upper case or lower case. In that case you can use the -i option.<\/p>\n\n\n\n

grep -i ken test-file.txt<\/code><\/pre>\n\n\n\n
\"grep<\/figure><\/div>\n\n\n\n

Grep for multiple strings<\/h2>\n\n\n\n

If you have more than one pattern you need to grep for, -e specifies what pattern you want to use. You can use -e multiple times to use more than one pattern.<\/p>\n\n\n\n

grep -e Ken -e file test-file.txt<\/code><\/pre>\n\n\n\n
\"grep<\/figure><\/div>\n\n\n\n

Grep lines before and after the match<\/h2>\n\n\n\n

This can be useful in cases where you are searching for a pattern in a log file but you want to know what came before or after the line you are matching. The -A, -B and -C options are what you need to know.<\/p>\n\n\n\n

-A 2<\/td>Print the next two lines after the matched line.<\/td><\/tr>
-B 2<\/td>Print the last two lines that came before the matched line.<\/td><\/tr>
-C 2<\/td>Print the last two lines that came before the matched line and also the next two after. Same as -A 2 -B 2.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

As an example, you can try printing one line before and one line after the match with -C 1.<\/p>\n\n\n\n

grep -C 1 Wikipedia test-file.txt<\/code><\/pre>\n\n\n\n
\"grep<\/figure><\/div>\n\n\n\n

Using grep in bash scripts<\/h2>\n\n\n\n

Usually, the grep exit status is 0 if lines are found and 1 if none are found. If an error occurs, the exit status will be 2. If you don’t want the matched lines to be printed you would use grep’s -q option to suppress output.<\/p>\n\n\n\n

Here is a sample bash script that checks if a pattern exists.<\/p>\n\n\n\n

if grep -q Globally test-file.txt\nthen\n  echo \"The string was found\"\nelse\n  echo \"The string was not found\"\nfi\n<\/code><\/pre>\n\n\n\n

Similarly, if you want to catch every possible exit status you could write a script like this.<\/p>\n\n\n\n

grep -q Globally test-file.txt\ngrepresult=$?\necho $grepresult\nif [[ $grepresult -eq 0 ]]\nthen\n  echo \"The string was found\"\nelif [[ $grepresult -eq 1 ]]\nthen\n  echo \"The string was not found\"\nelif [[ $grepresult -eq 2 ]]\nthen\n  echo \"An error occured\"\nelse\n  echo \"Unknown exit code from grep\"\nfi\n<\/code><\/pre>\n\n\n\n

Conclusion<\/h2>\n\n\n\n

Hopefully, you have found these grep examples helpful. In the Linux command line, the grep command is used quite frequently. So you learning these and other grep options can be of great help.<\/p>\n","protected":false},"excerpt":{"rendered":"

If I had to rank Linux commands by how often I use them. I think grep…<\/p>\n","protected":false},"author":1,"featured_media":1842,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[27],"tags":[19,28,29],"yoast_head":"\n8 grep command examples you must know - Linux Digest<\/title>\n<meta name=\"description\" content=\"Here are some grep command examples to get you started. Simple grep example. Grep with regex. Grep in reverse. Grep for multiple strings.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"8 grep command examples you must know - Linux Digest\" \/>\n<meta property=\"og:description\" content=\"Here are some grep command examples to get you started. Simple grep example. Grep with regex. Grep in reverse. Grep for multiple strings.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Digest\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-27T22:46:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-11-14T18:29:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/Copy-of-Untitled.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"The Linux Digest Guy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"The Linux Digest Guy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/\"},\"author\":{\"name\":\"The Linux Digest Guy\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\"},\"headline\":\"8 grep command examples you must know\",\"datePublished\":\"2019-11-27T22:46:19+00:00\",\"dateModified\":\"2020-11-14T18:29:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/\"},\"wordCount\":690,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"keywords\":[\"Command line\",\"grep\",\"search\"],\"articleSection\":[\"Command line tools\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/\",\"url\":\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/\",\"name\":\"8 grep command examples you must know - Linux Digest\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/#website\"},\"datePublished\":\"2019-11-27T22:46:19+00:00\",\"dateModified\":\"2020-11-14T18:29:44+00:00\",\"description\":\"Here are some grep command examples to get you started. Simple grep example. Grep with regex. Grep in reverse. Grep for multiple strings.\",\"breadcrumb\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/linuxdigest.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"8 grep command examples you must know\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/linuxdigest.com\/#website\",\"url\":\"https:\/\/linuxdigest.com\/\",\"name\":\"Linux Digest\",\"description\":\"Linux tutorials for everyone\",\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/linuxdigest.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/linuxdigest.com\/#organization\",\"name\":\"Linux Digest\",\"url\":\"https:\/\/linuxdigest.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"contentUrl\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"width\":1102,\"height\":170,\"caption\":\"Linux Digest\"},\"image\":{\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\",\"name\":\"The Linux Digest Guy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"caption\":\"The Linux Digest Guy\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"8 grep command examples you must know - Linux Digest","description":"Here are some grep command examples to get you started. Simple grep example. Grep with regex. Grep in reverse. Grep for multiple strings.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/","og_locale":"en_US","og_type":"article","og_title":"8 grep command examples you must know - Linux Digest","og_description":"Here are some grep command examples to get you started. Simple grep example. Grep with regex. Grep in reverse. Grep for multiple strings.","og_url":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/","og_site_name":"Linux Digest","article_published_time":"2019-11-27T22:46:19+00:00","article_modified_time":"2020-11-14T18:29:44+00:00","og_image":[{"width":300,"height":300,"url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/Copy-of-Untitled.png","type":"image\/png"}],"author":"The Linux Digest Guy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"The Linux Digest Guy","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/#article","isPartOf":{"@id":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/"},"author":{"name":"The Linux Digest Guy","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2"},"headline":"8 grep command examples you must know","datePublished":"2019-11-27T22:46:19+00:00","dateModified":"2020-11-14T18:29:44+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/"},"wordCount":690,"commentCount":0,"publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"keywords":["Command line","grep","search"],"articleSection":["Command line tools"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/","url":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/","name":"8 grep command examples you must know - Linux Digest","isPartOf":{"@id":"https:\/\/linuxdigest.com\/#website"},"datePublished":"2019-11-27T22:46:19+00:00","dateModified":"2020-11-14T18:29:44+00:00","description":"Here are some grep command examples to get you started. Simple grep example. Grep with regex. Grep in reverse. Grep for multiple strings.","breadcrumb":{"@id":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/linuxdigest.com\/howto\/grep-command-examples-you-must-know\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxdigest.com\/"},{"@type":"ListItem","position":2,"name":"8 grep command examples you must know"}]},{"@type":"WebSite","@id":"https:\/\/linuxdigest.com\/#website","url":"https:\/\/linuxdigest.com\/","name":"Linux Digest","description":"Linux tutorials for everyone","publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxdigest.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxdigest.com\/#organization","name":"Linux Digest","url":"https:\/\/linuxdigest.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","contentUrl":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","width":1102,"height":170,"caption":"Linux Digest"},"image":{"@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2","name":"The Linux Digest Guy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","caption":"The Linux Digest Guy"}}]}},"_links":{"self":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1769"}],"collection":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/comments?post=1769"}],"version-history":[{"count":8,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1769\/revisions"}],"predecessor-version":[{"id":1877,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1769\/revisions\/1877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media\/1842"}],"wp:attachment":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media?parent=1769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/categories?post=1769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/tags?post=1769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}