Linux Shell Basics (four) regular expressions and grep commands (in edit)

Source: Internet
Author: User
Tags script php

First, regular expression
Before learning the Find command, we have been exposed to some simple regular, so we can now learn some complex regularization. First of all, we still have to review what is regular expression, see this tutorial first, must see a few times, at least know the regular is about what looks like. (reprinted to Https://deerchao.net/tutorials/regex/regex.htm) a regular string of strings that describe a particular content, which is suitable for handling strings, and is less suitable for handling mathematical logic. Mastering the regular is a great benefit for writing shell scripts, and most of the shell scripts involve regular expressions. Regular use is also required in various programming tools.

Use grep sed awk to process strings
Can you write a shell script php?
There is also a regular in the Ngix configuration file
Regular core symbols
。 An arbitrary character

    • A
      ? 0 or 1 x
    • One or more

Second, grep command
The grep command is used to filter the characters in the file, (Global search Regular expression (RE) and print out of the line, full search of regular expressions and print out the lines) is a powerful text search tool, It can use regular expressions to search for text and print matching lines. The system itself through the/ETC/PROFILE.D directory under the colorgrep.sh file alias ==grep--color, can easily display the found row.

Usage: grep [options] ... PATTERN [FILE] ...
Grep-cniv ' keyword (regular) ' File path
Options
-I is case-insensitive, and grep does not differentiate keyword case by default
-C line number
-N Displays the line number of the keyword
-V Inverse (more used)
-R iterates through all levels of subdirectories
-A followed by a number, filtering out rows that meet the requirements and the following n rows
-B followed by numbers, filtering rows that meet the requirements and n rows above
-C followed by numbers, filtering rows that meet the requirements, and n rows above and below

Grep/egrep Example
Grep-n ' Root '/etc/passwd
Grep-nv

Extended regular GREP-E ==sed-r ==awk

Practice and Master Regular expressions
First do a preparation, copy the/etc/passwd document to the other path, such as/TMP to do the experiment

Example

1. Filter out the line where the root of the keyword is located and display the line number.

Grep-n ' Root '/etc/passwd

You can see that there are color identification keywords.

2. Count the number of occurrences of a keyword

Grep-c ' Nologin ' passwd

The following 15 means that the Nologin keyword appears 15 times.

3. Search for a keyword to appear in the line, and then filter out the line that does not appear the key word

Grep-vn ' Nologin ' passwd

4, search the entire folder, in the file content root. (Note the difference between what is found with the Find command)

Grep-r ' root '/etc

Purple is the path of the file, search out a lot of things, grep, etc under the root of all the files contained in the same line and the file information is listed.

Linux Shell Basics (four) regular expressions and grep commands (in edit)

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.