{"id":159,"date":"2018-07-13T06:21:20","date_gmt":"2018-07-13T06:21:20","guid":{"rendered":"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=159"},"modified":"2019-05-16T07:20:56","modified_gmt":"2019-05-16T07:20:56","slug":"manipulating-text-in-gnu-linux","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/chapter\/manipulating-text-in-gnu-linux\/","title":{"rendered":"Manipulating Text in GNU\/Linux"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/1OIUscDkWqY\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 <\/strong>\r\n\r\n<strong>Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Databases are used by most of the computer professionals to store records. In earlier days, before the database systems were developed data was stored in the text format. Huge text files used to exist that contained thousands of records. In the current scenario, searching the World Wide Web is possible due to search engines like Google, Yahoo etc. It will be quite interesting to learn how Google works. What Google does is, it searches the keywords across the entire web documents and tries to find the matching documents for producing results. However the difference between organization\u2019s data and Google\u2019s data is that organizations maintain\u00a0<span style=\"text-align: initial; font-size: 1em;\">structured data whereas Google searches are within unstructured documents or web pages. In both the cases, although the files may be quite large and the number of files may be more, searching multiple huge files in very less time is possible due to filters like grep, sed etc. In this chapter we will discuss the grep utility and its variants. Such utilities help in searching a filename from a directory listing, searching lines with a specific word from text documents, keyword from a program\u2019s source code etc.<\/span><\/p>\r\n\r\n<\/div>\r\n<strong>Regular Expression<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">When we search within any text document, it implies that we are looking for a matching pattern in any document. A regular expression is a text string (pattern) consisting of sequence of characters that is matched against a text document. For example, \u201ccolou?r\u201d is a regular expression which consists of alphabets and some special characters. A regular expression is made up of atoms and operators. An atom specifies what we are looking inside the text; operator helps is generating various combination of regular expression. An operator is not required in all regular expressions. Table .1 classifies atoms into various types.<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-160 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/444.png\" alt=\"\" width=\"665\" height=\"354\" \/><\/p>\r\n\r\n<div>\r\n<p style=\"text-align: justify;\">Characters like * (asterisk), ^ (caret), $(dollar), ?(question mark), .(dot) has special meaning in Linux and Unix. They can be used with regular expression to search within a text document, for example, they can help to search for a particular word that begins or ends with a particular pattern or searching a line that begins or ends with a particular pattern. The use of each one of them will be explored in the subsequent sections. The combination of atoms and\u00a0<span style=\"text-align: initial; font-size: 1em;\">operators works like a Swiss knife for a programmer. Table 2 classifies expressions into various types.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-161 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/5555.png\" alt=\"\" width=\"655\" height=\"247\" \/><\/p>\r\n\r\n<div>\r\n<p style=\"text-align: justify;\">Utilities available in Linux and Unix support most of the operators while using the regular expressions. Regular expressions are supported by many programming languages like Perl, PHP etc, they are also supported by Linux utilities like awd, grep, sed etc. In the following sections the family of grep command is discussed preceded by the discussion on use of metacharacters in regular expressions. It must be noted that not all the operators are supported by grep utility.<\/p>\r\n&nbsp;\r\n\r\n<strong>Pattern Searching with grep<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Grep is one of the most extensively used utility to search patterns from files. Grep is an acronym of \u201cGlobally Search a Regular Expression\u201d. Grep is a family of utilities for pattern searching, it includes commands like grep, egrep (extended grep) and fgrep (fixed character grep). Pattern searching has already been explored to some extent in chapter 3, where the features of vim editor were discussed to search and replace patterns. This section extensively discusses grep command and related utilities.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Grep command can be used to search patterns from files or output of other commands. Grep command saves the effort to locate patterns from a file without opening the file in any editor. Many tasks can be automated by using combination of grep and other commands through pipes. Grep being a filter can be used on left side or right side of a pipe. For example a user can easily find out whether his friend has logged in or not.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Grep is a pattern searching utility, it simply displays the lines to the standard output containing the matching pattern from a file. It must be noted that grep does not provide any\u00a0<span style=\"text-align: initial; font-size: 1em;\">option to process partial files, nor does it allow to add\/delete\/modify the lines within files, the users cannot search within a file by specifying criteria like line numbers.<\/span><\/p>\r\n\r\n<\/div>\r\nThe syntax of grep command is :\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-162 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/777.png\" alt=\"\" width=\"514\" height=\"501\" \/><\/p>\r\n\r\n<div>\r\n<p style=\"text-align: justify;\">Grep command can accept multiple file-names as its input, suppose the search pattern contains multiple words then grep command will consider the first argument to be a pattern and second to be filename, in such scenarios the pattern or regular expression must be quoted. Grep command remains silent and does not display any output if no matching pattern is found in the input file.<span style=\"text-align: initial; font-size: 1em;\">Grep command can be used with combination of other commands using pipes. The output of unix command can be input to the grep command. The following syntax illustrates the use of grep command in combination with other commands<\/span><\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-163 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/8888.png\" alt=\"\" width=\"521\" height=\"283\" \/><\/p>\r\n<p style=\"text-align: justify;\">The above screen displays that user demo has currently logged in, it also helps to identfiy the number of terminals being used by the user.<\/p>\r\n&nbsp;\r\n\r\n<strong>4 Learning grep through examples<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">This section continues the discussion of grep and explores the command in more detail through various illustrations. A sample file emp.txt is used that contains the employee database of any organization, the file contains records of employees separated by '-' character as a delimeter. The following screen displays the contents of emp.txt file. The first field is employee-id, second field is employee name, third field indicates the designation, fourth field is the department of the employee, fifth field signifies the salary and the last field is the highest qualification of the employee.<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-164 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/546546.jpg\" alt=\"\" width=\"826\" height=\"245\" \/><\/p>\r\n<p style=\"text-align: center;\"><strong>Figure: Contents of file emp.txt<\/strong><\/p>\r\n&nbsp;\r\n\r\nCase-1: List the managers\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">To display the employee with the designation of Manager, we simply search for the pattern MANAGER from the file. It must be noted that grep command is case sensitive. The following screens illustrate the search for manager in employee database<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-165 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/435435.png\" alt=\"\" width=\"515\" height=\"205\" \/><\/p>\r\n<p style=\"text-align: justify;\">In many cases, the grep command remains silent if no matching pattern is found and does not display any output on the screen. A user can check whether grep command executed successfully or not using the $? variable. If the value of $? displays 0, it signifies that grep executed successfully and found a pattern while the value 1 signifies that grep did not find any matching pattern. The following screen illustrates that a given pattern is not found in the employee database.<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-166 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-1.png\" alt=\"\" width=\"515\" height=\"205\" \/> <img class=\"size-full wp-image-167 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/4345555.jpg\" alt=\"\" width=\"826\" height=\"119\" \/><\/p>\r\n<p style=\"text-align: center;\"><strong>Figure: Grep remains silent when no match exists<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The $? variable signifies exit status of the last executed command. In the above example, grep remains silent (manager pattern in lower-case does not exist) and does not display any output, so by echoing the contents of $? variable a user can judge whether grep found a pattern or not. Here $? returns the value 1 which signifies failure of grep command. The $? is very helpful in shell programming and its use will be discussed in detail in further chapters.<\/p>\r\n&nbsp;\r\n\r\nCase-2 Count the number of Managers\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Options like -c and -n can be used to count the number of occurrences of any pattern and display the line numbers of the matching patterns respectively.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-168 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/1-3.jpg\" alt=\"\" width=\"886\" height=\"173\" \/><\/p>\r\n<p style=\"text-align: center;\"><strong>Figure: Grep options -c and -n<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the above illustration, grep command when used with -c option displays the count of the number of managers in the employee database while the -n option displays the line numbers (1 and 6) that contain the pattern manager in employee database.<\/p>\r\n&nbsp;\r\n\r\nCase-3 Match complete words\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Grep command searches for the regular expression. Even though if a user supplies sub-strings instead of entire words, grep displays the lines for corresponding patterns. To avoid this, options like -w will match complete word and will discard the sub-strings.<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-169 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/2.jpg\" alt=\"\" width=\"886\" height=\"173\" \/><\/p>\r\n<p style=\"text-align: center;\"><strong>Figure: Match complete words<\/strong><\/p>\r\n&nbsp;\r\n\r\nIn the above illustration, -w matches the complete word 'MANAGER' and does not display the output for sub-strings like 'MANA'.\r\n\r\n&nbsp;\r\n\r\nCase-4 List Managers from multiple files\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Suppose there are two files, emp.txt and sec.txt and the users wants to find the pattern 'MANAGER' from both the files then the user can supply multiple files to the command grep.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-170 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/3.jpg\" alt=\"\" width=\"886\" height=\"137\" \/><\/p>\r\n<p style=\"text-align: center;\"><strong>Figure: Grep from multiple files<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The output of grep command has a column in the beginning specifying the file from which patterns are located.<\/p>\r\n&nbsp;\r\n\r\nCase-5 List Managers and Clerks\r\n\r\nMultiple patterns can be found from files using -e option.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"alignnone size-full wp-image-171\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/4.jpg\" alt=\"\" width=\"886\" height=\"173\" \/><\/p>\r\n<p style=\"text-align: center;\"><strong>Figure: Search multiple patterns<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The above screen illustrates that patterns 'MANAGER' and 'CLERK' can be searched together using the -e option. A user can search multiple patterns from multiple files aswell. The egrep variant of grep provides similar functionality and is explored in the end of the chapter.<\/p>\r\n&nbsp;\r\n\r\nCase-6 List all employees who are not Managers\r\n<p style=\"text-align: justify;\">Inverse search can be done by providing the -v option of grep command. This option will display the records that do not contain 'MANAGER' pattern.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-172 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/5.jpg\" alt=\"\" width=\"838\" height=\"223\" \/><\/p>\r\n<p style=\"text-align: center;\"><strong>Figure: Patterns not containing MANAGER<\/strong><\/p>\r\n&nbsp;\r\n\r\nCase-7 List files that contain Managers\r\n\r\n&nbsp;\r\n\r\nThe -l option will list all the files that contains a pattern from the list supplied to grep command.\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-173 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/6.jpg\" alt=\"\" width=\"886\" height=\"173\" \/><\/p>\r\n<p style=\"text-align: center;\"><strong>Figure: Display files containing a pattern<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The above screen illustrates that two files, emp.txt and sec.txt are supplied to the grep command to check whether they contain a pattern or not, the output lists both the files containing a given pattern.<\/p>\r\n&nbsp;\r\n\r\nCase- 8 List a particular employee\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Quotes must be used while looking for a pattern with multiple words. Suppose the user wants to display the record of a user named Kavita Majumder then both the arguments cannot be supplied to grep command, grep will search for Kavita as a pattern and will treat Majumder as a filename instead the proper usage is<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-174 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/7.jpg\" alt=\"\" width=\"838\" height=\"206\" \/><\/p>\r\n<p style=\"text-align: center;\"><strong>Figure: Multiple patterns to grep command<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">It must be noted that if a pattern itself contains a single quote then the grep command can be supplied the pattern with double quotes. The following illustration explains giving an example of pattern \u2013 India's<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-175 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/8.png\" alt=\"\" width=\"508\" height=\"29\" \/><\/p>\r\n<strong>5 Using Metacharacters<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">This section provides examples of using various metacharacters with grep. Wild cards used by shell has already been discussed in the earlier chapters. This section explores pattern matching using wild card characters and formulating regular expressions. It must be noted that the use of regular expressions must be within quote marks and the metacharacters of shell may have different interpretation than those discussed here.<\/p>\r\nCase-1 Using ^\r\n<p style=\"text-align: justify;\">The symbol ^ is known as caret or circumflex character. It can be used to search and display the lines that start with a specific pattern. The ^ sign is prefixed to a pattern. For instance suppose the user wants to list the directories then the command can be<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-176 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/9.png\" alt=\"\" width=\"530\" height=\"190\" \/><\/p>\r\n\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Manipulating Text in GNU\/Linux<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/1OIUscDkWqY\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<strong>References<\/strong>:\r\n<ol>\r\n \t<li style=\"text-align: justify;\">Jon. Emmons and Terry Clark. 2006. <em>Easy Linux commands\u202f: working examples of\u00a0<\/em><em style=\"text-align: initial; font-size: 1em;\">Linux command syntax<\/em><span style=\"text-align: initial; font-size: 1em;\">, Rampant TechPress.<\/span><span style=\"text-align: initial; font-size: 1em;\">Richard Blum. 2008. <\/span><em style=\"text-align: initial; font-size: 1em;\">Linux command line and shell scripting bible<\/em><span style=\"text-align: initial; font-size: 1em;\">, Wiley Pub.<\/span><\/li>\r\n \t<li style=\"text-align: justify;\">William E. Shotts. 2012. <em>The Linux command line\u202f: a complete introduction<\/em>, No Starch Press.<\/li>\r\n \t<li style=\"text-align: justify;\">Daniel J. Barrett. 2016. <em>Linux pocket guide\u202f: essential commands<\/em>, O\u2019Reilly Media.<\/li>\r\n \t<li style=\"text-align: justify;\">Mark G. Sobell and Matthew Helmke. <em>A practical guide to Linux commands, editors,<\/em> <em>and shell programming<\/em>,<\/li>\r\n<\/ol>\r\n<\/div>\r\n<\/div>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/1OIUscDkWqY\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p><strong>\u00a0 \u00a0 <\/strong><\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Databases are used by most of the computer professionals to store records. In earlier days, before the database systems were developed data was stored in the text format. Huge text files used to exist that contained thousands of records. In the current scenario, searching the World Wide Web is possible due to search engines like Google, Yahoo etc. It will be quite interesting to learn how Google works. What Google does is, it searches the keywords across the entire web documents and tries to find the matching documents for producing results. However the difference between organization\u2019s data and Google\u2019s data is that organizations maintain\u00a0<span style=\"text-align: initial; font-size: 1em;\">structured data whereas Google searches are within unstructured documents or web pages. In both the cases, although the files may be quite large and the number of files may be more, searching multiple huge files in very less time is possible due to filters like grep, sed etc. In this chapter we will discuss the grep utility and its variants. Such utilities help in searching a filename from a directory listing, searching lines with a specific word from text documents, keyword from a program\u2019s source code etc.<\/span><\/p>\n<\/div>\n<p><strong>Regular Expression<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">When we search within any text document, it implies that we are looking for a matching pattern in any document. A regular expression is a text string (pattern) consisting of sequence of characters that is matched against a text document. For example, \u201ccolou?r\u201d is a regular expression which consists of alphabets and some special characters. A regular expression is made up of atoms and operators. An atom specifies what we are looking inside the text; operator helps is generating various combination of regular expression. An operator is not required in all regular expressions. Table .1 classifies atoms into various types.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-160 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/444.png\" alt=\"\" width=\"665\" height=\"354\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/444.png 665w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/444-300x160.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/444-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/444-225x120.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/444-350x186.png 350w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/p>\n<div>\n<p style=\"text-align: justify;\">Characters like * (asterisk), ^ (caret), $(dollar), ?(question mark), .(dot) has special meaning in Linux and Unix. They can be used with regular expression to search within a text document, for example, they can help to search for a particular word that begins or ends with a particular pattern or searching a line that begins or ends with a particular pattern. The use of each one of them will be explored in the subsequent sections. The combination of atoms and\u00a0<span style=\"text-align: initial; font-size: 1em;\">operators works like a Swiss knife for a programmer. Table 2 classifies expressions into various types.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-161 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/5555.png\" alt=\"\" width=\"655\" height=\"247\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5555.png 655w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5555-300x113.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5555-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5555-225x85.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5555-350x132.png 350w\" sizes=\"auto, (max-width: 655px) 100vw, 655px\" \/><\/p>\n<div>\n<p style=\"text-align: justify;\">Utilities available in Linux and Unix support most of the operators while using the regular expressions. Regular expressions are supported by many programming languages like Perl, PHP etc, they are also supported by Linux utilities like awd, grep, sed etc. In the following sections the family of grep command is discussed preceded by the discussion on use of metacharacters in regular expressions. It must be noted that not all the operators are supported by grep utility.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Pattern Searching with grep<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Grep is one of the most extensively used utility to search patterns from files. Grep is an acronym of \u201cGlobally Search a Regular Expression\u201d. Grep is a family of utilities for pattern searching, it includes commands like grep, egrep (extended grep) and fgrep (fixed character grep). Pattern searching has already been explored to some extent in chapter 3, where the features of vim editor were discussed to search and replace patterns. This section extensively discusses grep command and related utilities.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Grep command can be used to search patterns from files or output of other commands. Grep command saves the effort to locate patterns from a file without opening the file in any editor. Many tasks can be automated by using combination of grep and other commands through pipes. Grep being a filter can be used on left side or right side of a pipe. For example a user can easily find out whether his friend has logged in or not.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Grep is a pattern searching utility, it simply displays the lines to the standard output containing the matching pattern from a file. It must be noted that grep does not provide any\u00a0<span style=\"text-align: initial; font-size: 1em;\">option to process partial files, nor does it allow to add\/delete\/modify the lines within files, the users cannot search within a file by specifying criteria like line numbers.<\/span><\/p>\n<\/div>\n<p>The syntax of grep command is :<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-162 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/777.png\" alt=\"\" width=\"514\" height=\"501\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/777.png 514w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/777-300x292.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/777-65x63.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/777-225x219.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/777-350x341.png 350w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/p>\n<div>\n<p style=\"text-align: justify;\">Grep command can accept multiple file-names as its input, suppose the search pattern contains multiple words then grep command will consider the first argument to be a pattern and second to be filename, in such scenarios the pattern or regular expression must be quoted. Grep command remains silent and does not display any output if no matching pattern is found in the input file.<span style=\"text-align: initial; font-size: 1em;\">Grep command can be used with combination of other commands using pipes. The output of unix command can be input to the grep command. The following syntax illustrates the use of grep command in combination with other commands<\/span><\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-163 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/8888.png\" alt=\"\" width=\"521\" height=\"283\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8888.png 521w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8888-300x163.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8888-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8888-225x122.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8888-350x190.png 350w\" sizes=\"auto, (max-width: 521px) 100vw, 521px\" \/><\/p>\n<p style=\"text-align: justify;\">The above screen displays that user demo has currently logged in, it also helps to identfiy the number of terminals being used by the user.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4 Learning grep through examples<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">This section continues the discussion of grep and explores the command in more detail through various illustrations. A sample file emp.txt is used that contains the employee database of any organization, the file contains records of employees separated by &#8216;-&#8216; character as a delimeter. The following screen displays the contents of emp.txt file. The first field is employee-id, second field is employee name, third field indicates the designation, fourth field is the department of the employee, fifth field signifies the salary and the last field is the highest qualification of the employee.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-164 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/546546.jpg\" alt=\"\" width=\"826\" height=\"245\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/546546.jpg 826w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/546546-300x89.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/546546-768x228.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/546546-65x19.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/546546-225x67.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/546546-350x104.jpg 350w\" sizes=\"auto, (max-width: 826px) 100vw, 826px\" \/><\/p>\n<p style=\"text-align: center;\"><strong>Figure: Contents of file emp.txt<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Case-1: List the managers<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">To display the employee with the designation of Manager, we simply search for the pattern MANAGER from the file. It must be noted that grep command is case sensitive. The following screens illustrate the search for manager in employee database<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-165 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/435435.png\" alt=\"\" width=\"515\" height=\"205\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435.png 515w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-300x119.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-225x90.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-350x139.png 350w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/p>\n<p style=\"text-align: justify;\">In many cases, the grep command remains silent if no matching pattern is found and does not display any output on the screen. A user can check whether grep command executed successfully or not using the $? variable. If the value of $? displays 0, it signifies that grep executed successfully and found a pattern while the value 1 signifies that grep did not find any matching pattern. The following screen illustrates that a given pattern is not found in the employee database.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-166 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-1.png\" alt=\"\" width=\"515\" height=\"205\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-1.png 515w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-1-300x119.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-1-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-1-225x90.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/435435-1-350x139.png 350w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-167 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/4345555.jpg\" alt=\"\" width=\"826\" height=\"119\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4345555.jpg 826w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4345555-300x43.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4345555-768x111.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4345555-65x9.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4345555-225x32.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4345555-350x50.jpg 350w\" sizes=\"auto, (max-width: 826px) 100vw, 826px\" \/><\/p>\n<p style=\"text-align: center;\"><strong>Figure: Grep remains silent when no match exists<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The $? variable signifies exit status of the last executed command. In the above example, grep remains silent (manager pattern in lower-case does not exist) and does not display any output, so by echoing the contents of $? variable a user can judge whether grep found a pattern or not. Here $? returns the value 1 which signifies failure of grep command. The $? is very helpful in shell programming and its use will be discussed in detail in further chapters.<\/p>\n<p>&nbsp;<\/p>\n<p>Case-2 Count the number of Managers<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Options like -c and -n can be used to count the number of occurrences of any pattern and display the line numbers of the matching patterns respectively.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-168 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/1-3.jpg\" alt=\"\" width=\"886\" height=\"173\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-3.jpg 886w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-3-300x59.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-3-768x150.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-3-65x13.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-3-225x44.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-3-350x68.jpg 350w\" sizes=\"auto, (max-width: 886px) 100vw, 886px\" \/><\/p>\n<p style=\"text-align: center;\"><strong>Figure: Grep options -c and -n<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the above illustration, grep command when used with -c option displays the count of the number of managers in the employee database while the -n option displays the line numbers (1 and 6) that contain the pattern manager in employee database.<\/p>\n<p>&nbsp;<\/p>\n<p>Case-3 Match complete words<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Grep command searches for the regular expression. Even though if a user supplies sub-strings instead of entire words, grep displays the lines for corresponding patterns. To avoid this, options like -w will match complete word and will discard the sub-strings.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-169 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/2.jpg\" alt=\"\" width=\"886\" height=\"173\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2.jpg 886w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-300x59.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-768x150.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-65x13.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-225x44.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-350x68.jpg 350w\" sizes=\"auto, (max-width: 886px) 100vw, 886px\" \/><\/p>\n<p style=\"text-align: center;\"><strong>Figure: Match complete words<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>In the above illustration, -w matches the complete word &#8216;MANAGER&#8217; and does not display the output for sub-strings like &#8216;MANA&#8217;.<\/p>\n<p>&nbsp;<\/p>\n<p>Case-4 List Managers from multiple files<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Suppose there are two files, emp.txt and sec.txt and the users wants to find the pattern &#8216;MANAGER&#8217; from both the files then the user can supply multiple files to the command grep.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-170 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/3.jpg\" alt=\"\" width=\"886\" height=\"137\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3.jpg 886w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-300x46.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-768x119.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-65x10.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-225x35.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-350x54.jpg 350w\" sizes=\"auto, (max-width: 886px) 100vw, 886px\" \/><\/p>\n<p style=\"text-align: center;\"><strong>Figure: Grep from multiple files<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The output of grep command has a column in the beginning specifying the file from which patterns are located.<\/p>\n<p>&nbsp;<\/p>\n<p>Case-5 List Managers and Clerks<\/p>\n<p>Multiple patterns can be found from files using -e option.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-171\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/4.jpg\" alt=\"\" width=\"886\" height=\"173\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4.jpg 886w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-300x59.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-768x150.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-65x13.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-225x44.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-350x68.jpg 350w\" sizes=\"auto, (max-width: 886px) 100vw, 886px\" \/><\/p>\n<p style=\"text-align: center;\"><strong>Figure: Search multiple patterns<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The above screen illustrates that patterns &#8216;MANAGER&#8217; and &#8216;CLERK&#8217; can be searched together using the -e option. A user can search multiple patterns from multiple files aswell. The egrep variant of grep provides similar functionality and is explored in the end of the chapter.<\/p>\n<p>&nbsp;<\/p>\n<p>Case-6 List all employees who are not Managers<\/p>\n<p style=\"text-align: justify;\">Inverse search can be done by providing the -v option of grep command. This option will display the records that do not contain &#8216;MANAGER&#8217; pattern.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-172 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/5.jpg\" alt=\"\" width=\"838\" height=\"223\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5.jpg 838w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5-300x80.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5-768x204.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5-65x17.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5-225x60.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5-350x93.jpg 350w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><\/p>\n<p style=\"text-align: center;\"><strong>Figure: Patterns not containing MANAGER<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Case-7 List files that contain Managers<\/p>\n<p>&nbsp;<\/p>\n<p>The -l option will list all the files that contains a pattern from the list supplied to grep command.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-173 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/6.jpg\" alt=\"\" width=\"886\" height=\"173\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6.jpg 886w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6-300x59.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6-768x150.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6-65x13.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6-225x44.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6-350x68.jpg 350w\" sizes=\"auto, (max-width: 886px) 100vw, 886px\" \/><\/p>\n<p style=\"text-align: center;\"><strong>Figure: Display files containing a pattern<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The above screen illustrates that two files, emp.txt and sec.txt are supplied to the grep command to check whether they contain a pattern or not, the output lists both the files containing a given pattern.<\/p>\n<p>&nbsp;<\/p>\n<p>Case- 8 List a particular employee<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Quotes must be used while looking for a pattern with multiple words. Suppose the user wants to display the record of a user named Kavita Majumder then both the arguments cannot be supplied to grep command, grep will search for Kavita as a pattern and will treat Majumder as a filename instead the proper usage is<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-174 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/7.jpg\" alt=\"\" width=\"838\" height=\"206\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7.jpg 838w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7-300x74.jpg 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7-768x189.jpg 768w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7-65x16.jpg 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7-225x55.jpg 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7-350x86.jpg 350w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><\/p>\n<p style=\"text-align: center;\"><strong>Figure: Multiple patterns to grep command<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">It must be noted that if a pattern itself contains a single quote then the grep command can be supplied the pattern with double quotes. The following illustration explains giving an example of pattern \u2013 India&#8217;s<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-175 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/8.png\" alt=\"\" width=\"508\" height=\"29\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8.png 508w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-300x17.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-65x4.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-225x13.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-350x20.png 350w\" sizes=\"auto, (max-width: 508px) 100vw, 508px\" \/><\/p>\n<p><strong>5 Using Metacharacters<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">This section provides examples of using various metacharacters with grep. Wild cards used by shell has already been discussed in the earlier chapters. This section explores pattern matching using wild card characters and formulating regular expressions. It must be noted that the use of regular expressions must be within quote marks and the metacharacters of shell may have different interpretation than those discussed here.<\/p>\n<p>Case-1 Using ^<\/p>\n<p style=\"text-align: justify;\">The symbol ^ is known as caret or circumflex character. It can be used to search and display the lines that start with a specific pattern. The ^ sign is prefixed to a pattern. For instance suppose the user wants to list the directories then the command can be<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-176 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/9.png\" alt=\"\" width=\"530\" height=\"190\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9.png 530w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9-300x108.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9-225x81.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9-350x125.png 350w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Manipulating Text in GNU\/Linux<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/1OIUscDkWqY\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>References<\/strong>:<\/p>\n<ol>\n<li style=\"text-align: justify;\">Jon. Emmons and Terry Clark. 2006. <em>Easy Linux commands\u202f: working examples of\u00a0<\/em><em style=\"text-align: initial; font-size: 1em;\">Linux command syntax<\/em><span style=\"text-align: initial; font-size: 1em;\">, Rampant TechPress.<\/span><span style=\"text-align: initial; font-size: 1em;\">Richard Blum. 2008. <\/span><em style=\"text-align: initial; font-size: 1em;\">Linux command line and shell scripting bible<\/em><span style=\"text-align: initial; font-size: 1em;\">, Wiley Pub.<\/span><\/li>\n<li style=\"text-align: justify;\">William E. Shotts. 2012. <em>The Linux command line\u202f: a complete introduction<\/em>, No Starch Press.<\/li>\n<li style=\"text-align: justify;\">Daniel J. Barrett. 2016. <em>Linux pocket guide\u202f: essential commands<\/em>, O\u2019Reilly Media.<\/li>\n<li style=\"text-align: justify;\">Mark G. Sobell and Matthew Helmke. <em>A practical guide to Linux commands, editors,<\/em> <em>and shell programming<\/em>,<\/li>\n<\/ol>\n<\/div>\n<\/div>\n","protected":false},"author":4,"menu_order":11,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-hardik-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-159","chapter","type-chapter","status-publish","hentry","contributor-mr-hardik-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":6,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/159\/revisions"}],"predecessor-version":[{"id":687,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/159\/revisions\/687"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/159\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapter-type?post=159"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/contributor?post=159"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/license?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}