Author’s brief introduction
Mr Lemon, senior operations engineer (self-described), SRE specialist (target), dreams of buying a Porsche by the age of 35. Like to study the bottom technology, think the bottom foundation is king. All new technologies are inseparable from the operating system (CPU, memory, disk), network, etc. Adhere to the input and output, record their own learning bit by bit, in the ordinary insist on moving forward, one day will meet a different yourself. Public account: Yunwewang (ID: Leeeee_Li).
One, foreword
I usually like to take notes, work for four or five years, notes on thousands of. Recently left the job, coupled with the bottleneck of the workplace, prepare to review and sort out the relevant notes, comb the knowledge point, there may be a series of blog output, from the basic to the advanced.
What is Grep
Global Regular Expression Print Global Regular Expression
Grep is a Linux/Unix command line tool that searches for strings in a specified file. Text search patterns are called regular expressions. When it finds a match, it prints the line with the result. The grep command is handy when searching for large log files.
Grammar:
grep [options] pattern [files]
Copy the code
3. Common parameters
-c: displays the number of matched rows
-h: prints the matching line but does not display the file name
-i: case insensitive
-l: Only the file names containing matching characters are displayed when multiple files are queried
-n: Displays the matching line and line number
-v: displays the unmatched lines
-w: matches the entire word, not the entire string
-b: displays the matched position
-o: displays only matched strings
-s: No error information is displayed
-e exp: Matches multiple values
-e: uses regular matching
Four, the instance,
1: indicates the color of the matching item
echo"This is a word \ n heihei" | grep word -- color = autoCopy the code
2: fetch the /etc/passwd file containing root
grep root /etc/passwd or cat /etc/passwd |grep root
Copy the code
3: run the /etc/passwd command to retrieve the file that does not contain root
grep -v root /etc/passwd or cat /etc/passwd |grep -v root
Copy the code
4: Find the line containing ETH in the demSG output and output the line number in color
dmesg | grep eth -n --color=auto
Copy the code
5: in the demSG output, locate the eth row, and locate the first two and last three lines containing ETH
dmesg |grep eth -n -A3 -B2 --color=auto
Copy the code
five,Grep Indicates the exit status
echo $?
# 0: indicates success
# 1: no match is found
# 2: indicates that the file in the parameter does not exist
Copy the code
Vi.Application scenarios
1: matches the context of the row
ifconfig | grep -C 3 "Link encap"
Copy the code
-A n After n line, A memory is (After)
-b n n lines, B memory is (Before)
-c n First n lines, last n lines, C memory is (Center)
2: Uses -c to collect statistics
grep -c "baidu.com" file.txt
Copy the code
3: Searches for the directory based on the file content
grep "declare" # search in current directory
grep -r "declare" Search in current directory and subdirectory
grep -r -l "declare" The file name is not specified
grep -R --exclude-dir="filename" baidu.com A directory does not match
Copy the code
Filter out comment lines
grep ^[^#] filename
Copy the code
Filter out comment lines with Spaces
grep "^\s*[^# \t].*$" filename
Copy the code
Find the string f containing the string file1 in file2
File1: DDF DDC file2: shinjldin adcddf DDF grep -f file1 file2# Results obtained:
#adcddf
#ddf
Copy the code
7, ps aux | grep init and ps aux | grep [I] the difference between a nit
Ps aux | grep init:
Ps aux | grep [I] nit:
The first init she’s going to put grep init in the process, so it matches init
The second [I]nit will grep [I]nit into the process, but [I]nit can only match init and nit, so there is only one
echo grep init |grep init
echo grep init |grep [i]nit
echo grep [i]nit |grep init
echo grep [i]nit |grep [i]nit
Copy the code
The difference between grep -e and grep
1. Method of use
Grep -e "^[a-za-z0-9]" filename grep"^[a-zA-Z0-9]" filename # if you do not add E, you will get a matching error
Copy the code
2, the instance,
Both of the following ways do the same thing, using -e and not using -e
cat a.txt |grep -oE Id = '[0-9] {9}'
cat a.txt |grep -o Id = '[0-9] \ {9 \}'
Copy the code