Introduction to common methods of GREP commands under Linux

Source: Internet
Author: User

1. Search for the specified string from a single file:

$ grep "literal-string" filename

The command outputs the contents of the string in the file, and if the string has no spaces, it can be without double quotes. FileName can be multiple files, with each file separated by a space.

The plus -i parameter can ignore the case. Add -u parameter to search for a word instead of searching for a string with that word

2. Display multiple lines near the matching line:
    • -a displays n rows after matching rows

$ grep-a N "string" filename

    • -B Displays the n rows before matching rows

$ grep-b N "string" filename

    • -c display n rows before and after matching rows

$ grep-c N "string" filename

3. Recursive Search: -r

$ GREP-R "This" *

Search for all files with "This" in the current directory and sub-directories.

4, do not match the search: -v

$ grep-v "Go" demo_text

Displays rows that do not contain the search string "go".

5. Number of rows matched by statistics: -c

$ grep-c "Go" filename

The number of rows in the statistics file that contain a "go" string.

6. Only file names with strings are displayed: -l

$ grep-l "this" filename

Displays the file name of the file with the "this" string.

7. Display line number when output:

Grep-n "this" filename

Displays the line number of the line that contains the "this" string in the file.

Introduction to common methods of GREP commands under Linux

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.