Command line tool-grep

Source: Internet
Author: User

Grep Regular Expression metacharacters (basic set)

^ Start of the anchor row: '^ grep' matches all rows starting with grep.


$ The End Of The Anchor row is as follows: 'grep $ 'matches all rows ending with grep.


. Match a non-linefeed character such as: 'gr. P' match gr followed by any character, followed by P.


* Match zero or multiple previous characters, for example, '* grep'. Match All one or more spaces followed by the grep line. . * Represents any character.


[] Matches a character in a specified range, for example, '[Gg] rep' matches grep and grep.


[^] Match a character that is not within the specified range, for example, '[^ A-FH-Z] rep' match a line that does not start with a letter that does not contain the A-F and H-Z, followed by Rep.


\ (.. \) Indicates matching characters, such as '\ (love \)', and love is marked as 1.


\ <Specifies the start of a word, for example, '\ <grep' matches a row that contains a word starting with grep.


\> Anchor specifies the end of a word. For example, 'grep \> 'matches the row containing the word ending with grep.


X \ {M \} repeats the characters X and M consecutively. For example, 'O \ {5 \} 'matches the rows that contain 5 consecutive O Records.


X \ {M, \} repeats the character x continuously for at least m times, for example, 'O \ {5, \} 'matches rows with at least 5 o consecutively.


X \ {M, N \} repeats the character x consecutively, at least m times, no more than N times, for example, 'O \ {5, 10 \} 'matches rows of 5-10 consecutive O.


\ W matches a character with a number, that is, a [A-Za-z0-9], for example, 'g \ W * P' matches a character with g followed by zero or more characters or numbers, then p.


The inverse form of \ W. It matches a non-word character, such as a period ending. \ W * can match multiple.


The \ B word lock. For example, '\ bgrep \ B' only matches grep, that is, it can only be a grep word, with spaces on both sides.


Note: What are the metacharacters in the basic Regular Expression of grep ?, +, {, |, (,) Lose their special meanings and become common characters. To use special meanings, you can add the '\' prefix, or '-e' option (extended regular expression)


Grep "word1 \ | word2" test.txt

Grep-e "word1 | word2" test.txt

The preceding two commands are equivalent.


Common instances

1. Case Insensitive during search

grep -i "string" FILE


2. display the content of rows before and after matching rows


2.1. Show the N rows after matching rows

grep -A <N> "string" FILE


2.2. Show n rows before matching rows

grep -B <N> "string" FILE


2.3. Show n rows before and after matching rows

grep -C <N> "string" FILE


3. Match the entire word rather than the substring

grep -w "word" FILE


4. Use 'grep _ options' to highlight the matched string

export GREP_OPTIONS=‘--color=auto‘ GREP_COLOR=‘100;8‘


5. Reverse match

grep -v "string" FILE


6. Display rows that do not contain all specified strings

grep -v -e "pattren1" -e "pattren2" FILE


7. count the number of matched rows

grep -c "string" FILE


8. Display files containing matching items

grep -l "string" FILES


9. display only the rows that match the item, not the matched item.

grep -o "string" FILE


10. The row number is displayed in the output.

grep -n "string" FILE


11. Select the row containing six, seven, or eight.

grep -E "six|seven|eight" test.txt


This article is from the "two bear blog" blog, please be sure to keep this source http://t19880825.blog.51cto.com/9445110/1570209

Command line tool-grep

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.