How do I use the Grep command with regular expressions on a Linux and Unix-like operating systems?Linux comes with GNU grep, which supports extended regular expressions. GNU grep is the default on all Linux systems. The grep command is used to locate information stored anywhere on your server or workstation.Regular ExpressionsRegular Expressions is nothing but a pattern to match for each input line. A pattern is a sequence of characters. Following all are examples of pattern:^w1w1|w2[^ ]grep Regular Expressions ExamplesSearch for vivek in /etc/passswdgrep vivek /etc/passwdSample outputs:vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bashvivekgite:x:1001:1001::/home/vivekgite:/bin/shgitevivek:x:1002:1002::/home/gitevivek:/bin/shSearch vivek in any case (i.e. case insensitive search)grep -i -w vivek /etc/passwdSearch vivek or raj in any casegrep -E -i -w 'vivek|raj' /etc/passwdThe PATTERN in last example, used as an extended regular expression.AnchorsYou can use ^ and $ to force a regex to match only at the start or end of a line, respectively. The following example displays lines starting with the vivek only:grep ^vivek /etc/passwdSample outputs:vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bashvivekgite:x:1001:1001::/home/vivekgite:/bin/shYou can display only lines starting with the word vivek only i.e. do not display vivekgite, vivekg etc:grep -w ^vivek /etc/passwdFind lines ending with word foo:grep 'foo$' filenameMatch line only containing foo:grep '^foo$' filenameYou can search for blank lines with the following examples:grep '^$' filenameCharacter ClassMatch Vivek or vivek:grep '[vV]ivek' filenameORgrep '[vV][iI][Vv][Ee][kK]' filenameYou can also match digits (i.e match vivek1 or Vivek2 etc):grep -w '[vV]ivek[0-9]' filenameYou can match two numeric digits (i.e. match foo11, foo12 etc):grep 'foo[0-9][0-9]' filenameYou are not limited to digits, you can match at least one letter:grep '[A-Za-z]' filenameDisplay all the lines containing either a w or n character:grep [wn] filenameWithin a bracket expression, the name of a character class enclosed in [: and :] stands for the list of all characters belonging to that class. Standard character class names are:[:alnum:] Alphanumeric characters.[:alpha:] Alphabetic characters[:blank:] Blank characters: space and tab.[:digit:] Digits: 0 1 2 3 4 5 6 7 8 9.[:lower:] 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.[:space:] Space characters: tab, newline, vertical tab, form feed, carriage return, and space.[:upper:] 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.In this example match all upper case letters:grep '[:upper:]' filenameWildcardsYou can use the . for a single character match. In this example match all 3 character word starting with b and ending in t:grep '\' filenameWhere,\< Match the empty string at the beginning of word\> Match the empty string at the end of word.Print all lines with exactly two characters:grep '^..$' filenameDisplay any lines starting with a dot and digit:grep '^\.[0-9]' filenameEscaping the dotThe following regex to find an IP address 192.168.1.254 will not work:grep '192.168.1.254' /etc/hostsAll three dots need to be escaped:grep '192\.168\.1\.254' /etc/hostsThe following example will only match an IP address:egrep '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' filenameThe following will match word Linux or UNIX in any case:egrep -i '^(linux|unix)' filenameHow Do I Search a Pattern Which Has a Leading Symbol?Searches for all lines matching test using -e option