grep [OPTIONS] PATTERN [FILE...]  grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]Copy the code

function

Grep is a powerful text search tool that uses regular expressions to search for text (in a given file) and print out matching lines.

For example,

Show test filetestFileThe content of the

$cat testFile
Copy the code

After the input, the output is as follows:

hello, this is a file
this file used for test the
command of grep.
do you know?
yes yeeeeeees i knooooooww!
the west is
what is ves?
this is a world
which is tes good.
west tes
westeses
westeseses
west tesves
Copy the code

This is the content of the file we are testing.

Looks for strings in all files in the current directory and subdirectoriestest

$grep -r 'test' *
Copy the code

After the input, the output is as follows:

good/test2:this is a test for the good/test2:test the string. test2:grep test for test2:used to test the test3:grep test  for test3:used to test the testFile:this file used for test theCopy the code

This is probably the most commonly used command. The -r option means recursive search for subdirectories.

To find thetestAnd color the matching string in the result

$grep --color 'test' *
Copy the code

This colors all matching test strings in the output.

All files in the current directory.docFind string in filesort

$grep "sort" *.doc
Copy the code

Displays the number of rows that match

$grep -c "we" testFile
Copy the code

After the input, the output is as follows:

5
Copy the code

This displays the number of rows that match we.

According to containweCharacter line and print the line number

$grep -n "we" testFile
Copy the code

After the input, the output is as follows:

6:the west is
10:west tes
11:westeses
12:westeseses
13:west tesves
Copy the code

This prints the matching line number to the far left of the output.

Display mismatchweThe rows of

$grep -v "we" testFile
Copy the code

After the input, the output is as follows:

hello, this is a file
this file used for test the
command of grep.
do you know?
yes yeeeeeees i knooooooww!
what is ves?
this is a world
which is tes good.
Copy the code

Here the -v option displays rows that do not match we.

Case matching is ignored

$ls |grep -i 'file'
Copy the code

After the input, the output is as follows:

testFile
Copy the code

In this way, the -i option is added to make file matching case insensitive.

Range matching

$grep 'h[ae]' testFile
Copy the code

After the input, the output is as follows:

hello, this is a file
this file used for test the
the west is
what is ves?
Copy the code

The match here is either ha or he, and if you use [a,e] that would include a comma.

Show only includetestMatching files

$grep -l 'test' *
Copy the code

After the input, the output is as follows:

test2
test3
testFile
Copy the code

Here, the -l option lists only files with matching strings.

According tols -lOutput content todThe beginning of the line

$ls -l | grep '^d'
Copy the code

or

$ls -l |grep ^d
Copy the code

After the input, the output is as follows:

drwxr-xr-x 2 vaqeteart member 4096 Jul 12 18:44 good
Copy the code

In this case, good is a directory.

According tols -lIn the output, nodThe beginning of the line

$ls -l |grep '^[^d]'
Copy the code

After the input, the output is as follows:

total 16
-rw-r--r-- 1 vaqeteart member    7 Jul 12 18:42 2
-rw-r--r-- 1 vaqeteart member  114 Jul 12 18:42 test2
-rw-r--r-- 1 vaqeteart member  212 Jul 12 19:01 testFile
Copy the code

Here, we use [^] to specify characters that are not included.

Display alltContains in the file at the beginningtestThe rows of

$grep 'test' t*
Copy the code

After the input, the output is as follows:

test2:grep test for
test2:used to test the
testFile:this file used for test the
Copy the code

Here, to the left of the colon is the file name of the corresponding line.

Displayed in thetestFiletest2In-file matchtestThe rows of

$grep 'test' testFile test2
Copy the code

or

$grep test testFile test2
Copy the code

After the input, the output is as follows:

testFile:this file used for test the
test2:grep test for
test2:used to test the
Copy the code

For the insurance period, it is best to add “” after the search.

According totestFileA line in a string with at least five consecutive lowercase characters

$grep '[a-z]\{5\}' testFile
Copy the code

After the input, the output is as follows:

hello, this is a file
command of grep.
yes yeeeeeees i knooooooww!
this is a world
which is tes good.
westeses
westeseses
west tesves
Copy the code

According totestFileContains a string that begins with five consecutive lowercase characters

$grep '^[a-z]\{5,\}' testFile
Copy the code

After the input, the output is as follows:

hello, this is a file
command of grep.
which is tes good.
westeses
westeseses
Copy the code

According totestFileContained inthLine for the beginning word

$grep '\<th'
Copy the code

After the input, the output is as follows:

hello, this is a file
this file used for test the
the west is
this is a world
Copy the code

Here, words that start with th are: this and the.

According totestFileContained instThe line at the end of the word

$grep 'st\>' testFile
Copy the code

After the input, the output is as follows:

this file used for test the
the west is
west tes
west tesves
Copy the code

Here, the words ending in st are test and west.

Searches using tags

$ grep 'w\(es\)t t\1v\1' testFile
Copy the code

After the input, the output is as follows:

west tesves
Copy the code

