Home > Article > Operation and Maintenance > Detailed explanation of grep command
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].
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
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
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
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!