grep common usage

Source: Internet
Author: User
Tags dmesg

[[email protected] ~]# grep [-ACINV] [--color=auto] ' search string ' filename options and Parameters:-A: Binary file search data as text file-c: Calculate find ' Search The number of strings '-I: ignores case differences, so the case is treated as the same-N: the output line number-V: Reverse selection, that is, the line that shows no ' 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 portion using color. This is a very good function Ah! But if you use grep every time you have to add--color=auto and show a lot of trouble ~ at this time the good alias will have to deal with it! You can add this line in ~/.BASHRC: "Alias grep= ' grep--color=auto '" and then "source ~/.BASHRC" to take effect immediately! In this way, each time you run grep, he will automatically add color to your  1. Remove the/etc/passwd, which has the root line, to the # grep root/etc/passwdroot:x:0:0:root:/root:/bin/ Bashoperator:x:11:0:operator:/root:/sbin/nologin or # CAT/ETC/PASSWD | grep root root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin 2. Will/etc/passwd, A row with root appears and displays the line number of these lines in/etc/passwd # grep-n Root/etc/passwd1:root:x:0:0:root:/root:/bin/bash30:operator:x:11:0:o Perator:/root:/sbin/nologin 3./etc/passwd, remove the row without root # grep-v Root/etc/passwdroot:x:0:0:root:/root:/bin /bashoperator:x:11:0:operator:/root:/sBin/nologin 4./etc/passwd will be removed without the presence of root and Nologin lines # grep-v ROOT/ETC/PASSWD | Grep-v nologinroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin 5. Use DMESG to list core information, With grep, find the one that contains the ETH, color the key that was caught, and add the line number to indicate: [[email protected] ~]# DMESG | Grep-n--color=auto ' eth ' 247:eth0:realtek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0:identified 8139 chip Type ' rtl-8139c ' 294:eth0:link up, 100Mbps, Full-duplex, LPA 0xc5e1305:eth0:no IPv6 routers present# you'll find that in addition to the ETH there will be special colors to watch Above, there is a line number at the front!  6. Use DMESG to list the core information, and then grep to find the line containing the ETH, in the first two lines of the keyword row and the last three lines also caught out display [[email protected] ~]# DMESG | Grep-n-a3-b2--color=auto ' eth ' 245-pci:setting IRQ Ten as Level-triggered246-acpi:pci Interrupt 0000:00:0e.0[a] Link [lnkb] ... 247:eth0:realtek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0:identified 8139 chip type ' rtl-8139c ' 249-input : PC Speaker as/class/input/input2250-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 248 after three lines are also displayed! # This allows you to take the keyword back and forth data to analyze it!  7. Finding directories recursively based on file contents # grep ' energywise ' *         #在当前目录搜索带 ' energywise ' files # grep-r ' energywise ' * &N Bsp     #在当前目录及其子目录下搜索 ' energywise ' File # grep-l-R ' energywise ' *   #在当前目录及其子目录下搜索 ' energywise ' files, but does not display matching rows , only matching files are displayed

For more advanced usage of grep, refer to
Http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html

grep common usage

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.