search
Homephp教程PHP开发linux pipeline command grep

linux pipeline command grep

Dec 13, 2016 pm 02:26 PM
grep

Function description: Find strings that meet the conditions in the file.

Syntax: grep [-abcEFGhHilLnqrsvVwxy][-A][-B][-C][-d][- e

Supplementary Note: The grep command is used to find files containing the specified template style. file, if the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name given is "-", the grep command will read data from the standard input device.

Parameters:
-a or --text Don’t ignore binary data.
-A or --after-context= In addition to displaying the column that matches the template style, and display the content after that column.
-b or --byte-offset Before displaying the column that matches the template style, mark the bit number of the first character of the column.
-B or --before-context= In addition to displaying the column that matches the template style, it also displays the content before that column.
-c or --count Count the number of columns that match the template style.
-C or --context= or - In addition to displaying the column that conforms to the template style, it also displays the content before and after that column.
-d or --directories= This parameter must be used when specifying a directory rather than a file to be searched, otherwise the grep command will report information and stop the action.
-e

Usage of linux grep command
Use grep command to search text files from www.linuxso.com

If you want to find a string in several text files, you can use the ‘grep’ command. ‘grep’ searches text for a specified string.
Suppose you are searching for a file with the string 'magic' in the '/usr/src/linux/Documentation' directory:
$ grep magic /usr/src/linux/Documentation/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?

The file ‘sysrp.txt’ contains this string and discusses the function of SysRQ.
By default, ‘grep’ only searches the current directory. If there are many subdirectories under this directory, 'grep' will list it like this:
grep: sound: Is a directory
This may make the output of 'grep' difficult to read. There are two solutions here:
Explicitly ask to search the subdirectory: grep -r
or ignore the subdirectory: grep -d skip
Of course, if a lot of output is expected, you can pipe it to 'less' Read
$ grep magic /usr/src/linux/Documentation/* | less
This way, you can read more conveniently.
One thing to note is that you must provide a file filtering method (use * to search all files). If you forget, 'grep' will wait until the program is interrupted. If you encounter this, press and try again.
Here are some interesting command line parameters:
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, not Part of the string (such as matching 'magic', not 'magical'),
grep -C number pattern files: matching context displays [number] lines respectively,
grep pattern1 | pattern2 files: displays lines matching pattern1 or pattern2 ,
grep pattern1 files | grep pattern2: Display lines that match both pattern1 and pattern2.
There are also some special symbols for searching:
mark the beginning and end of words respectively.
For example:
grep man * will match 'Batman', 'manic', 'man', etc.,
grep 'grep '' only matches 'man', not other strings such as 'Batman' or 'manic'.
'^': means that the matched string is at the beginning of the line,
'$': means that the matched string is at the end of the line,
If you are not used to command line parameters, you can try 'grep' in the graphical interface, such as reXgrep. This software provides syntax such as AND, OR, NOT, and beautiful buttons :-). If you just need clearer output, try fungrep .

.grep Search string
Command format:
grep string filename
There are many ways to find strings. For example, I want to find all lines starting with M. At this time, the concept of pattern must be introduced. Here are some simple ones □Example, and explanation:
^M Lines starting with M, ^ means the beginning
M$ Lines ending with M, $ means the end
^[0-9] Lines starting with a number, [] Can enumerate letters
^[124ab] Lines starting with 1, 2, 4, a, or b
^b.503 Period represents any letter
* Asterisk represents more than 0 letters (can be none)
+ Plus sign Represents more than 1 letter
. Slashes can remove special meanings
cat passwd | grep ^b Lists the list of university account applicants
cat passwd | grep ^s Lists the list of exchange students who have applied for accounts
cat passwd | grep '^b.503' Lists all grades in the Department of Electrical Engineering...
grep '^.' myfile.txt Lists all lines starting with a period

~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1. Introduction to grep

grep (global search regular expression(RE ) and print out the line, comprehensive search for regular expressions and print out the line) is a powerful text search tool that can use regular expressions to search for text and print out matching lines. The Unix grep family includes grep, egrep, and fgrep. The egrep and fgrep commands are only slightly different from grep. egrep is an extension of grep, supporting more re metacharacters. fgrep is fixed grep or fast grep, which treats all letters as words. That is to say, the metacharacters in regular expressions represent their own literal meanings. , no longer special. Linux uses the GNU version of grep. It is more powerful and can use the functions of egrep and fgrep through the -G, -E, and -F command line options.

The way grep works is that it searches one or more files for a string template. If the template contains spaces, it must be quoted, and all strings following the template are treated as file names. The search results are sent to the screen without affecting the original file content.

grep can be used in shell scripts because grep indicates the status of the search by returning a status value, which is 0 if the template search is successful, 1 if the search is unsuccessful, and 2 if the file being searched does not exist. We can use these return values ​​to perform some automated text processing work.


2. grep regular expression metacharacter set (basic set)

^

anchor the beginning of the line. For example: '^grep' matches all lines starting with grep.

$

End of anchor line For example: 'grep$' matches all lines ending with grep.

Match a non-newline character. For example: 'gr.p' matches gr followed by any character, then p.

*

Match zero or more previous characters. For example: '*grep' matches all lines with one or more spaces followed by grep. .* used together represents any character.

[]

matches characters within a specified range, such as '[Gg]rep' matches Grep and grep.

[^]

matches a character that is not within the specified range, such as: '[^A-FH-Z]rep' matches a line starting with a letter that does not contain A-R and T-Z, followed by rep.

(..)

marks matching characters, such as '(love)', love is marked as 1.

Anchor the beginning of a word, like: '

>

Anchor the end of a word, like 'grep>' Matches lines that contain words ending with grep.

x{m}

Repeat the character x, m times, such as: '0{5}' matches lines containing 5 o's.

x{m,}

Repeat the character x, at least m times, such as: 'o{5,}' matches lines with at least 5 o's.

x{m,n}

Repeat the character x, at least m times and no more than n times. For example: 'o{5,10}' matches lines with 5--10 o's.

w

matches literal and numeric characters, that is, [A-Za-z0-9], for example: 'Gw*p' matches G followed by zero or more literal or numeric characters, then p. The inverted form of

W

w matches one or more non-word characters, such as period, period, etc.

b

Word lock character, such as: 'bgrepb' only matches grep.

3. Metacharacter extension set for egrep and grep -E

+

matches one or more previous characters. For example: '[a-z]+able', matches a string of one or more lowercase letters followed by able, such as loveable, enable, disable, etc.

?

Matches zero or more previous characters. For example: 'gr?p' matches lines with gr followed by one or no characters, then p.

a|b|c

matches a or b or c. For example: grep|sed matches grep or sed

()

grouping symbols, such as: love(able|rs)ov+ matches loveable or lovers, matches one or more ov.

x{m},x{m,},x{m,n}

functions the same as x{m},x{m,},x{m,n}

4. POSIX character class

for To maintain the same character encoding in different countries, POSIX (The Portable Operating System Interface) adds special character classes, such as [:alnum:] which is another way of writing A-Za-z0-9. They must be placed within [] signs to become regular expressions, such as [A- Za-z0-9] or [[:alnum:]]. Except for fgrep, grep under Linux supports POSIX character classes.

[:alnum:]

Alphanumeric characters

[:alpha:]

Literal characters

[:digit:]

Numeric characters

[:graph:]

Non-empty characters (non-space, Control characters)

[:lower:]

lowercase characters

[:cntrl:]

Control characters

[:print:]

Non-empty characters (including spaces)

[:punct:]

Punctuation

[:space:]

All whitespace characters (new lines, spaces, tabs)

[:upper:]

Uppercase characters

[:xdigit:]

Hex digits ( 0-9, a-f, A-F)

5. Grep command option

-?

Show matching lines above and below simultaneously? lines, such as: grep -2 pattern filename displays the upper and lower lines of the matching line at the same time.

-b, --byte-offset

Print the block number where the line is located before printing the matching line.

-c,--count

Only prints the number of matching lines and does not display the matching content.

-f File, --file=File

Extract template from file. An empty file contains 0 templates, so nothing matches.

-h, --no-filename

When searching for multiple files, do not show matching filename prefixes.

-i, --ignore-case

Ignore case differences.

-q, --quiet

Cancel the display and only return the exit status. 0 means a matching row was found.

-l, --files-with-matches

Print a list of files matching the template.

-L, --files-without-match

Print a list of files that do not match the template.

-n, --line-number

Print the line number in front of the matching line.

-s, --silent

Do not display error messages about non-existent or unreadable files.

-v, --revert-match

Reverse retrieval, only display unmatched lines.

-w, --word-regexp

If quoted by , search the expression as a word.

-V, --version

Display software version information.

6. Examples

To use grep well, you actually need to write regular expressions well, so we will not explain all the functions of grep with examples here. We will only list a few examples to explain how to write a regular expression.

$ ls -l | grep '^a'

Filter the output of ls -l through the pipeline and only display lines starting with a.

$ grep 'test' d*

Display all lines containing test in files starting with d.

$ grep 'test' aa bb cc

Displays the lines matching test in the aa, bb, cc files.

$ grep '[a-z]{5}' aa

Displays all lines containing strings each with at least 5 consecutive lowercase characters.

$ grep 'w(es)t.*1' aa

If west is matched, es is stored in the memory and marked as 1, and then searches for any number of characters (.*). These characters are followed by another es (1). If found, the line is displayed. If you use egrep or grep -E, you don't need to escape with the "" sign, just write it directly as 'w(es)t.*1'.


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools