background preloader

HowTo: Use grep Command In Linux / UNIX – Examples

HowTo: Use grep Command In Linux / UNIX – Examples
How do I use grep command on Linux, Apple OS X, and Unix-like operating systems? Can you give me a simple examples of the grep command? The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. grep is considered as one of the most useful commands on Linux and Unix-like operating systems. Did you know? The name, "grep", derives from the command used to perform a similar operation, using the Unix/Linux text editor ed: g/re/p The grep command syntax The syntax is as follows: grep 'word' filename grep 'word' file1 file2 file3 grep 'string1 string2' filename cat otherfile | grep 'something'command | grep 'something'command option1 | grep 'data'grep --color 'data' fileName How do I use grep command to search a file? foo:x:1000:1000:foo,,,:/home/foo:/bin/ksh

egrep mini-tutorial egrep is an acronym for "Extended Global Regular Expressions Print". It is a program which scans a specified file line by line, returning lines that contain a pattern matching a given regular expression. The standard egrep command looks like: egrep <flags> '<regular expression>' <filename> Some common flags are: -c for counting the number of successful matches and not printing the actual matches, -i to make the search case insensitive, -n to print the line number before each match printout, -v to take the complement of the regular expression (i.e. return the lines which don't match), and -l to print the filenames of files with lines which match the expression. ( The Greek letter ε (epsilon) represents the empty string) egrep '^(0|1)+ [a-zA-Z]+$' searchfile.txt match all lines in searchfile.txt which start with a non-empty bitstring, followed by a space, followed by a non-empty alphabetic word which ends the line egrep -c '^1|01$' lots_o_bits count the number of lines with at least eleven 1's

grep History[edit] Usage[edit] Grep searches files specified as arguments, or, if missing, the program's standard input. By default, it reports matching lines on standard output, but specific modes of operation may be chosen with command line options. A simple example of a common usage of grep is the following, which searches the file fruitlist.txt for lines containing the text string apple: $ grep apple fruitlist.txt Matches occur when the specific sequence of characters is recognized, for example, lines containing pineapple or apples are printed irrespective of word boundaries. $ grep apple *.txt Regular expressions can be used to match more complicated text patterns. $ grep ^a.ple fruitlist.txt $ ed fruitlist.txt g/^a.ple/p q where the second line is the command given to ed to print the relevant lines, and the third line is the command to exit from the editor. Like most Unix commands, grep accepts options in the form of command-line arguments to change its behavior. Variations[edit] See also[edit]

grep, egrep, fgrep -- match patterns in a file grep [] [ pattern] ... [ patternfile] ... [[[[][]][[]]]] [ num] [ num] [ [num]] [ action] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [pattern] [file ...] egrep [] [ pattern] ... [ patternfile] ... [[[[][]][[]]]] [ num] [ num] [ [num]] [ action] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [pattern] [file ...] fgrep [] [ pattern] ... [ patternfile] ... [[[[][]][[]]]] [ num] [ num] [ [num]] [ action] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [pattern] [file ...] fgrep searches files for one or more pattern arguments. egrep works in a similar way, but uses extended regular expression matching (as well as the \< and \> metacharacters) as described in the regexp reference page. Note: When using the man utility to view the regexp reference page, use the following command to ensure that you get the correct reference page: man 5 regexp grep is a combination of fgrep and egrep. Options num =num displays num lines following each matched line. [num] [=num] num action =action

grep () NOTE: click here if you get an empty page. grep, egrep, fgrep - print lines matching a pattern grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...] Grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN. -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. A regular expression is a pattern that describes a set of strings. Grep's behavior is affected by the following environment variables. Normally, exit status is 0 if selected lines are found and 1 otherwise. Email bug reports to bug-grep@gnu.org.

-o (only matching)

Related: