Detailed explanation of grep command

王林
Release: 2020-07-02 13:52:32
Original
7779 people have browsed it

grep is a powerful text search tool that uses regular expressions to search text and prints matching lines. Common syntax of grep command: [grep [-acinv] [--color=auto] 'Search string' filename].

Detailed explanation of grep command

grep

(Recommended tutorial: linux tutorial)

grep is a powerful Text search tool that uses regular expressions to search for text and prints matching lines.

Common syntax:

[root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename
Copy after login

Options and parameters:

  • -a: Search binary files as text files

  • -c: Count the number of times 'search string' is found

  • -i: Ignore the difference in case, so the case is considered the same

  • -n: Output line number

  • -v: Reverse selection, that is, display the line without 'search string' content

  • --color=auto: You can add color to the found keyword part

(Video tutorial: linux video tutorial )

Example:

Take out the lines where root appears in /etc/passwd

# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
或
# cat /etc/passwd | grep root 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
Copy after login

Take out the lines where root appears in/etc/passwd, and display these at the same time Line number in /etc/passwd

# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
30:operator:x:11:0:operator:/root:/sbin/nologin
Copy after login

The above is the detailed content of Detailed explanation of grep command. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template