Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed description and examples of grep filtering

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

The tools for regular expressions are: grep egrep sed awk

1.grep action

Grep command is a powerful text search tool in Linux system. It can use regular expressions to search text and print matching lines. The full name of grep is Global Regular Expression Print, which represents the global regular expression version, and its permission is for all users. You can filter specified rows

two。 Format

Grep [options]

3. Main parameters

[options] main parameters:

-c: outputs only the count of matching rows.

-I: case-insensitive (for single characters only).

-h: the file name is not displayed when querying multiple files.

-l: when querying multiple files, only the file names that contain matching characters are output.

-n: displays matching lines and 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.

The main parameters of the pattern regular expression:

\: ignore the original meaning of special characters in regular expressions.

^: matches the start line of the regular expression.

$: matches the end line of the regular expression.

\: to the end of the line matching the regular expression.

[]: single character, such as [A], that is, A meets the requirements.

[0-9]: represents a number

[-]: range, such as [Amurz], that is, A, B, C all the way up to Z meet the requirements.

. All single characters

*: there are characters, the length can be 0, and multiple characters can be matched.

. Represents any character

. * represents any character that begins with s / s and ends with d can match

\? Indicates that the letter before the escape symbol can match s\? d means to show the s before the escape or to start with a separate d.

? + for egrep

-r: match directory

-- color: marked with color

-A: you can display not only the matching lines, but also the following lines, depending on the number of lines after A.

-B: you can display not only the matching lines, but also the above lines, depending on the number of lines after B.

-C: you can display not only matching lines, but also several more lines up and down, depending on the number after C.

Example

Check the password file cat / etc/passwd

Example 1: filter out the rows of root under / etc/passwd

Grep 'root' / etc/passwd

Example 2: display and filter out the information of root under / etc/grpup

Cat / etc/group | grep 'root' / etc/group

Example 3: filter out the information of root under / etc/passwd and color mark it

Grep-- color 'root' / etc/passwd

Example 4: filter out the information of root under / etc/passwd and mark the number of outgoing lines with color.

Grep-- color-n 'root' / etc/passwd

Note: because grep-color is used more frequently, you can set temporary aliases.

Setting method: alias cg='grep-- color'

If you want to set the alias permanently, the method is

Vim .bashrc

Add a line alias cg='grep-- color'

Example 5: filter out the information of root under / etc/passwd and show only the number of rows

Cg-c 'root' / etc/passwd

Example 6: filter out lines that do not contain root information under / etc/passwd

Cg-v 'root' / etc/passwd

Example 7: filter out the information about root under / etc/passwd and display the following three lines of information

Cg-n-A 3 'root' / etc/passwd

Example 8: filter out the information about root under / etc/passwd and display more information from the above four lines

Cg-n-B 4 'root' / etc/passwd

Example 9: filter out the information about games under / etc/passwd and display more information on the upper and lower lines

Cg-n-C 2 'games' / etc/passwd

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report