grep command uses the Xiang solution

Source: Internet
Author: User

grep is a powerful text search tool under Linux

grep: (Global search Regular expression (RE) and print out of the line

Principle: According to the user-specified text pattern, the text is searched line by line, displaying the rows that are matched by the pattern.

grep can use regular expressions for pattern matching

Format:grep [options] ' PATTERN ' file,...

Note: The use of single quotation marks is recommended for characters that do not have special meanings such as variables, or double quotes ""

Common options:
- v: Reverse, showing the line that cannot be matched to by the pattern;
- o: Displays only the strings that are matched by the pattern, not the entire row;
-I: Character case insensitive, ignore-case
- E: extended Regular expression support
-A #: Displays matching lines to the next # line that matches to
- b #: Displays matching lines to the top # lines that are attached to the display
- C #: Show matching lines, append display to the top and bottom # lines

--color=auto : Make matching text appear in color

Example:

1. Display the lines in the/proc/meminfo file that begin with the case s;

# grep-i ' ^s '/proc/meminfo #-I means case insensitive # grep ' ^[ss] '/proc/meminfo # [] brackets denote one of the character sets

2, remove the default shell for non-bash users;

# grep ' bash$ '/etc/passwd # Linux Chinese this defaults to $ as Terminator

3. Remove the user whose default shell is bash and its ID number is the largest;

# grep ' bash$ '/etc/passwd | Sort-n-T:-k3 | Tail-1 | Cut-d:-f1

4, display/etc/rc.d/rc.sysinit file, start with #, followed by at least one white space character, and then have at least one non-whitespace character line;

# grep ' ^#[[:space:]]\{1,\}[^[:space:]\{1,\} '/etc/rc.d/sysinit

5. Display the line beginning with at least one blank character in/boot/grub/grub.conf;

# grep ' ^[[:space:]]\{1,\} '/boot/grub/grub.conf

6, find out the/etc/passwd file in one or two digits;

# grep ' \<[0-9]\{1,2\}\> '/etc/passwd #其中一对尖括是用来锚定的, otherwise like 1234, such a number will also match, the topic of the clear 1-bit or 2-bit

7. Find the integer between 1 and 255 in the result of ifconfig command;

# Ifconfig | Grep-e--color=auto "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \> "

8. View all the information of the root user on the current system;

grep ' ^root\> '/etc/passwd

9, add user bash and Testbash, basher, and then find the current system on its user name and the default shell of the same user;

# Useradd bash; useradd Testbash; Useradd basher # Multiple commands separated by semicolons # grep ' ^\ ([[: alnum:]]\{1,\}\). *\1$ '/etc/passwd

10. Find the line ending with "LISTEN" or "established" in the result of netstat-tan command execution;

# Netstat-tan | Grep-e--color=auto ' (listen| Established) [[: space:]]*$ '

11, remove all the users of the current system shell, requirements: Each shell is displayed only once, and in ascending order;

# cut-d:-f7/etc/passwd | Sort-u

This article is from "Rookie Diary" blog, please make sure to keep this source http://zkxfoo.blog.51cto.com/1605971/1660709

grep command uses the Xiang solution

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.