Some uses of shell grep

Source: Internet
Author: User

#1, total number of statistical characters

#统计系统中不允许登录的用户数

Grep-c ' Nologin '/etc/passwd


#2, case-insensitive lookup of all lines of the specified character;

Grep-i ' Good ' 9.txt


#3, print the line and line number of the specified character;

Grep-n "Good" 9.txt


#4, lines that do not print the specified characters

Grep-v "Good" 9.txt


#5, printing begins with a character good, followed by a line of numbers

grep "good[0-9]" 9.txt


#6, lines that match G or G

grep "[Gg]ood" 9.txt


#7, display the line at the beginning of the output that is not good

grep "^[^good]" 9.txt


#8, Match G, two arbitrary characters, immediately following the D line

grep "G.. D "9.txt


#9, match uppercase letters, and follow OO word lines

grep ' [A-z]oo ' 9.txt


#10, print a row with more than n occurrences of the specified character

Grep-n ' o\{2,\} ' 9.txtgrep-n ' o\{3,4\} ' 9.txt


#11, the line number where the blank line is printed

Grep-n "^$" 9.txt


#12, mismatched # and blank lines in a file

Grep-ve ' #|^$ ' httpd.conf


#13, matching files that contain the specified characters

#-r: In the current directory and its subdirectories

#-l: Matching rows are not displayed, only matching files are displayed

Grep-r-n ' good '/home/bel/*grep-r-l ' good '/home/bel/*


#14, Match IPV4 address

Ifconfig | grep--color-e "\< ([0-9]{1,3}\.) {3} [0-9] {1,3}^$\> "


#15,

#显示匹配某个结果之后的3行, use the-a option

#显示匹配某个结果之前的3行, use the-B option

#显示匹配某个结果的前三行和后三行, use the-C option

Seq 10 | grep ' 5 '-a3seq 10 | grep ' 5 '-b3seq 10 | grep ' 5 '-c3


This article is from the "Mo Jing" blog, reproduced please contact the author!

Some uses of shell grep

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.