Using grep and regular expression to realize text-to-match quickly and precisely

Source: Internet
Author: User
Tags egrep

What is grep?

grep (Global search Regular expression (RE) and print out of the line, whose full meaning is global searching for regular expressions, is printed out. is a powerful, easy-to-use text Search tool. It can match the text in the file according to the matching method, and print the whole line that matches.

in Linux, the grep family has three members, respectively, Grep,egrep and Fgrep, which are used slightly differently, where egrep is the extension of grep, while fgrep is processing all the letters as words when making text-to-pass. Let the meta-characters in the regular expression lose meaning and treat them as ordinary words. The Egrep and FGREP functions can be implemented with grep followed by the-E or-f option when used.

Common options and general usage for grep.

The frequently used options for grep are the following

-V: Reverse Displays the lookup text, that is, matches to the instead of display, displays the characters that have not been matched.

Example: "Hello" string in normal match file test          

[[email protected] tmp]# grep "Hello" test


Plus-v option

[[email protected] tmp]# grep-v "Hello" test

This is a test txt

Welcome to use grep

-I: Ignore case in text match

[email protected] tmp]# grep-i " How is You" test

How is You

How is You

-N: Displays the line number when the text is output

[email protected] tmp]# grep-n " How is You" test

5:how is You

-C: Shows the number of times the match was matched

[email protected] tmp]# Grep-ic " How is You" test


--color=auto: Use this option to display the string that is matched to

[email protected] tmp]# grep--color=auto "how" Test

how is you

Other options are no longer mentioned, and can be viewed by viewing the grep help document.

grep combines regular expressions for fast and accurate matching of text.

Combined with grep and regular expressions, you can quickly and accurately find the strings and rows you want to match and speed up your work.

Example 1: Find the row in the/etc/passwd file where root is located

[[email protected] tmp]# grep--color=auto "^root"/etc/passwd

Root : X:0:0:root:/root:/bin/bash

Example 2: Find the SYSTEM account in the/TEC/PASSWD file, display its user name and UID

[Email protected]p tmp]# cut-d:-f1,3/etc/passwd|grep--color=auto "\<[1-4[0-9][0-9]\>"


operator: One

Games :














Example 3: Taking the base name of a string/etc/sysconfig/

[[ Email protected] tmp]# echo/etc/sysconfig |grep-e-o "/[[:alpha:]]*$" | grep-o-E "[[: alnum:]]*]


Example 4: Match a reasonable IP address in the Testip file

[email protected] tmp]# cat Testip


[email protected] tmp]# cat Testip |grep--colorauto-e "(^\b[1-9]\b|^\b[1-9][0-9]\b|^\b1[0-9][0-9]\b|^\b2[1-3[ 0-9]) \. (\b[0-9]\b|\b[1-9][0-9]\b|\b1[0-9][0-9]\b|\b2[0-4][0-9]\b|\b25[0-5]\b) \. (\b[0-9]\b|\b[1-9][0-9]\b|\b1[0-9][0-9]\b|\b2[0-4][0-9]\b|\b25[0-5]\b) \. (\b[0-9]\b|\b[1-9][0-9]\b|\b1[0-9][0-9]\b|\b2[0-4][0-9]\b|\b25[0-5]\b) $ "

This article is from the "linux-Bubble" blog, make sure to keep this source

Using grep and regular expression to realize text-to-match quickly and precisely

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: 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.