{"id":1785,"date":"2022-02-24T21:15:00","date_gmt":"2022-02-24T15:45:00","guid":{"rendered":"https:\/\/smarttech101.com\/?p=1785"},"modified":"2022-11-12T02:08:51","modified_gmt":"2022-11-11T20:38:51","slug":"grep-command-in-linux","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/grep-command-in-linux\/","title":{"rendered":"Grep Command in Linux with Examples"},"content":{"rendered":"\n
Grep command in Linux is used to “grapple” any type of strings from all sorts of files. In this article, I will talk about its basic applications with examples. <\/p>\n\n\n\n
-<\/code><\/a><\/li>- Concluding Remarks<\/a><\/li><\/ul>\n\n\n\n
Basics of grep command in Linux<\/h2>\n\n\n\n
Basic Syntax:<\/p>\n\n\n\n
grep [OPTION...] PATTERNS [FILE...]<\/code><\/pre>\n\n\n\nIn the above syntax, <\/p>\n\n\n\n
- OPTION is also called “flags”. The
--recursive<\/code> and -r<\/code> are few of the options of the grep. The three dots ...<\/code> indicate that you can use more than one option.<\/li>- Here, PATTERNS are special types of words being searched. In the example given below it is “it”.<\/li>
- FILE is the name of the file where you want to search for your PATTERN. For the following example, it is “file.txt”<\/li><\/ul>\n\n\n\n
For example, The following grep command prints all the lines having the pattern “it” from the file “file.txt”.<\/p>\n\n\n\n
[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo ~]$ grep \"it\" file.txt\nit is first line.\nit is third line.<\/code><\/pre>\n\n\n\nIf you do not provide any filename and do not use the flag --recursive<\/code>, grep searches from the standard input (|, and <<\/code>). <\/p>\n\n\n\n[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo ~]$ cat file.txt | grep \"it\"\nit is first line.\nit is third line.\n\n[ajay@lenovo ~]$ < file.txt grep \"it\"\nit is first line.\nit is third line.\n\n[ajay@lenovo ~]$ grep \"it\" < file.txt\nit is first line.\nit is third line.<\/code><\/pre>\n\n\n\nSimilarly, the file name dash “-” means the standard input as in the following example:<\/p>\n\n\n\n
[ajay@lenovo ~]$ free --human --total\n total used free shared buff\/cache available\nMem: 6.7Gi 2.0Gi 139Mi 43Mi 4.5Gi 4.3Gi\nSwap: 14Gi 89Mi 14Gi\nTotal: 21Gi 2.1Gi 14Gi\n\n[ajay@lenovo ~]$ free --human --total | grep \"Total\" -\nTotal: 21Gi 2.1Gi 14Gi<\/code><\/pre>\n\n\n\nHow to make grep search case-insensitively<\/h2>\n\n\n\n
By default, grep searches case-sensitively. This means, grep will consider “smarttech101”, for example, and “SmartTech101” two different words.<\/p>\n\n\n\n
To make grep ignore the case, use the flag -i or --ignore-case<\/code>.<\/p>\n\n\n\nExample:<\/p>\n\n\n\n
[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo ~]$ grep --ignore-case \"It\" file.txt\nit is first line.\nit is third line.<\/code><\/pre>\n\n\n\nHow to grep multiple patterns<\/h2>\n\n\n\n
If you use --regexp<\/code> or -e multiple times, grep will search for all patterns given.<\/p>\n\n\n\n[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo ~]$ grep --regexp=\"first\" --regexp=\"second\" file.txt\nit is first line.\nsecond line.<\/code><\/pre>\n\n\n\nAlternatively, you can put all of your patterns in a file separated by a newline character and then use that file with the flag --file<\/code> or -f<\/code>. Example:<\/p>\n\n\n\n[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo test]$ cat pattern-file.txt\nfirst\nsecond\n\n[ajay@lenovo test]$ grep --file=pattern-file.txt file.txt\nit is first line.\nsecond line.<\/code><\/pre>\n\n\n\nNote 1: <\/strong>You can use both the flags --file<\/code> and --regexp<\/code> together. In that case, grep will search for all the patterns together.<\/p>\n\n\n\nNote 2<\/strong>: When the flag --regexp<\/code> used only once, grep behaves normally as shown below: <\/p>\n\n\n\n[ajay@lenovo ~]$ grep --regexp=\"first\" file.txt\nit is first line.<\/code><\/pre>\n\n\n\nHow to grep pattern(s) in all files in a directory recursively<\/h2>\n\n\n\n
To search for a pattern in all the existing files in the current working directory, you need to use the flag --recursive<\/code> or -r.<\/p>\n\n\n\n[ajay@lenovo test]$ ls -1\nfile2.txt\nfile.txt\n\n[ajay@lenovo test]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo test]$ cat file2.txt\nI means first\nII means 2nd\nIII means third\n\n[ajay@lenovo test]$ grep --recursive \"first\"\nfile2.txt:I means first\nfile.txt:it is first line.<\/code><\/pre>\n\n\n\n\ud83d\udcd3<\/strong> Notes:<\/strong> to remove the filename, use the flag --no-filename<\/code> or -h<\/code>:<\/p>\n\n\n\n[ajay@lenovo test]$ grep --no-filename --recursive \"first\"\nI means first\nit is first line.<\/code><\/pre>\n\n\n\nHow to print the line number as well in grep<\/h2>\n\n\n\n
The flag --line-number<\/code> or -n prepends each line in the output with the serial number of that line in its input file. <\/p>\n\n\n\nFor instance, in the following example, the lines “it is third line.” and “III means third” are found in the third line of their respective files. Therefore, ‘3’ is being prepended in both lines of the output.<\/p>\n\n\n\n
[ajay@lenovo test]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo test]$ cat file2.txt\nI means first\nII means 2nd\nIII means third\n\n[ajay@lenovo test]$ grep --line-number \"third\" file.txt file2.txt\nfile.txt:3:it is third line.\nfile2.txt:3:III means third<\/code><\/pre>\n\n\n\nHow to get colored output using grep<\/h2>\n\n\n\n
Grep outputs in the color using the escape sequences. For this, use the flag --color=WHEN<\/code>, where WHEN is<\/p>\n\n\n\nalways<\/code> to output in color always<\/li>never<\/code> to never output in color<\/li>auto<\/code>: grep uses the escape sequences when it outputs on the terminal. When it outputs into the pipe, it does not use the sequences. <\/li><\/ul>\n\n\n\n
Figure: grep with colored output<\/figcaption><\/figure>\n\n\n\nNote 1: <\/strong>If you mention only the flag --color<\/code> and not the =WHEN<\/code>, grep will assume auto<\/code>. Ex –<\/p>\n\n\n\n
Figure: grep printing in color by just using the --color<\/code><\/figcaption><\/figure>\n\n\n\nNote 2: <\/strong>The flag --color=auto<\/code> is very handy. Hence, you can set an alias in your .bashrc<\/strong> or .zshrc<\/strong> file.<\/p>\n\n\n\nFun Point \ud83d\ude03: <\/strong>This flag, when combined with --line-number<\/code> and --recursive<\/code> is one of the most powerful applications of grep. Suppose you want to see how you used the tee<\/strong> command in the past in your scripts. But you do not know the locations of these scripts. To know the locations, you need to execute the following command:<\/p>\n\n\n\n[ajay@lenovo test]$ grep --line-number --color=always --recursive \"tee\" ~\/.my_scripts\/<\/code><\/pre>\n\n\n\nOutput:<\/p>\n\n\n\n
Figure: grep with colored output<\/figcaption><\/figure>\n\n\n\nHow to print only the matching part in grep<\/h2>\n\n\n\n
In all of the above examples, grep is printing the whole lines in which it found the pattern. To print only the matching pattern, use the flag --only-matching<\/code> or -o<\/code>.<\/p>\n\n\n\n[ajay@lenovo test]$ echo \"smarttech101smarttech101\" | grep --only-matching \"smart\"\nsmart\nsmart<\/code><\/pre>\n\n\n\nPlease note that grep prints all the matching patterns on separate lines even if they were part of the same line. This is clearly visible in the above example.<\/p>\n\n\n\n
Application: <\/strong>You can use this flag for web scrapping (i.e. get only the required text from HTML pages).<\/p>\n\n\n\nHow to grep only the non-matching lines<\/h2>\n\n\n\n
Using the flag --invert-match<\/code> or -v<\/code>, you can invert the matching i.e. grep will print out only those lines which do not have the PATTERN.<\/p>\n\n\n\n[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo ~]$ grep --invert-match \"second\" file.txt\nit is first line.\nit is third line.<\/code><\/pre>\n\n\n\nHow to make grep to output only the number of matching lines<\/h2>\n\n\n\n
For this, use the --count<\/code> or -c<\/code> flag. Now, grep will output only the number of matching lines corresponding to each file. For example –<\/p>\n\n\n\n[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo ~]$ cat file2.txt\nI means first\n1st also means first\nII means 2nd\nIII means third\n\n[ajay@lenovo ~]$ grep --count \"first\" file.txt file2.txt\nfile.txt:1\nfile2.txt:2<\/code><\/pre>\n\n\n\nIn the above example, the string “first” is found only once in the file.txt but twice in the file2.txt. <\/p>\n\n\n\n
When you use this flag --count<\/code> with the flag --invert-match<\/code>, grep will output the number of non-matching lines. For example –<\/p>\n\n\n\n[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\n\n[ajay@lenovo ~]$ cat file2.txt\nI means first\n1st also means first\nII means 2nd\nIII means third\n\n[ajay@lenovo ~]$ grep --invert-match --count \"first\" file.txt file2.txt\nfile.txt:2\nfile2.txt:2<\/code><\/pre>\n\n\n\nHow to search directly for a word using grep<\/h2>\n\n\n\n
For this, use the flag --word-regexp<\/code> or -w<\/code>. For example,<\/p>\n\n\n\n[ajay@lenovo ~]$ echo -e 'ajay jay patel\\najay\\njay patel\\najay jay\\njaya prada' | grep --word-regexp --color \"jay\"\n\najay jay<\/b><\/font> patel\njay<\/b><\/font> patel\najay jay<\/b><\/font>\n<\/pre>\n\n\n\nHere, in the above example, <\/p>\n\n\n\n
- If “jay” comes at the start of the line, it should be followed by the non-word characters (
\\W<\/code>) which are anything other than alphabets, numbers, and underscore. Hence, the third line from the echo command<\/a>‘s output is matched, but not the fifth one.<\/li>- If “jay” comes at the end of the line, it should be preceded by
\\W<\/code>. Hence, second line does not match, but the fourth line matches.<\/li>- If “jay” comes in between the line, it should be both followed and preceded by
\\W<\/code>. The first line from the echo illustrates this.<\/li><\/ol>\n\n\n\nHow to search directly for a line using grep<\/h2>\n\n\n\n
To search for a line, use the flag --line-regexp<\/code> or -x<\/code>. Example –<\/p>\n\n\n\n[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\nit is not first line.\n\n[ajay@lenovo ~]$ grep --line-regexp \"it is first line.\" file.txt\nit is first line.<\/code><\/pre>\n\n\n\nWhat is PATTERN in grep command in Linux<\/h2>\n\n\n\n
There are four types of patterns in grep – extended regular expressions (EREs), fixed strings, basic regular expressions (BREs), and Perl-compatible regular expressions (PCREs).<\/p>\n\n\n\n
Extended Regular Expressions in grep<\/h3>\n\n\n\n
This is my favorite since I find myself using it most of the time. To use this, use the flag -E<\/code> or --extended-regexp<\/code>. <\/p>\n\n\n\nThe command egrep<\/code> is the same as grep --extended-regexp<\/code>. But, avoid using that since the egrep<\/code> has been deprecated, and hence in the future, it<\/code> might not work.<\/p>\n\n\n\n[ajay@lenovo ~]$ cat file.txt\nit is first line.\nsecond line.\nit is third line.\nit is not first line.\n\n[ajay@lenovo ~]$ grep --extended-regexp \"first|third\" file.txt\nit is first line.\nit is third line.\nit is not first line.<\/code><\/pre>\n\n\n\nIn the above example, grep will search for the “first” OR “third”. Here, is a table of the most widely used regular expressions and their meanings:<\/p>\n\n\n\nRegular expression<\/th> Meaning<\/th><\/tr><\/thead> .<\/td> any single character<\/td><\/tr> *<\/td> the preceding item matching zero or more times<\/td><\/tr> +<\/td> the preceding item matching one or more times<\/td><\/tr> ^<\/td> beginning of the line<\/td><\/tr> $<\/td> end of the line<\/td><\/tr> ?<\/td> the preceding item is optional<\/td><\/tr> [az]<\/td> the character \u201ca\u201d OR \u201cz\u201d<\/td><\/tr> [a-z]<\/td> any letter from a to z (lowercase)<\/td><\/tr> [A-Z]<\/td> any letter from A to Z (uppercase)<\/td><\/tr> [A-Za-z]<\/td> any letter<\/td><\/tr> [0-9]<\/td> any number<\/td><\/tr> {n}<\/td> the preceding item matching exactly n times<\/td><\/tr> {n,}<\/td> the preceding item matching n or more times<\/td><\/tr> {,m}<\/td> the preceding item matching m or less than m times<\/td><\/tr> {n,m}<\/td> the preceding item matching n to m times<\/td><\/tr> \\s<\/td> any space character (space, horizontal and vertical tab, and newline, carriage return, formfeed)<\/td><\/tr> \\S<\/td> any Non-whitespace character (i.e. exact opposite of \\s)<\/td><\/tr><\/tbody><\/table>Table: The most widely used regular expressions and their meanings; source: man page<\/figcaption><\/figure>\n\n\n\n