AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Grep examples4/29/2023 ![]() ![]() It is in the same directory as my first file. It interpret PATTERN as an extended regular expression.Now I have created another file called LNU.txt which has the following lines of text as follows. The egrep is the same as grep -E command. Grep 'bash\>' /etc / passwd grep '\' /etc / passwd Table 1: grep (egrep) regex operator Operator I hope following table will help you quickly understand regular expressions in grep when using under Linux or Unix-like systems: $ grep -o regex filename grep Regular Expression Operator $ grep -color regex filename How Do I Show Only The Matches, Not The Lines? $ egrep ']" filename How Do I Highlight with grep? The following example will only match an IP address: ![]() The following regex to find an IP address 192.168.1.254 will not work (remember the dot matches any single character?):Īll three dots need to be escaped: 192.168.2.18 centos7 Say you just want to match an IP address 192.168.2.254 and nothing else. Print all lines with exactly two characters:ĭisplay any lines starting with a dot and digit: \ Match the empty string at the end of word.$ grep '' filename How negates matching in sets In this example match all upper case letters: ] – Upper-case letters: ‘A B C D E F G H I J K L M N O P Q R S T U V W X Y Z’.] – Space characters: tab, newline, vertical tab, form feed, carriage return, and space.] – Lower-case letters: ‘a b c d e f g h i j k l m n o p q r s t u v w x y z’.Within a bracket expression, the name of a character class enclosed in “” stands for the list of all characters belonging to that class. You are not limited to digits, you can match at least one letter:ĭisplay all the lines containing either a “w” or “n” character: In other words match foo11, foo12, foo22 and so on, enter: In this example match two numeric digits. For example, try to math words such as vivek1, Vivek2 and so on: Let us match digits and upper and lower case characters. Say you want to Match both ‘Vivek’ or ‘vivek’: You can match specific characters and character ranges using syntax. The dot (.) matches any single character. ![]() Matching Sets of Characters How to match sets of character using grep You can search for blank lines with the following examples: You can display only lines starting with the word vivek only i.e. The following example displays lines starting with the vivek only: You can use ^ and $ to force a regex to match only at the start or end of a line, respectively. You can escape the dot (.) by preceding it with a \ (backslash): But, what if you need to match dot (.) only? I want to tell my grep command that I want actual dot (.) character and not the regex special meaning of the. Our final example find all filenames starting with purchase but ending with db:Ī dot (.) has a special meaning in regex, i.e. Next I need to find all filenames starting with purchase and followed by another character: Let us find all filenames starting with purchase, type: character (period, or dot) matches any one character. $ grep -E -i '^(linux|unix)' filename How to match single characters # Same as above by passing the '-E' to the grep # The following will match word Linux or UNIX in any case using the egrep command: The PATTERN in last example, used as an extended regular expression. Let us try to search two words ‘vivek’ or ‘raj’ in any case: Next, search for a word named ‘vivek’ in any case (i.e. Sample outputs: vivek:x:1000:1000:Vivek Gite,:/home/vivek:/bin/bash Search for a word named ‘vivek’ in the /etc/passwd file: The grep understands three different types of regular expression syntax as follows: ![]()
0 Comments
Read More
Leave a Reply. |