Can you Grep it????

Simple Grep Commands

The following flags will be the most common pertaining to match control.
Please review man grep for a full explanation of these. I recommend taking
detailed notes explaining these actions in your notebooks.
-e = The -e PATTERN form mirrors the above behavior; however, when explicitly provided as an
option, repeated use allows multiple patterns to be search from a file
-i = Option -i toggles case insensitivity
-v = This option will invert the match. By default grep will print the line that contains a
match of the pattern; -v inverts this such that lines that do not match are sent to stdout

grep “John” names.txt – grep for names with John in it.

Example Output:

John Reyes

John Ortiz

grep -i “Jonathan” names.txt – search for names with Jonathan in it. The -i ignores capitalization.

Example Output:

Jonathan Crutchfield

grep -n “Sean” names.txt –grep for the name Sean and show the number that they are listed.

Example Output: 

4:Sean Coffman

grep “^B” names.txt – Grep out names starting with a B.

Example output: 

Betty Mendoza
Brian Jacobs
Bilbo Baggins

How to grep out names beginning with vowels:
grep -i ‘^[aeiou]’ names.txt  

How to grep out names that begin consonants:
grep -i ‘^[^aeiou]’ names.txt

How to grep out numbers over 1000 starting with the word gator:
grep “^gator[1-9][0-9][0-9][0-9]” servers.txt

grep ‘^#’ ./.htaccess = greps for all anything in a .htaccess starting
with # the # represents starting with (inside the quotes)


grep ‘DB_[^PC]’ ./wp-config.php = can negate a character or sets
of characters by including the caret (^) within our brackets
as such: [^…]. Any characters or range specified in the
character class will be negated

Grep the database credentials of a file.

grep ‘DB_[UNH]’ ./wp-config.php = greps for all the database
credentials. The UNH help narrow down what it is you are looking for. You can take out the H and add a P for the password.
Example output:
define(‘DB_NAME’, ‘johnno_wrdp1’);
define(‘DB_USER’, ‘johnno_wrdp1’);
define(‘DB_HOST’, ‘localhost’);

 Grep out strings starting with letters a-d

grep ‘^[a-d]’ ./lulzstext.txt = Starting with a -d