{"id":11450,"date":"2019-03-14T07:55:34","date_gmt":"2019-03-14T07:55:34","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11450"},"modified":"2019-03-14T07:55:34","modified_gmt":"2019-03-14T07:55:34","slug":"12-practical-examples-of-linux-grep-command","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/12-practical-examples-of-linux-grep-command\/","title":{"rendered":"12 Practical Examples of Linux grep Command"},"content":{"rendered":"<p>Have you ever been confronted with the task of looking for a particular string or pattern in a file, yet have no idea where to start looking? Well then, here is\u00a0<strong>grep<\/strong>\u00a0to the rescue!<\/p>\n<div id=\"attachment_4617\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/11\/Grep-Command-Examples.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4617\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/11\/Grep-Command-Examples.png\" alt=\"Grep Command Examples\" width=\"425\" height=\"276\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">12 Grep Command Examples<\/p>\n<\/div>\n<p><strong>grep<\/strong>\u00a0is a powerful file pattern searcher that comes equipped on every distribution of\u00a0<strong>Linux<\/strong>. If, for whatever reason, it is not installed on your system, you can easily install it via your package manager (<strong>apt-get<\/strong>\u00a0on\u00a0<strong>Debian<\/strong>\/<strong>Ubuntu<\/strong>\u00a0and\u00a0<strong>yum<\/strong>\u00a0on\u00a0<strong>RHEL<\/strong>\/<strong>CentOS<\/strong>\/<strong>Fedora<\/strong>).<\/p>\n<pre>$ sudo apt-get install grep         #Debian\/Ubuntu<\/pre>\n<pre>$ sudo yum install grep             #RHEL\/CentOS\/Fedora<\/pre>\n<p>I have found that the easiest way to get your feet wet with\u00a0<strong>grep<\/strong>\u00a0is to just dive right in and use some real world examples.<\/p>\n<h3>1. Search and Find Files<\/h3>\n<p>Let\u2019s say that you have just installed a fresh copy of the new\u00a0<strong>Ubuntu<\/strong>\u00a0on your machine, and that you are going to give\u00a0<strong>Python<\/strong>\u00a0scripting a shot. You have been scouring the web looking for tutorials, but you see that there are two different versions of\u00a0<strong>Python<\/strong>\u00a0in use, and you don\u2019t know which one was installed on your system by the\u00a0<strong>Ubuntu<\/strong>\u00a0installer, or if it installed any modules. Simply run this command:<\/p>\n<pre># dpkg -l | grep -i python<\/pre>\n<h5>Sample Output<\/h5>\n<pre>ii  python2.7                        2.7.3-0ubuntu3.4                    Interactive high-level object-oriented language (version 2.7)\r\nii  python2.7-minimal                2.7.3-0ubuntu3.4                    Minimal subset of the Python language (version 2.7)\r\nii  python-openssl                   0.12-1ubuntu2.1                     Python wrapper around the OpenSSL library\r\nii  python-pam                       0.4.2-12.2ubuntu4                   A Python interface to the PAM library<\/pre>\n<p><center>First, we ran\u00a0<strong>dpkg \u2013l<\/strong>, which lists installed\u00a0<strong>*.deb<\/strong>\u00a0packages on your system. Second, we piped that output to\u00a0<strong>grep \u2013i<\/strong>\u00a0python, which simple states \u201cgo to grep and filter out and return everything with \u2018python\u2019 in it.\u201d The\u00a0<strong>\u2013i<\/strong>\u00a0option is there to ignore-case, as\u00a0<strong>grep<\/strong>\u00a0is case-sensitive. Using the\u00a0<strong>\u2013i<\/strong>\u00a0option is a good habit of getting into, unless of course you are trying to nail down a more specific search.<\/center><\/p>\n<h3>2. Search and Filter Files<\/h3>\n<p>The\u00a0<strong>grep<\/strong>\u00a0can also be used to search and filter within individual files or multiple files. Lets take this scenario:<\/p>\n<p>You are having some trouble with your\u00a0<strong>Apache Web Server<\/strong>, and you have reached out to one of the many awesome forums on the net asking for some help. The kind soul who replies to you has asked you to post the contents of your\u00a0<strong>\/etc\/apache2\/sites-available\/default-ssl<\/strong>\u00a0file. Wouldn\u2019t it be easier for you, the guy helping you, and everyone reading it, if you could remove all of the commented lines? Well you can! Just run this:<\/p>\n<pre># grep \u2013v \u201c#\u201d  \/etc\/apache2\/sites-available\/default-ssl<\/pre>\n<p>The\u00a0<strong>\u2013v<\/strong>\u00a0option tells\u00a0<strong>grep<\/strong>\u00a0to invert its output, meaning that instead of printing matching lines, do the opposite and print all of the lines that don\u2019t match the expression, in this case, the\u00a0<strong>#<\/strong>\u00a0commented lines.<\/p>\n<h3>3. Find all .mp3 Files Only<\/h3>\n<p>The\u00a0<strong>grep<\/strong>\u00a0can be very useful for filtering from\u00a0<strong>stdout<\/strong>. For example, let\u2019s say that you have an entire folder full of music files in a bunch of different formats. You want to find all of the<strong>\u00a0*.mp3<\/strong>\u00a0files from the artist\u00a0<strong>JayZ<\/strong>, but you don\u2019t want any of the remixed tracks. Using a\u00a0<strong>find command<\/strong>\u00a0with a couple of\u00a0<strong>grep<\/strong>\u00a0pipes will do the trick:<\/p>\n<pre># find . \u2013name \u201c*.mp3\u201d | grep \u2013i JayZ | grep \u2013vi \u201cremix\u201d<\/pre>\n<p>In this example, we are using find to print all of the files with a\u00a0<strong>*.mp3 extension<\/strong>, piping it to\u00a0<strong>grep \u2013i<\/strong>\u00a0to filter out and prints all files with the name \u201c<strong>JayZ<\/strong>\u201d and then another pipe to\u00a0<strong>grep \u2013vi<\/strong>\u00a0which filters out and does not print all filenames with the string (in any case) \u201c<strong>remix<\/strong>\u201d.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/35-practical-examples-of-linux-find-command\/\" target=\"_blank\" rel=\"noopener\">35 Practical Examples of Linux Find Command<\/a><\/p>\n<h3>4. Display Number of Lines Before or After Search String<\/h3>\n<p>Another couple of options are the\u00a0<strong>\u2013A<\/strong>\u00a0and\u00a0<strong>\u2013B<\/strong>\u00a0switches, which displays the matched line and number of lines either that come before or after the search string. While the man page gives a more detailed explanation, I find it easiest to remember the options as\u00a0<strong>\u2013A<\/strong>\u00a0=\u00a0<strong>after<\/strong>, and\u00a0<strong>\u2013B<\/strong>\u00a0=\u00a0<strong>before<\/strong>:<\/p>\n<pre># ifconfig | grep \u2013A 4 eth0\r\n# ifconfig | grep  -B 2 UP<\/pre>\n<h3>5. Prints Number of Lines Around Match<\/h3>\n<p>The grep\u2019s\u00a0<strong>\u2013C<\/strong>\u00a0option is similar, but instead of printing the lines that come either before or after the string, it prints the lines in either direction:<\/p>\n<pre># ifconfig | grep \u2013C 2 lo<\/pre>\n<h3>6. Count Number of Matches<\/h3>\n<p>Similar to piping a\u00a0<strong>grep<\/strong>\u00a0string to word count (<strong>wc<\/strong>\u00a0program) grep\u2019s built-in option can perform the same for you:<\/p>\n<pre># ifconfig | grep \u2013c inet6<\/pre>\n<h3>7. Search Files by Given String<\/h3>\n<p>The\u00a0<strong>\u2013n<\/strong>\u00a0option for\u00a0<strong>grep<\/strong>\u00a0is very useful when debugging files during compile errors. It displays the line number in the file of the given search string:<\/p>\n<pre># grep \u2013n \u201cmain\u201d setup..py<\/pre>\n<h3>8. Search a string Recursively in all Directories<\/h3>\n<p>If you would like to search for a string in the current directory along with all of the subdirectories, you can specify the\u00a0<strong>\u2013r<\/strong>\u00a0option to search recursively:<\/p>\n<pre># grep \u2013r \u201cfunction\u201d *<\/pre>\n<h3>9. Searches for the entire pattern<\/h3>\n<p>Passing the\u00a0<strong>\u2013w<\/strong>\u00a0option to grep searches for the entire pattern that is in the string. For example, using:<\/p>\n<pre># ifconfig | grep \u2013w \u201cRUNNING\u201d<\/pre>\n<p>Will print out the line containing the pattern in quotes. On the other hand, if you try:<\/p>\n<pre># ifconfig | grep \u2013w \u201cRUN\u201d<\/pre>\n<p>Nothing will be returned as we are not searching for a pattern, but an entire word.<\/p>\n<h3>10. Search a string in Gzipped Files<\/h3>\n<p>Deserving some mention are grep\u2019s derivatives. The first is\u00a0<strong>zgrep<\/strong>, which, similar to\u00a0<strong>zcat<\/strong>, is for use on\u00a0<strong>gzipped<\/strong>files. It takes the same options as\u00a0<strong>grep<\/strong>\u00a0and is used in the same way:<\/p>\n<pre># zgrep \u2013i error \/var\/log\/syslog.2.gz<\/pre>\n<h3>11. Match Regular Expression in Files<\/h3>\n<p>The\u00a0<strong>egrep<\/strong>\u00a0is another derivative that stands for \u201c<strong>Extended Global Regular Expression<\/strong>\u201d. It recognizes additional expression meta-characters such\u00a0<strong>at + ? |<\/strong>\u00a0and\u00a0<strong>()<\/strong>.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/difference-between-grep-egrep-and-fgrep-in-linux\/\" target=\"_blank\" rel=\"noopener\">What\u2019s Difference Between Grep, Egrep and Fgrep in Linux?<\/a><\/p>\n<p>egrep is very useful for searching source files, and other pieces of code, should the need arise. It can be invoked from regular grep by specifying the\u00a0<strong>\u2013E<\/strong>\u00a0option.<\/p>\n<pre># grep \u2013E<\/pre>\n<h3>12. Search a Fixed Pattern String<\/h3>\n<p>The\u00a0<strong>fgrep<\/strong>\u00a0searches a file or list of files for a fixed pattern string. It is the same as\u00a0<strong>grep \u2013F<\/strong>. A common way of using\u00a0<strong>fgrep<\/strong>\u00a0is to pass a file of patterns to it:<\/p>\n<pre># fgrep \u2013f file_full_of_patterns.txt file_to_search.txt<\/pre>\n<p>This is just a starting point with\u00a0<strong>grep<\/strong>, but as you are probably able to see, it is invaluable for a variety of purposes. Aside from the simple one line commands we have implemented,\u00a0<strong>grep<\/strong>\u00a0can be used to write powerful\u00a0<strong>cron<\/strong>\u00a0jobs, and robust\u00a0<strong>shell scripts<\/strong>, for a start.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-grep-commands-character-classes-bracket-expressions\/\" target=\"_blank\" rel=\"noopener\">11 \u2018Grep\u2019 Commands on Character Classes and Bracket Expressions<\/a><\/p>\n<p>Be creative, experiment with the options in the\u00a0<strong>man page<\/strong>, and come up with\u00a0<strong>grep expressions<\/strong>\u00a0that serve your own purposes!<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/12-practical-examples-of-linux-grep-command\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever been confronted with the task of looking for a particular string or pattern in a file, yet have no idea where to start looking? Well then, here is\u00a0grep\u00a0to the rescue! 12 Grep Command Examples grep\u00a0is a powerful file pattern searcher that comes equipped on every distribution of\u00a0Linux. If, for whatever reason, it &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/12-practical-examples-of-linux-grep-command\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;12 Practical Examples of Linux grep Command&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11450","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11450"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11450\/revisions"}],"predecessor-version":[{"id":11451,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11450\/revisions\/11451"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}