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:
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
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.
grep -l “error” logfile* - searches all files which start with “logfile” and lists the filename for those which contain the word “error”
grep -L “error” logfile* - searches all files which start with “logfile” and lists the filename for those which do not contain the word “error”
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
grep -r “error” /logs/ - searches all files in the folder /logs as well as its sub-directories and their files for the string “error”
grep --color “error” logfile – finds the lines with the string “error” and prints the line. The string will be printed with a different color
grep “\<wi” logfile – displays lines with words which begin with “wi”
grep “ed\>” logfile – displays lines with words which end in “ed”
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.
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”
- -B x (--before-context=x) – displays x number of lines before the line which matches the “search_string”
- -C x (--context=x) – displays x number of lines before and after the line which matches the “search-string”
- -v (--invert-match) – performs the specified search and lists lines which do not match the “search_string”
- -e (--regexp=”search_string”) – used to specify multiple “search_strings”
- -c (--count) – counts number of matches within the file and displays the match count
- -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
- “search_string$” - string is located at the end of the line
- “\<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.