Here, according to the regular expression \(\ parentheses, es matches will be marked as 1, so that we can use \1 instead of es later. This is equivalent to searching: grep ‘west tesves’ testFile. If you use egrep or grep -e, instead of escaping \, you can use the 1 label directly.

describe

Grep is a powerful text search tool that uses regular expressions to search 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 and supports more re metacharacters. Fgrep is fixed grep or fast grep. These metacharacters regard all letters as words, that is, metacharacters in regular expressions are not special anymore.

Linux uses the GNU version of grep. It is more powerful, and you can use the egrep and fgrep functions with the -g, -e, and -f command-line options.

Grep works like this: it searches for string templates in one or more files. If the template includes Spaces, it must be referenced, and all strings after the template are treated as filenames. 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, 0 if the template search is successful, 1 if the search is unsuccessful, and 2 if the file does not exist. We can use these return values to do some automated text processing.

Some options for grep:

  • -?:Display both above and below the matching line? Line, such as:grep -2 pattern filenameDisplay 2 rows above and below the matching row.
  • -b, –byte-offset: prints the block number of the matching line.
  • -c,–count: only the number of matched lines is displayed.
  • -f File, — File =File: Extracts a template from a File. The empty file contains zero templates, so nothing matches.
  • -h, –no-filename: no matching filename prefix is displayed when multiple files are searched.
  • -i, –ignore-case: ignores case differences.
  • -q, –quiet: Cancels the display and returns only the exit state. 0 indicates that a matching row was found.
  • -l, –files-with-matches: Prints the list of files matching the template.
  • -l, –files-without-match: prints the list of files that do not match the template.
  • -n, –line-number: prints the line number before the matched line.
  • -s, –silent: Does not display error information about the file that does not exist or cannot be read.
  • -v, –revert-match: reverse lookup, showing only lines that do not match.
  • -w, –word-regexp: Searches the expression as a word if it is referenced by \< and \>.
  • -v, –version: displays the software version.

other

Used forgrepMeta-character set for regular expressions (base set)

  • ^: Start of anchor line e.g. ‘^grep’ matches all lines starting with grep.
  • $: end of anchored line e.g. ‘grep$’ matches all lines ending with grep.
  • .: Matches a non-newline character such as ‘gr.p’ Matches GR followed by any character, then p.
  • *: matches zero or more previous characters. For example, ‘a*’ matches one or more A characters, and ‘*grep’ matches one or more Spaces followed by grep.
  • *: indicates any character.
  • []: matches a character in a specified range, for example, ‘[Gg]rep’ matches Grep and Grep.
  • [^]: matches A character that is not in the specified range, such as ‘[^ a-fh-z]rep’ matches the beginning of A letter that is not in the a-r and H-z range, following the line of rep.
  • \ [.. \): marks a match character, such as ‘(love)’. Love is marked as 1, so that if the same expression references love again, only \1 is used.
  • \<: Anchor the beginning of a word, e.g. ‘\>’
  • \ >:: Anchors the end of a word, such as ‘grep\>’ to match a line containing a word ending with grep.
  • X \{m\}: repeat the character x, m times, such as ‘o\{5\}’ to match a line containing five O’s.
  • X \{m,\}: Repeats the character x at least m times, such as ‘o\{5,\}’ to match lines with at least five O’s.
  • X \{m,n\}: repeat the character x at least m times, but not more than n times, e.g. ‘o\{5,10\}’ matches 5-10 O lines.
  • \w:: matches literal and numeric characters, that is, [A-zA-z0-9], such as ‘G\w*p’ matches G followed by zero or more literal or numeric characters, followed by p.
  • \W: the inverted form of \W matches one or more non-word characters, such as periods, etc.
  • \b: Word lock, such as ‘\bgrepb\’ matches only grep.

Used foregrepgrep -EMetacharacter extension set of

  • +: matches one or more previous characters. For example, ‘[a-z]+able’, which matches one or more lowercase letters followed by able, such as loveable,enable,disable, etc.
  • ? : Matches zero or one previous character. Such as: ‘gr? P ‘matches g followed by one or no R character, followed by a line of P.
  • A | b | c: match a or b or c. Such as: grep | sed to match grep and sed
  • () : grouping symbols, such as: love (able) | rs ov + matching loveable or lovers, to match one or more of the ov.
  • X {m},x{m,},x{m,n}: same function as x\{m\},x\{m,\},x\{m,n}

POSIX character classes

POSIX(The Portable Operating System Interface) adds special character classes to maintain one-to-one in character encodings in different countries, such as “[:alnum:]”, which is another way of writing a-za-z0-9. They need to be placed inside [] to become regular expressions, such as [A-za-z0-9] or [[:alnum:]]. In Linux, grep except fgrep supports POSIX character classes.

  • [:alnum:]: alphanumeric character
  • [:alpha:]: text character
  • [:digit:]: digit character
  • [:graph:]: non-null characters (non-spaces, control characters)
  • [:lower:]: indicates lowercase characters
  • [: CNTRL :]: indicates the control character
  • [:print:]: non-null characters (including Spaces)
  • [:punct:]: punctuation
  • [:space:]: all whitespace characters (newlines, Spaces, tabs)
  • [:upper:]: indicates uppercase characters
  • [:xdigit:]: hexadecimal digit (0-9, A-F, A-F)