Command tutorial example: grep

Use grep and regular expressions to analyze text


This is a second tier child page of RHCSA Topic Overview please refer to the parent page if this is your first visit. Also refer to the side bar for a directory.
This is just a quick run-though about how to use regex (Regular Expressions) with grep

GREP(1)                                                                    General Commands Manual                                                                    GREP(1)  
  -E, --extended-regexp
             Interpret pattern as an extended regular expression (i.e. force grep to behave as egrep).
  -v, --invert-match
             Invert the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)
  -i, --ignore-case
             Ignore case distinctions in both the PATTERN and the input files.  (-i is specified by POSIX.)
  -r, --recursive
             Read all files under each directory, recursively, following symbolic links only if they are on the command line.  This is equivalent to the -d recurse option.
Pattern Matches
* Any string of 0 or more characters.
? Any single character
~ The current user's home directory. ("$HOME" is more reliable)
~username User username's home direcotry
~+ The current working directory
~- The previous working directory
[abc...] Any one character in the enclosed class
[!abc...] Any one character not in the enclosed class
[^abc...] Any one character not in the enclosed class
[[:alpha:]] Any alphabetic character
[[:lower:]] Any lower-case character
[[:upper:]] Any upper-case character
[[:alnum:]] Any alphabetic character or digit
[[:punct:]] Any alphabetic character that is not alphanumeric
[[:digit:]] Any digit 0-9
[[:space:]] Any one whitespace character; may include tabs, newline, or carriage returns, and form feeds as well as space


OR Example:

curl -s | grep -E "TITLE|H1"  

AND Example:

curl -s | grep -E "301.*ed"  

IP SEARCH Example:

grep -E "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" some_file  

Finding configured SSL cipers in web hosting platform:

grep -ir cipher /etc/nginx/  


grep -ir cipher /etc/httpd/  

Bonus Example with sed

Exclude lines with html tags:

curl -s | grep -Ev "<[^>]*>"  

This can also be paired wtih sed to remove html tags: