Linux text search tool--grep

Table of contents

Linux text search tool--grep

effect

Format

parameter

Notice

Example

Operation object file: /etc/passwd

grep filter command example


Linux text search tool--grep

effect

        grep is a powerful file search and filtering tool in Linux. It can retrieve file contents according to regular expressions and display the matching results on the screen (matching content will be marked red)

Format

        grep -parameter condition filename

parameter

parameter effect
-i   Ignore case
-c Count the number of matching rows
-v  Negate , do not display matching lines
-w match words
-E     Equivalent to egrep, which enables extended regular expressions
-n Show line number
-rl Print files in the specified directory
-A number  Match line and n lines below
-B numbers Match lines and n lines above
-C numbers Match lines and n lines above and below
-q     Silent mode, no content is output , use $? to determine whether the execution is successful.
-o  Show only matching content

Notice

        How it works --- grep can search in one or more files. If the condition contains spaces, you need to use double quotes

        Work result --- If the search is successful, a status code of 0 is returned , otherwise a status code of 1 is returned.

Example

Operation object file: /etc/passwd

       Function ---Record account information, divided into 7 segments, separated by colons

        Meaning ---Account name: Password code x: UID: GID: Comment: Home directory: Login Shell

Notice:

/sbin/nologin          in the last paragraph indicates that login is not allowed

grep filter command example

[root@localhost ~]# grep -n "root" /etc/passwd

[root@localhost ~]# grep -n "sshd" /etc/passwd

[root@localhost ~]# grep -n "/sbin/nologin" /etc/passwd

[root@localhost ~]# grep -c "/bin/bash" /etc/passwd

[root@localhost ~]# grep "/bin/bash" /etc/passwd | wc -l

[root@localhost ~]# grep -nv "/sbin/nologin" /etc/passwd

[root@localhost ~]# grep -ni "d" /etc/passwd

[root@localhost ~]# grep -nA2 "ftp" /etc/passwd

[root@localhost ~]# grep -nB3 "ftp" /etc/passwd

[root@localhost ~]# grep -nC3 "ftp" /etc/passwd

Guess you like

Origin blog.csdn.net/qq_57289939/article/details/132854424