Linux:Pattern searching command

grep command

  • grep is an acronym for globally search a regular expression and print it
  • The command searches the specified input fully (globally) for a match with the supplied pattern and displays it
  • In the following example the command searches for a word raj in the file studentinfo and if found the lines containing it would be displayed on the screen.

Example: grep raj studentinfo

  • You can use grep to search a pattern in several files. In the following example the word raj will be searched for in both the files studentinfo and studentdetails and if found, the lines containing it would be displayed along with the name of the file where it was found. This way we would be able to make out the file from which the line is listed

Example: grep raj studentinfo studentdetails

  • For a search pattern comprising more than a single word , single quotes can be used to enclose the same, as given below

Example: grep ‘Naga Sadhus’ –i –n kumbhamela prayag

  • The above command searches for the pattern enclosed within ‘ ’, it is not case sensitive (-i). The –n option causes the number of the lines in which pattern was found to be printed by the side of each line
  • In the following example grep would search for all the occurrences of Manjunath and manjunath in the file studentinfo and display the lines which contain one of these words

Example: grep [Mm]anjunath studentinfo

  • In the following example you can search for a 5 character word which starts with n and ends with u

Example: grep n???u studentinfo

  • You can also use ‘*’ in search command as wild character. For example grep p* studentinfo will search for all the patterns which starts with p
  • Similarly ‘^[pqr]’ would help us search only those lines which begin with p, q, r
  • Likewise, we can build search pattern which helps only those lines to qualify which end with that pattern. For example ‘[a-c]$’ would help search those lines which end with any character between a to c
  • In the following example output of the long listing ls -l is given as the input to grep command and in the listing it searches and displays only those lines which starts with ‘d’. That means it displays only the names of the directories

Example: ls -l | grep '^d'

  • If you only want to list those files that have symbolic links, you can use the pattern ^l

Example: ls -l | grep '^l'

  • The brackets match on either a set of characters, a range of characters, or a nonmatch of those characters
  • For example, the pattern doc[wxy] matches on the patterns "docw", "docx", and "docy", but not on "docz"
  • The same pattern can be specified with a range: doc[w-z]
  • However, the pattern doc[^xy] will match on any pattern beginning with "doc" but not ending in x or y. Thus, "docz" will be retrieved, but not "docx" or "docy"
  • In the next example, you can find all lines that reference "docw", "docx", or "docy" in the file documents

Example: grep 'doc[wxy]' documents

Linux: Listing of filest << Previous
Next>> Linux: Filter commands cut, wc, spell and sort

Our aim is to provide information to the knowledge seekers. 

comments powered by Disqus