Linux File Processing tool (grep)

Source: Internet
Author: User

Introduction

grep is a powerful text search tool that uses regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.

grep Basic Usage
    • Format

grep [options]


    • Main parameters

[Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.

    • grep command using exercise (regular expression)

1. Display the line in the/proc/meminfo file that begins with the size S; (Requires: use two ways)

[[email protected] ~]# grep ' ^[ss] '/proc/meminfo

[Email protected] ~]# grep-i ' ^s '/proc/meminfo

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/9D/4E/wKiom1l92wPB1qkfAAA1aDH3OFA409.png-wh_500x0-wm_ 3-wmp_4-s_1724101278.png "title=" ' B} ' 867cwd1@p7kmfeb7]ph.png "alt=" wkiom1l92wpb1qkfaaa1adh3ofa409.png-wh_50 "/ >

2. Display three user root, Mage, Wang's UID and default shell

[[email protected] ~]# Cat/etc/passwd|egrep "^ (Root|mage|wang) \>" | Cut-d:-f1,3,7

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/9D/4E/wKiom1l93AezPfMzAAAYkNvFT78682.png-wh_500x0-wm_ 3-wmp_4-s_1754003070.png "title=" [%u6]274$i9u1j@q4 (Hk}bj.png "alt=" wkiom1l93aezpfmzaaayknvft78682.png-wh_50 "/ >

3. Find the line at the beginning of the/etc/rc.d/init.d/functions file that has a word (including an underscore) followed by a parenthesis [Email protected] ~]# egrep "[_[:alpha:]]+\ (\)"/etc/rc.d/init.d/functions

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9D/4E/wKiom1l93FqBon3VAABUs0xSlLY259.png "title=" i[n$ Ie@l8 ' c3u0kr@09s)]i.png "alt=" Wkiom1l93fqbon3vaabus0xslly259.png "/>

4. Use Egrep to remove its base name in/etc/rc.d/init.d/functions

[Email protected] ~]# echo "/etc/rc.d/init.d/functions" | Egrep-o "[^/]+/?$"

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9D/4E/wKioL1l93Z2RmPpjAAAcY4rRxWQ095.png "title=" t{l9x {~8O4B2] Cgs}t0}fqh.png "alt=" Wkiol1l93z2rmppjaaacy4rrxwq095.png "/>

5. Use Egrep to remove the directory name of the path above

[Email protected] ~]# echo "/etc/rc.d/init.d/functions/" | Egrep-o "/.*[^/]" | Egrep-o "/.*/"

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/4E/wKiom1l93jqjo5sPAAAVkFjrJcA930.png "title=" 7mbcnoh8zgt5l%36kv0%cj9.png "alt=" Wkiom1l93jqjo5spaaavkfjrjca930.png "/>

6. Display all IPV4 addresses in ifconfig command results

[Email protected] ~]# Ifconfig | Egrep-o "\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/9D/4E/wKioL1l93viB8X4JAAAucWgx6OA993.png "title=" Kezrh1c7swn ('%0_ (mk_myu.png "alt=" Wkiol1l93vib8x4jaaaucwgx6oa993.png "/>


Linux File Processing tool (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.