Linux Text Processing command-grep

Source: Internet
Author: User
Tags character classes dmesg egrep

Introduction

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

The grep family of Unix includes grep, Egrep, and Fgrep. Egrep and Fgrep commands are only a small difference from grep. Egrep is the extension of grep, which supports more re metacharacters, and fgrep is fixed grep or fast grep, which regards all the letters as words, that is, the metacharacters in the regular expression represents the literal meaning back to itself, no longer special. Linux uses the GNU version of grep. It is more powerful and can use the Egrep and FGREP functions with the-G,-e,-f command line options.

grep common usage
[Email protected] ~]#grep[-ACINV] [--color=auto]'Search String'filename Options and Parameters:-A: the binary file is searched for data in the form of a text file-C: Calculation found'Search String'the number of-I: ignoring case differences, so case is considered the same-N: Output line number  by the way-V:Reverse Selection, which shows no'Search String'The content of that line! --color=auto: You can find thekeyword part plus colorThe Display Oh!

For example:

Use DMESG to list the core information, then grep to find the line containing the ETH, to be caught in the keyword color, and add line number to indicate:

[Email protected]:~$ DMESG |grep-N--color'ETH'801:[2.775859] Jme:jmicron jmc2xx Ethernet driver version1.0.8809:[2.778396] Jme0000: -:00.5eth0:jmc250 Gigabit Ethernet Chiprev: atPcirev:3MACADDR:xx: 1b:8c: on: Geneva:Geneva904:[16.531794] Jme0000: -:00.5Eth0:link is down905:[16.531837] Ipv6:addrconf (NETDEV_UP): Eth0:link is isn't ready
grep and regular Expressions

Character class

Search for character classes: If I want to search for the two words of test or taste, I can find that they have a common ' t?st ' existence ~ this time, I can search for:

grep ' t[ae]st ' regular_express.txt

Inverse selection of character classes [^]: If you want to search for a line with OO, but do not want oo preceded by G, as follows:

grep ' [^g]oo ' regular_express.txt

Continuous character class: Again, suppose I don't want to have a small write section in front of Oo, so I can write [^abcd....z]oo, but this doesn't seem so convenient, because the order of encoding on the ASCII of lowercase bytes is sequential, so we can simplify it to the bottom:

grep ' [^a-z]oo ' regular_express.txt

That is, when we are in a set of collection bytes, if the byte group is contiguous, such as uppercase English/lowercase english/numeric, etc., you can use [a-z],[a-z],[0-9] and other ways to write, then if our request string is the number and English? Oh! He wrote it all together and became: [A-za-z0-9].

Beginning of line with trailing bytes ^ $
Line beginning character: What if I want the only one listed at the beginning of the line? This is the time to use the anchor byte! We can do this:

grep ' ^the ' regular_express.txt

Note: At this time ^ is outside of [], must be separated from the preceding character class, ^ symbol, which is different from outside of the character class symbol (parentheses [])! In [] represents the "reverse selection", outside [] represents the meaning of positioning at the beginning of the line!

So, if I don't want to start with the English alphabet, then it can be:

grep ' ^[^a-za-z] ' regular_express.txt

So if I want to find out, the line ending at the end of the row is the decimal point (.):

grep ' \.$ ' regular_express.txt

It is important to note that because the decimal point has other meanings (described below), you must use the escape character (\) to remove its special meaning!

Find the blank line:

grep ' ^$ ' regular_express.txt

Of course, other regular expression techniques can also be used here.

Linux Text Processing command-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.