Detailed usage of grep

Source: Internet
Author: User
Tags dmesg
    [root@www ~]# grep [-ACINV] [--color=auto] ' search string ' filename options and Parameters:-A: Search the binary file as a text file-C : Calculates the number of times to find the ' search string '---I: Ignores case differences, so capitalization is treated as the same-N: by the way output line number-V: Reverse selection, which shows the line without the ' search string ' content. --color=auto: Can be found in the keyword part of the color display OH. In terms of the display of keywords, grep can use--color=auto to display the keyword parts in color. This is a very good function ah. But if you have to use grep every time you have to add the--color=auto is also a very troublesome ~ at this time the useful alias to deal with it. You can add this line in the ~/.BASHRC: "Alias grep= ' grep--color=auto '" and "source ~/.BASHRC" to take effect immediately oh. So every time that you run grep, he will automatically add color to your display   1. Will/etc/passwd, there appears root line out to # grep root/etc/passwd root:x:0:0:root:/root:/bin/bash op Erator:x:11:0:operator:/root:/sbin/nologin or # CAT/ETC/PASSWD | grep root  root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin   2. Will/etc/passwd, There are rows that appear root are taken out, and the lines that appear in the/etc/passwd line number # Grep-n root/etc/passwd 1:root:x:0:0:root:/root:/bin/bash 30:operator:x:11:0:o Perator:/root:/sbin/nologin   3. Will/etc/passwd, will not appear root of the row out of the # grep-v root/etc/passwd root:x:0:0:root:/root:/bin /bash oPerator:x:11:0:operator:/root:/sbin/nologin   4. Will/etc/passwd, will not appear root and Nologin row out to # grep-v ROOT/ETC/PASSWD | Grep-v nologin root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin   5. List the core information with DMESG, Then with grep to find the line containing ETH, to be captured by the keyword color, and add line number to indicate: [Root@www ~]# DMESG | Grep-n--color=auto ' eth ' 247:eth0:realtek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 248:eth0:identified 8139 Ch IP type ' rtl-8139c ' 294:eth0:link up, 100Mbps, Full-duplex, LPA 0xc5e1 305:eth0:no IPv6 Routers present # You'll find special in addition to ETH In addition to the color to indicate, the front still has line number OH.   6. Use DMESG to list the core information, and then grep to find the ETH line, the first two lines of the key line and the following three lines also caught together to show [Root@www ~]# DMESG | Grep-n-a3-b2--color=auto ' eth ' 245-pci:setting IRQ level-triggered 246-acpi:pci] ; Link [lnkb] ... 247:eth0:realtek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ ten 248:eth0:identified 8139 chip type ' rtl-8139c ' 249-INP UT:PC Speaker as/class/input/input2 250-acpi:pci Interrupt 0000:00:01.4[b]-> LinK [lnkb] ... 251-HDB:ATAPI 48X DVD-ROM dvd-r-ram cd-r/rw drive, 2048kB Cache, UDMA (66) # as shown above, you will find that the first two lines of the keyword 247 and three lines after 248 are also displayed. # This allows you to capture the data before and after the keyword analysis.   7. Recursively find directories based on file content # grep ' energywise ' *         #在当前目录搜索带 ' energywise ' line of files # grep-r ' energywise ' *       #在当前目录及其子目录下搜索 ' energywise ' files

# grep-l-R ' energywise ' * #在当前目录及其子目录下搜索 ' energywise ' files, but does not display matching rows, only matching files are displayed



Grep-v "File" a

Filter the row of file in a

Grep-e "2004:22:5[0-9]" a

Find the time in a file in the 2004:22:50->2004:22:59 row

Greap-e "^[^210]" a

Find rows in a file that do not contain 210

Grep-e "H*p" a

Rows containing H and p in a file

Grep-e "[5-8][6-9][0-3]" a

Find rows that are greater than 560 or less than 893

Grep-e "4{2}" a

Find rows that contain two 4

Grep-e "4{2,}" a

Look for rows greater than two 4

Grep-e "4{2,4}" a

Find rows greater than two 4 less than 4 4

Grep-e "^$" a

Find empty lines in a file

grep ". "A

Find contained in a file. The line

Grep-e "^4{2,4}$" a

Find a File 2 4 to 4 4

Grep-e "^d" a

Find lines in a file that start with D

Grep-e "^[^d]" a

Find rows in a file that do not start with D



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.