Linux+: Linux Shell 19 – Grep Command

J

Jarret W. Buse

Guest
Linux+: Linux Shell 19 – Grep Command

The Global Regular Expression Print (grep) command is used to search for patterns within a file. The feature is very handy for finding specific strings of characters in a large file. It is easy to determine if the string exists within the file so the file does not have to be opened and searched which can require more system resources and time.

NOTE: It is assumed that grep stands for Global Regular Expression Print. There are many variations of what grep stands for, but Global Regular Expression Print seems to be predominant.

The standard syntax for grep is:

grep options “search_string” filenames

The “search_string” is what you are looking for within the file or files you specify. If multiple files are searched, they are listed one after another separated by a space.

The basic options for grep are as follows:

  • -i (--ignore-case) – case-insensitive search

grep -i “error” logfile – searches a file called “logfile” for any occurrence of the word “error” no matter if any letter or letters are upper-case

  • -w (--word-regexp) – matches whole word in “search_string”

grep -w “error” logfile – searches a file called “logfile” for any occurrence of the whole word “error”. The option will not return the line if the word found is “errors”, “errorcode”, etc.

  • -A x (--after-context=x) – displays x number of lines after the line which matches the “search_string”
grep -A 5 “error” logfile – finds each line containing the string “error” and prints it as well as the next five lines

  • -B x (--before-context=x) – displays x number of lines before the line which matches the “search_string”
grep -B 5 “error” logfile – finds each line containing the string “error” and prints it as well as the five lines before the match

  • -C x (--context=x) – displays x number of lines before and after the line which matches the “search-string”
grep -C 5 “error” logfile – finds each line containing the string “error” and prints it as well as the five lines before and five lines after the match

  • -v (--invert-match) – performs the specified search and lists lines which do not match the “search_string”
grep -v “error” logfile – searches logfile and displays all lines which do not include the “search_string” of “error”

  • -e (--regexp=”search_string”) – used to specify multiple “search_strings”
grep -e “string1” -e “string2” logfile – searches the logfile for both “search_strings”, in this case, “string1” and “string2”

  • -c (--count) – counts number of matches within the file and displays the match count
grep -c “error” logfile – searches logfile and counts how many times the string “error” occurs

  • -l (--files-with-matches)– lists the filenames of the files which contain the “search_string”

grep -l “error” logfile* - searches all files which start with “logfile” and lists the filename for those which contain the word “error”

  • -L (--files-without-match)– lists the filenames of the files which do not contain the “search_string”

grep -L “error” logfile* - searches all files which start with “logfile” and lists the filename for those which do not contain the word “error”

  • -n (--line-number) – lists the line number of the line which contains “search_string”

grep -n “error” logfile – searches the logfile and finds the occurrences of “error” and lists the line along with the line number of the matching line

  • -r (--recursive) – recursive search of a folder and sub-directories

grep -r “error” /logs/ - searches all files in the folder /logs as well as its sub-directories and their files for the string “error”

  • --color – lists matches in color

grep --color “error” logfile – finds the lines with the string “error” and prints the line. The string will be printed with a different color

  • ^“search_string” - string is located at the beginning of the line
grep ^“error” logfile – searches for line in logfile which start with the string “error”

  • “search_string$” - string is located at the end of the line
grep “error$” logfile – searches the logfile for lines which end with “error”

  • “\<search_string” - specifies the search string which begins a word

grep “\<wi” logfile – displays lines with words which begin with “wi”

  • “search_string\>” - specifies the search string which ends a word

grep “ed\>” logfile – displays lines with words which end in “ed”

  • --help – shows help information for usage of grep
  • --version – shows version information for the installed grep command

These are a basic list of the options for grep. The command is a very powerful one which can be used for many purposes. It is a command with which you should become very familiar and comfortable using.
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 13,044

Members online


Top