How to use the grep command in Linux programming

Source: Internet
Author: User
Tags regular expression

grep (Global search regular RE) and print out of the line, a comprehensive search for regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text and print matching rows.

Parameters:

1.-anum,--after-context=num lists the following NUM lines, in addition to the list of conforming lines.

such as: $ grep–a1 Panda file (searches for panda-style rows from file, and displays the following 1 lines of the line)

2.-bnum,--before-context=num is relative to-a num, but this parameter is a num line that displays a line other than the conforming row and appears before it. such as: (Search for a row with panda style from file, and display the first 1 lines of the row)

$ grep-b 1 Panda file

3,-c[num],-num,--context[=num] list and list the top and bottom NUM rows, and the default value is 2.

such as: (list file In addition to the line containing the panda style and the top and bottom 2 rows) (to change the default value, directly change num)

$ grep-c[num] Panda file

4,-C,--count does not display line style lines, only shows the number of rows that match. If plus-v,--invert-match, the parameter shows the total number of rows not conforming

5,-i,--ignore-case ignore the case difference

6,-n,--line-number print line number before matching line

7,-v,--revert-match reverse search, show only mismatched rows

8. Exact match:

For example, in the extract string "48", The return result contains other strings containing "48" such as 484 and 483, which should actually extract rows that contain only 48.

An efficient way to use grep to extract exact matches is to add \> after the string is extracted. Assuming that 48 is now accurately extracted,

The method is as follows:

#grep ' 48\> ' filename

9.-S does not display an error message that does not exist or does not match text

Such as: Execute the command grep "root"/etc/password, because the password file does not exist, so the error message on the screen, if you use the grep command-s switch, you can mask the error message

To use a good grep this tool, in fact, is to write a good regular expression, so here is not the grep all the features of the example to explain, only a few examples, explain a regular expression of the wording.

$ ls-l | grep ' ^d '

Filters the contents of the LS-L output through a pipe, showing only the lines beginning with D.

$ grep ' test ' d*

Displays the rows that contain test in all files that start with D.

$ grep ' test ' AA bb cc

Displays the row that matches test in the aa,bb,cc file.

$ grep ' [a-z]\{5,\} ' AA

Displays all the lines that contain at least 5 consecutive lowercase characters for each string.

$grep ' t[a|e]st ' filename

Displays all rows that contain test or tast.

$grep ' \.$ ' filename

Displays all rows that end with.

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.