
Hope you like this post on grep command in Unix with examplesĪwk command examples in Unix: Awk command in Unix or Linux is a powerful command for processing text. Grep is a very useful command for search word, expression in the Unix operating system. Sometimes we want to exclude one directory from grep recursive search grep -r -exclude-dir=log "TOM" * Conclusion (5) Grep excludes directory in recursive search. we can use “grep -n” grep -n "ORA-0600" alert.log (4) When we want to show the line number of the matched pattern within the file. (3) When you are searching error using grep on a huge file, it may be useful to see some lines around the match. (2) Suppose you want to count how many lines matches the given pattern/string, then use the option -c (“grep -c”) grep -c "TOM" 1.txt Example of “grep -l” grep -l ORA-0600 *.trc This will simply print all the file names. (1) Sometimes we just want the grep to show out only the file names which matched the given pattern then we use the -l (lower-case L) option. Here the output of the “ps -ef” command is input for the grep command Some more Important Grep commands Pipe command in Linux lets u input the output of one command to the other command. It discarded all the lines having any of these three words from the output of ps -ef ps -ef| egrep -v 'cat|bad|sat' : It searches all the three words in the file egrep 'cat|bad|sat' file.txt If you want to search multiple words in the same grep command, then use the egrep command in UNIX (17) line starts with “.” and 2 lower case letters} letters grep '^\.' egrep command in Unix (16)any line that starts with a Period “.” grep '^\.' (15) ‘kite’, with or without quotes grep '"*kite"*' (14) ‘kite’ within double quotes grep '"kite"' (13)lines with exactly one character grep '^.$' (12)anything, not a letter or number grep '' (11)any line with at least one letter grep '' 1.txt (10)list your mail grep '^From: ' /usr/mail/$USER (9)search for pairs of numeric digits grep '' file (7)search for TOM, Tom, TOm, or ToM grep 'T' file.txt Grep '\^s' file.txt(6)Search for ‘kite’ or ‘Kite’ (5)lines starting with ‘^s’, “\” escapes the ^ (4)lines containing only ‘kite’ grep '^kite$' (3) ‘kite’ at the end of line grep 'kite$' (2) ‘kite’ at the start of a line grep '^kite' file.txt (1)search file.txt for lines with ‘kite’ grep kite file.txt This can also be done like this also grep 'tom\|bob\|bill\|' file.txt Understanding Regular Expressions : grep regex examples (9) You can search multiple words in a file using “grep -e” grep -e tom -e bob -e bill file.txt The below example searches adpatch.log for word failure in any case grep -w failure adpatch.log (8) We can use the “grep -w” option for searching the specific work, not the sub-string. It will not show the lines which have oracle string in it ps -ef|grep -v oracle We can use grep -v to exclude the search item. Its searches for oracle string in current directory files and all the files in a subdirectory grep -r "oracle" * (5) pipe who to grep, look for applmgr who | grep applmgr (4) find ‘run time’ or ‘run-time’ in all txt in file.txt grep runtime *.txt You can use the option “grep -i” to make it case insensitive. By default grep command is case sensitive. Were an all-eating shame, and thriftless praise.(3) grep case insensitive search. To say within thine own deep sunken eyes, Where all the treasure of thy lusty days Then being asked, where all thy beauty lies, Will be a tattered weed of small worth held: Thy youth's proud livery so gazed on now, When forty winters shall besiege thy brow,Īnd dig deep trenches in thy beauty's field, To eat the world's due, by the grave and thee.

Within thine own bud buriest thy content,Īnd tender churl mak'st waste in niggarding:

Thou that art now the world's fresh ornament, Thy self thy foe, to thy sweet self too cruel: That thereby beauty's rose might never die,īut thou contracted to thine own bright eyes,įeed'st thy light's flame with self-substantial fuel, Display only those lines of an input file, which contain the required words.įrom fairest creatures we desire increase, The search should not be sensitive to case. Given a text file, which will be piped to your command through STDIN, use grep to display all those lines which contain any of the following words in them: More details about common examples of grep usage may be read here.īefore using grep it is recommended that one should become familiar with regular expressions, to be able to harness the command to its fullest. For example, one might opt for case-insensitive search, or to display only the fragment matching the specified search pattern, or to display only the line number of an input file where the specified string or regular expression has been found. A variety of options exist, which make it possible to use the command in several different ways and to handle many different situations.

Grep is a multi-purpose search tool, which is used to find specified strings or regular expressions.
