The grep command usage for Shell Scripting Learning notes

Source: Internet
Author: User
Tags posix egrep



grep Basic Usage

grep [Options] [mode] [file:]


Options

-C outputs only the number of matching rows

-I ignore case when searching

-H does not display file names when querying multiple files

-L searches only for matching file names, not specific matching rows

-n Lists all matching rows and displays line numbers

-S does not display error messages that do not exist or have no matching text

-V Displays all lines that do not contain matching text

-W Match Whole word

-X matches an entire row

-R recursive search that not only belongs to the current working directory, but also searches subdirectories

-Q Disables the storage of any results to postpone status indicating whether the owning is successful

-B Prints the offset of the header that matches the leading file, in bytes

-O with the-B option to print the offset of the matched word from the head of the file, in bytes

-E supports extended regular expressions

-F does not support regular expressions, the literal meaning of the installation string is matched

Mode

String

Variable

Regular expressions



1.grep search for files or characters with spaces that need to be enclosed in quotation marks

2.grep You can use wildcards when searching multiple files




grep combined with regular expressions


1. Match the beginning of the line

Example:

grep ^a file finds lines beginning with a in text


Example: grep-c ^$ file Find the number of blank lines in text


Grep-c ^[^$] The number of non-blank lines in the file lookup text


2. Setting case

grep-n [Cc] file case-insensitive lookup of rows with C

Grep-n-I. C file


3. Match repeating characters

grep ^/..../file matches a total of six characters with or ending with any four characters in the middle


grep ^-*b See the beginning of a line with 0 "-" or any "-" matches with B end


4. Escape character

Example 1: Escaping "."

grep www\.example\.com search for rows containing www.example.com

Example 2: Escape "-"

grep ' \-{5\} file


5.POSIX characters

[: Upper:] denotes uppercase [A-z]

[: lower:] = lowercase letter [A-z]

[:d igit:] means number [0-9]

[: Alnum:] denotes uppercase and lowercase letters and numbers [0-9A-Z-A-Z]

[: Space:] Indicates a space or TAB key

[: Alpha:] denotes uppercase and lowercase letters [a-z-a-z]

[: Cntrl:] represents the CTRL key

[: Graph:] or [:p rint:] denotes ASCII 33-126 characters

[: xdigit:] = 16 binary number [0-9a-f-a-f]


Example:

grep ^[[:upper:]] file finds lines beginning with uppercase letters in a line character

POSIX characters need to be enclosed in [].

6. Exact match

grep "\<the\> matches the line containing the word

Must have a quoted number


7. Or character (extended regular expression character)

Grep-e "Ou|se" matches rows with OUs or SE


8.egrep and Fgrep

Egrep "Ou|se" file

Egrep "^-+b" file match begins with a "-" or "-" and a line containing B is followed


Fgrep CA file only matches characters


[0-9]*. [A-za-z] [A-z-a-z]


*.doc *.txt *.ppt *.docx *.pptx


Grep-n-I chapter *.doc.

Grep-n [CC][HH][AA][PP][TT][EE][RR] *.doc ...



Grep-r-C ^$ *

Grep-c ^$ *


GREP-E: $| [^[:alpha]]


This article is from the "Endmoon" blog, make sure to keep this source http://endmoon.blog.51cto.com/10533729/1682382

The grep command usage for Shell Scripting Learning notes

Related Article

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.