



In the following example, the string “ nobody” will match only if it occurs at the very end of a line. $ : Use the $ (dollar) symbol to match expression at the end of a line. In the following example, the string “ re” will match only if it occurs at the very beginning of a line. ^ : Use the ^ (caret) symbol to match expression at the start of a line. GNU Grep has three regular expression feature() sets, Basic, Extended and Perl-compatible.īy default, grep interprets the pattern as a basic regular expression where all characters except the meta-characters are actually regular expressions that match themselves. # grep -C5 "nobody" /etc/passwd Regular Expression: C n : Prints searched line and n lines after before the result. B n : Prints searched line and n line before the result. A n : Prints searched line and n lines after the result. v : This prints out all the lines that do not matches the pattern, in this example line 18 will not print. n : Display the matched lines and their line numbers. In the example it will display /etc/passwd. h : Display the matched lines, but do not display the filenames. c : This prints only a count of the lines that match a pattern In this example, grep would loop through every line of the file “passwd” and print out every line that contains the word ‘nobady’: Grep command-line Some Useful Options & Description: The simplest possible example of grep is: To be able to search the file, the user running the command must have read access to the file. Grep includes a number of options that control its behavior. Grep will repeat this process until the file runs out of lines. Beginning at the first line in the file, grep copies a line into a buffer, compares it against the search string, and if the comparison passes, prints the line to the screen. In the simplest terms, grep (global regular expression print) will search input files for a search string, and print the lines that match it. It’s name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect. Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file.
