-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grep-command
73 lines (38 loc) · 1.78 KB
/
Grep-command
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Grep is an acronym that stands for Global Regular Expression Print.
Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file/files.
The text search pattern is called a regular expression. When it finds a match, it prints the line with the result. The grep command is handy when searching through large log files.
NOTE : It is case-sensitive by default
Syntax
grep [pattern] [file]
Example 1 : Search a string in a file, error.log
grep ERROR error.log
Example 2 : Search a complete word
grep -w ERROR error.log
Example 3 : It is case-sensitive by default, to search a string as case-insenstive
grep -i error error.log
Example 4 : Search a string in complete folder
grep ERROR path-to-folder/*
Example 5 : Search a string in all txt files
grep ERROR path-to-folder/*.txy
Example 6 : Search a string in all files starting with specific characters
grep ERROR path-to-folder/2020-10-10-*.logs
Example 7 : Limit the output with max-count
grep -m10 ERROR path-to-folder/*
Example 8 : Sometimes you need to look some lines before your search
grep -B 10 ERROR path-to-folder/*
result will include 10 lines previous to search
Example 9 : Sometimes you need to look some lines after your search
grep -A 10 ERROR path-to-folder/*
result will include 10 lines lines to search
Example 10 : Nested grep
grep ERROR path-to-folder/* | grep TimeOut
Example 11 : Include subdirectories
grep -r ERROR path-to-folder/*
Example 12 : Inverse grep, you can search what do not match with search
grep -v ERROR path-to-folder/*
Example 13 : Search a complete line
grep -x "Exception at line 100" path-to-folder/*
Example 14 : List the names of matching files
grep -l ERROR path-to-folder/*
Example 15 : Count the number of matches
grep -c ERROR path-to-folder/*