Shell Programming---grep command

Source: Internet
Author: User
Tags egrep

grep is a powerful text-search tool that uses regular expressions to search for text and print matching lines


grep [option] [mode] [file]


-C #只输出匹配行的数量
-I. #搜索时忽略大小写
-H #查询多文件时不显示文件名
-L #只列出符合匹配的文件名 instead of listing specific matching rows
-N#列出所有的匹配行 and displays the line number
-S#不显示不存在或无匹配文本的错误信息
-V#显示不包含匹配文本的所有行
-W#匹配整词
-X#匹配整行
-R#递归搜索 not only search the current working directory, but also search subdirectories
-Q#禁止输出任何结果, the exit status indicates whether the search was successful
-B#打印匹配行距文件头部的偏移量, in your own unit
-O#与-B option to print the offset of the matched word from the head in bytes, in combination with
-E#支持扩展的正则表达式
-F#不支持正则表达式, match the literal meaning of the string

Grep-c root/etc/passwd #文本中有两个root匹配, which shows the number of rows is 2
Grep-i XXX 1#文本中有XXX和xxx,-I will ignore the case after the output.
Grep-h Root/etc/passwd/etc/shadow#原本查询多文件时, the match will show the file name and matching rows, and only the matching rows will be displayed after the plus-H, without showing which file matches
Grep-l Root/etc/passwd/etc/shadowOnly matching file names are displayed after #加-L, not specific matching rows
Grep-n root/etc/passwd#-n matches the line number in front of the output match result
Grep-s root/etc/passwd#不显示错误信息, for example: grep ROOT/ETC/PASSWDWDWD
Grep-v root/etc/passwd #列出除了匹配行root以外的所有行
Grep-w root/etc/passwd #列出文件中有单个root的单词的行
Grep-x root/etc/passwd #列出文件中有单个root为整行的行
Grep-r root/tmp#列出 root in/tmp under root and subdirectory files, recursively query out all root characters
Grep-q Root/tmp/root #不输出任何信息, with the Exit method, success is 0, unsuccessful for other values

GREP-VC Root/etc/passwd/etc/shadow #显示出文件pass和sha中有多少行是不包含root的行


1. Match the beginning of the line
(1), search "/etc/passwd" in the beginning of "root" line, and print travel number
Grep-n ^root/etc/passwd
(2), retrieve the blank line in the "/etc/services" file, and list the line number. Retrieving empty lines again with a few lines
Grep-n ^$/etc/services
Grep-c ^$/etc/services
(3), the search file begins with "-", and repeats any time, followed by the D-Character matching line
Search file with "/" Start, Intermediate "4" any characters, the "6" character is still "/" matching line
grep ^-*d filename
grep ^/..../filename
(4), Search for "sed.edu.cn" matching line
grep sed\.edu\.cn
(5), search "-" symbol repeated 5 times the line of text
Search for "the" line that matches exactly
Features of grep ' \-\{5\} ' filename #考验 ' and \{\} expressions
Features of grep ' \<the\> ' filename #考验 ' and \<\> expressions

grep #标准grep命令. Support for regular expressions
Egrep #扩展grep命令. Support for basic and extended regular expressions
Fgrep #快速grep命令. Regular expressions are not supported and are matched by the literal meaning of the string

Egrep Command and GREP-E equivalence
Fgrep Command and Grep-f equivalence


Analyze what the following regular expression means
(1), kk* grep kk* test #列出文件test包含kK及后面有任意多个字符的行
(2), k\{6,8\} grep ' k\{6,8\} ' test #列出文件test中k单词重复6-8 rows
(3), k\{6,\} grep ' k\{6,\} ' test #列出文件test中k单词大于6个的行
(4), k\{10\} grep ' k\{6\} ' test #列出文件test中k单词为6个的行
(5), ^new year$
(6), ^$#空行
(7), [0-9][0-9][a-z] grep [0-9][0-9][a-z] File #列出文件中前两个字符为数字, the next character is a matching line of letters
(8), [a-h]\{1,3\},[0-9]\{5\} #grep ' [a-h]\{1,3\} ' Test | grep ' [0-9]\{5\} ' #列出1-3 a-h words, 5 digits of a character
(9), ^\ ...
(10), [^p-z]*\. #列出除了p a line of any character other than-Z

2. Use the wildcard function to list all numbers starting with a number, and the last 3 to be a period and 2 of any letter of the file name
[0-9]*[a-z]\{2\} ...

3. View the following three commands
Grep-c ^$ filename #列出空行的数目
Grep-c ^[^$] FileName #列出非空行的数目
GREP-C ^^ $ filename

4. Count the number of blank rows in the current directory and all files under subdirectories
Grep-r ^$/root/| Wc-l

5. Count the number of rows that contain non-blank rows for all files in the current directory and sub-directories
Grep-r ^[^$]/root/| Wc-l

6, the combination of the description of the symbol, to observe whether the right and wrong
Grep-n-\{5,\} file #缺引号, missing escape character \-
Grep-n '-\{5,\} ' file #缺转义符, system default does not know-yes option
Grep-n ' \-\{5,\} ' file #正确

Shell Programming---grep command

Related Article

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.