This section explains how to use the grep command for string searches. It introduces frequently-used options and techniques for combining the command with other commands.
format
grep [options] search pattern [file...]
BRE and ERE
You need to be aware of the difference between BRE (basic-regexp) and ERE (extended-regexp).
grep defaults to BRE. The -E option can be used to search with ERE.
Regular Expressions | command |
---|---|
BRE | grep sed |
ERE | grep -E egrep sed -r awk |
For example, the regular expression for either a or b would be a|b
, which is only available with an ERE.
Examples of Use
Search from a single file
Display lines containing the string HttpKernel in the file app/Http/Kernel.php
$ grep "HttpKernel" app/Http/Kernel.php
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
Search from multiple files
Specify multiple paths
Search from app/Http/Kernel.php and app/Console/Kernel.php
$ grep "app" app/Http/Kernel.php app/Console/Kernel.php
directory specification
If all files under the directory app/Http contain the string HttpKernel, display the file name & line number & contents of the line.
$ grep -nr "HttpKernel" app/Http
app/Http/Kernel.php:5:use Illuminate\Foundation\Http\Kernel as HttpKernel;
app/Http/Kernel.php:7:class Kernel extends HttpKernel
Excluded Directory Designation
--exclude-dir
allows you to specify directories to exclude from search.
$ grep -nr xxx . --exclude-dir={dist, .nuxt}
Search from command output results
Display lines containing the string nginx from the command output results.
$ ps -ef|grep "nginx"
Contains either of multiple keywords
( -e
)
Show lines from command output that contain the string nginx or mysql.
$ ps -ef|grep -E "nginx|mysql"
or
$ ps -ef|grep -e "nginx" -e "mysql"
Files that do not contain search keywords
( -L
)
The -L option is used to specify the condition of “not included”.
$ grep –Lr SearchKeyword Directory
In the above example, files that do not contain the search keyword can be extracted from files that exist under the corresponding directory.
Search by excluding specified keywords
( -v
)
You can specify excluded keywords with the -v option.
$ cat test.txt
1 aaaa
2 bbbb
3 aaaa
4 cccc
5 dddd
6 @aaaa
$
$ grep aaaa test.txt
1 aaaa
3 aaaa
6 @aaaa
$
$ grep aaaa test.txt | grep -v @aaaa
1 aaaa
3 aaaa
Display only file names
( -l
)
The -l option will return only filenames in the search results.
$ grep –l SearchKeyword Directory
Do not display file names
( -h
)
The -h option suppresses the display of file names in the search results.
$ grep –h SearchKeyword Directory
case-insensitive
( -i
)
Search for and display lines containing the string http in the file test.txt in a case-insensitive manner.
$ grep -i "http" test.txt
Also displays previous and next lines
( -B
-A
)
Display the lines in the file test.txt that contain the string 44 and the lines before and after it.
$ cat test.txt
1111
2222
3333
4444
5555
6666
7777
$ grep -n -B 2 -A 1 "44" test.txt
2-2222
3-3333
4:4444
5-5555
Search by fixed string
Since the search is performed with regular expressions, all lines will be hit as follows.
$ cat test.txt
3333
44.*44
5555
$ grep ".*" test.txt
3333
44.*44
5555
You can escape it, but it will be difficult to see.
$ grep "\.\*" test.txt
44.*44
The -F option allows you to search using fixed characters.
$ grep -F ".*" test.txt
44.*44
Application with xargs
Pass a list of all files under the current directory that contain the string aaa to sed for replacement
$ grep -rl "aaa" . | xargs sed -i -e 's/aaa/bbb/g'
Highlight and display
$ grep --color=always "aaa" test.txt