The text Processing tool in Linux, Grep,egrep

Source: Internet
Author: User
Tags echo command egrep

A: A brief introduction to grep and Egrep :
grep and Egrep are more of a tool for Linux text Search in Linux. It combines regular expressions to implement rows that match the model.

Two: syntax format :

grep [option] ... ' PATTERN ' FILE ... Support for basic regular expressions
egrep [option] ... ' PATTERN ' FILE ... Supports extended regular expressions
The main uses of [option] are:
-V: Reverse selection shows which rows are not matched to
-O: Displays the matched string instead of the entire row
-i:ignore-case, ignoring character case
-E: Supports the use of extended regular expressions equivalent to Egrep
PATTERN:
The main point here is the regular expression, the regular expression is a class of characters written by the pattern, many of the characters do not represent its literal meaning, but the expression of control or the function of the wildcard;
Metacharacters: does not represent its literal meaning, but is used for additional functional descriptions, namely basic regular expressions and extended regular expressions.


Three: We combine grep and egrep examples to briefly introduce the next two differences:
1. Display the line beginning with at least one blank character in/boot/grub/grub.conf;
# grep "^[[:space:]]\{1,\}"/boot/grub/grub.conf
# egrep "^[[:space:]]{1,}"/boot/grub/grub.conf
2. Displays the default shell for root, fedora, or User1 users on the current system;
# grep-e "^ (root|fedora|user1):"/etc/passwd|Cut-d:-f7 Note that the "|" in the logical expression can be used directly Symbol
3. Locate a word in the/etc/rc.d/init.d/functions file followed by a set of parentheses "()" lines;
# grep-o "\<[[:alnum:])\+\>\ (\) "/etc/rc.d/init.d/functions
# egrep-o "\<[[:alnum:])+\>\ (\) "/etc/rc.d/init.d/functions Note that the" + "sign in the extended regular expression can be used directly
4. Use the echo command to output a path, and then use grep to remove its base name;
# echo "/etc/sysconfig/" | Grep-o "[[: alnum:]]\+/\?"
# echo "/etc/sysconfig/" | Egrep-o "[[: alnum:]]+/?"Note which can be used directly?" , grep needs to be translated
It is not difficult to see that egrep can directly use some of the characters that are encountered in grep that need to be translated


Four: grep and egrep summary :
The basic regular expressions supported by grep are:
Character matching:.,. *, [], [^]
Number of matches: (match more than one character by grouping) *, \?, \+, \{m,n\}
Location anchoring: ^, $, \< (\b), \> (\b)
Grouping and referencing: \ (\), \ n

Extended regular expressions supported by Egrep are:
Number of matches: *,?, +, {m,n}
Group reference: (), \ n
Or: a|b
So we find the use of egrep more powerful and concise.


This article is from the Linux OPS blog, so be sure to keep this source http://germanygu.blog.51cto.com/3574209/1437582

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.