Home > php教程 > PHP开发 > body text

Grep usage summary

高洛峰
Release: 2016-12-13 14:36:37
Original
1267 people have browsed it

Use grep to search file contents - fast and convenient

1. Function
The grep command can specify a file to search for specific content and output the lines containing these contents to the standard output. The full name of grep is Global Regular Expression Print, which means the global regular expression version. Its usage permissions are for all users.
2. Format
grep [options]

Basic format
grep pattern [file...]
(1)grep search string [filename]
(2)grep regular expression [filename]
Search all in the file Where pattern appears, pattern can be either the string to be searched or a regular expression.
Note: It is best to use double quotes when entering the string to be searched/and when using regular expressions for pattern matching, Note the use of single quotes
3. Main parameters
[options] Main parameters:
-c: Only output the count of matching lines.
-I: Case-insensitive (only applicable to single characters).
-h: Do not display file names when querying multiple files.
-l: When querying multiple files, only the file names containing matching characters will be output.
-n: Display matching lines and line numbers.
-s: Do not display error messages that do not exist or have no matching text.
-v: Display all lines that do not contain matching text.

Note: n will be invalid when there is c
Main parameters of pattern regular expression: Application of regular expression (Note: It is best to enclose the regular expression in single quotes)
: The principle of ignoring special characters in regular expressions There is meaning.
^: Matches the starting line of the regular expression.
$: Matches the end line of the regular expression.
<: Start from the line matching the regular expression.
>: Go to the end of the line matching the regular expression.
[ ]: A single character, such as [A], that is, A meets the requirements.
[-]: Range, such as [A-Z], that is, A, B, C to Z all meet the requirements.
. : All single characters.
*: There are characters, and the length can be 0.


There are some interesting command line parameters below:

grep -i pattern files: Search case-insensitively. The default is case-sensitive,

grep -l pattern files: only match file names are listed,

grep -L pattern files: list unmatched file names,

grep -w pattern files: only match whole words , rather than part of the string (such as matching 'magic', not 'magical'),

grep -C number pattern files: matching contexts display [number] lines respectively,

grep pattern1 | pattern2 files: display matching Lines of pattern1 or pattern2,

grep pattern1 files | grep pattern2: Display lines that match both pattern1 and pattern2.

grep -n pattern files will display line number information

grep -c pattern files will find the total number of lines


Regular expressions are a very important concept in Linux/Unix systems. A regular expression (also called "regex" or "regexp") is a pattern that can describe a type of string. If a string can be described by a regular expression, we say that the character matches the regular expression. This is similar to how users in DOS can use the wildcard "*" to represent any character. On Linux systems, regular expressions are often used to find patterns in text, as well as perform "search-replace" operations and other functions on text.



1. grep search string [filename]

Create a file named grep.txt with the following text:

I like golf.

Golf is played on grass.

I created gilf.

1. Search all instances of the string golf in the grep.txt file and output the lines containing the string

grep golf grep.txt

I like golf.

grep -n "golf" grep.txt

1:I like golf.

2. grep regular expression [filename]

grep -n '[gG]olf' grep.txt

1:I like golf.

2:Golf is played on grass.



Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template