Home  >  Article  >  php教程  >  Introduction to grep command under CentOS

Introduction to grep command under CentOS

高洛峰
高洛峰Original
2016-12-13 15:38:281410browse

Introduction to grep command

grep is a command line tool originally used in Unix operating systems. Given a file list or standard input, grep will search for text that matches one or more regular expressions and output only matching (or unmatched) lines or text.

grep can query matching lines in the file based on the provided matching pattern list. After a matching line is found, the line content will be output to standard output (default). If other parameters are used, output in other formats can be generated

grep is used Matches text, which has no limit on the length of the input line except by available memory, and can match any character within the line.

Usage

grep -[acinv] 'Search content string' filename

Introduction to grep command under CentOS

Actual usage example

1. Search the log to see how many 503 errors there are

grep -c '503' /var/log/httpd/error_log-20141116

2. Search for lines containing the word error , and output the line number

grep -n 'error' /var/log/httpd/error_log-20141116

3. Search for lines without the word error, and output the line number

grep -nv 'error' /var/log/httpd/error_log-20141116

4. Search to find how many installed software versions there are

#centos下查看安装的Python版本
rpm -qa | grep -i python
#Ubuntu下查看安装的Python版本
sudo dpkg -l | grep -i python

5. Filter the comment symbol #

#匹配 # 符号的行,但是输出的是 # 符号以外的行
grep -v '#' /etc/httpd/conf/httpd.conf

of the configuration file 6. Query each network card and IP address

ifconfig | grep -n inet

7. Ignore case search (-i)

grep -i "ErroR" log.txt

8. Search in all subdirectories (-r)

 grep -r "exception" log.txt
9、精准全匹配搜索(-w)
grep -w "boo" /path/to/file

10. Accurate whole-word matching search for two different Word

grep -w 'word1|word2' /path/to/file

11. Count the number of occurrences of a string (-c)

grep -c 'word' /path/to/file
#-n的话, 会在结果中,列出匹配字符串的序列号,并且会列出内容
grep -n 'word' /path/to/file

12. List only file names (-l)

grep -l 'main' *.pls

13. Highlight search results (–color)

grep --color apache /etc/passwd

grep regular expressions Shiyuan character set arrangement

Introduction to grep command under CentOS


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn