06-linux Text Processing-grep

Source: Internet
Author: User

  • The grep command finds a character pattern in one or more files.
  • If the pattern contains spaces, you must enclose it in quotation marks.
grep Tom /etc/passwd
  • grep will look in the file for/etc/passwd in Find mode Tom.
  • The results are as follows:
    • Find successful, the corresponding line in the file appears on the screen
    • No output is found without finding the specified pattern
    • The specified file is not a valid file and an error message will be displayed on the screen.
    • Finding the pattern to find, grep returns exit status 0, indicating success,
    • The mode to find is not found, the exit status returned is 1,
    • When the specified file is not found, the exit status will be 2.
ps -ef | grep root
  • The output of the PS command is sent to grep, and all the rows containing the root are printed on the screen.
grep options
Options function
-E If this option is added, then the subsequent match pattern is the extended regular expression, which is grep-e = Egrep
-I. Ignore case differences when comparing characters
-W The expression as a word to find, equivalent to the regular "<...>" (... Represent your custom rules)
-X The content that is matched is exactly the entire line, which is equivalent to the regular "^...$"
-V Reverse, that is, the output of the opposite of our definition of the pattern
-C Count. Statistics, count the number of rows that match the result, mainly not the number of matches, is the number of rows.
-M Matches only the specified number of rows, after which the content is not matched
-N Display the line number in the output, it is clear here that the so-called line number is the line number of the line content in the original file, rather than the output result of the row number
-O Only matches are displayed, and grep defaults to displaying a row that satisfies the match criteria, plus this parameter shows only the matching results, for example, if we want to match an IP address, we only need the result, not the contents of the line.
-R Recursive matching. If you want to match content in multiple files or directories in a directory, you need this parameter
-B The output satisfies the first few lines of the condition row, such as the Grep-b 3 "AA" file, which indicates that a row with a AA is output in file, and the first 3 rows of the AA are also output
-A This is similar to-B, where the output satisfies the following lines of the condition row
-C This is equivalent to the simultaneous use of-b-a, that is, both front and rear output
Example
显示包含#的行,并在前面添加行数 grep -n ‘#‘ /etc/ssh/sshd_config
显示包含#的行,并关闭大小写敏感性grep -i ‘#‘ /etc/ssh/sshd_config
显示sshd配置文件,排除空行和以#号开头的cat /etc/ssh/sshd_config |grep -v ‘^$‘ |grep -v ‘^#‘
显示包含s内容的所有文件名grep -l ‘s‘ /etc/ssh/*
统计包含有#的行数grep -c ‘#‘ /etc/ssh/sshd_config
只显示包含Port词的行 grep -w ‘Port‘ /etc/ssh/sshd_config

06-linux Text Processing-grep

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.