The grep command of the Linux shell

Source: Internet
Author: User
Tags syslog egrep

grep command


grep Example:

[OPTIONS] PATTERN [FILE ...]

[Email protected]:~# grep ' root '/etc/passwd root:x:0:0:root:/root:/bin/bash [email protected]:~# grep "$USER"/ ETC/PASSWD Root:x:0:0:root:/root:/bin/bash [email protected]:~# grep ' whoami '/etc/passwd root:x:0:0:root:/root:/ Bin/bash [email protected]:~# grep ' $USER '/etc/passwd

Parameters:

--color=auto: Color shows what you've searched for

-I: Ignore character case

-N: Displays the matching line number Grep-n root/etc/passwd

-O: Show only the matched string Grep-o ' root '/etc/passwd

-Q: Silent mode, do not output any information, through the echo $? To determine whether to find

-A #:after, after # lines, matches to the line, and matches the line to the after # line

[Email protected]:~# grep-n-A 3 root/etc/passwd 1:root:x:0:0:root:/root:/bin/bash 2-daemon:x:1:1: Daemon:/usr/sbin:/usr/sbin/nologin 3-bin:x:2:2:bin:/bin:/usr/sbin/nologin 4-sys:x:3:3:sys:/dev:/usr/sbin/nol Ogin

-B #:before before # line, matched to the row, and matched to the first three rows of the row

[Email protected]:~# grep-n-B 4 mysql/etc/passwd 33-hplip:x:114:7:hplip system user,,,:/var/run/hplip :/bin/false 34-pulse:x:115:122:pulseaudio daemon,,,:/var/run/pulse:/bin/false 35-runingday:x:1000:1000:runin Gday,,,:/home/runingday:/bin/bash 36-sshd:x:116:65534::/var/run/sshd:/usr/sbin/nologin 37:mysql:x:117:125:My SQL Server,,,:/nonexistent:/bin/false

-C #: # lines before and after the context

[Email protected]:~# grep-n-C 2 syslog/etc/passwd 18-nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/ Nologin 19-libuuid:x:100:101::/var/lib/libuuid:20:syslog:x:101:104::/home/syslog:/bin/false 21-messa Gebus:x:102:106::/var/run/dbus:/bin/false 22-usbmux:x:103:46:usbmux daemon,,,:/home/usbmux:/bin/false

-E: Implementing a logical or relationship between various options

[Email protected]:~# grep-e ' root '-e ' syslog '/etc/passwd root:x:0:0:root:/root:/bin/bash syslog:x : 101:104::/home/syslog:/bin/false

-W: Entire line matches Whole word

[Email protected]:~# grep-w oo/etc/passwd [email protected]:~# grep-w root/etc/passwd root:x:0:0 : Root:/root:/bin/bash

-e: Using the ere extension regular expression


    1. Meta-character classification

Character matching

      • . Match any single character

      • [] matches any single character within the specified range

      • [^] matches any single character outside the specified range

      • [:d igit:] All numbers

      • [: Lower:] All lowercase letters

      • [: Upper:] All uppercase letters

      • [: Alpha:] All letters, including case

      • [: Alnum:] All letters and numbers

      • [:p UNCT:] All punctuation

      • [: Space:] Space and Tab

Number of Matches

    • * match the preceding character any time, including 0 times

    • . * Any character of any length

    • \ match the preceding character 0 or 1 times

    • \+ matches the characters in front of it at least 1 times

    • \{m\} matches the preceding character m times

    • \{m,n\} matches the preceding character at least m times, up to N times

    • \{,n\} matches the preceding character up to n times

    • \{m,\} matches the preceding character at least m times

Anchor at beginning of line

    • ^ Beginning of the line anchor, for the leftmost mode

    • $ line End anchor for the right side of the pattern

    • \< or \b The first anchor for the left side of the word pattern

    • \> or \b Ending anchor for the right side of the word pattern

    • \<pattern\> Match Whole Word

    • ^pattern$ for pattern matching entire row

      • ^$ Matching Blank lines

      • ^[[:space:]]*$ Blank Line



Practice:

    1. Displays the UID and default shell for the current system root, Runingday, or syslog user

    2. Find the line with a parenthesis followed by a word (including an underscore) in the/etc/rc.d/init.d/functions file

    3. Use Egrep to remove a base name from the/etc/rc.d/init.d/functions

    4. Use Egrep to remove the directory name from the path above

    5. Find the IPV4 address of this machine in the ifconfig command result

    6. Displays lines in the/proc/meminfo file that begin with size s (required: in two ways)

    7. Displays lines in the/etc/passwd file that do not end in/bin/bash

    8. Displays the user name of the user with the largest ID number in the/etc/passwd file

    9. Show user RPC default shell program

    10. Find the two-bit or three-digit number in the/etc/passwd

    11. Displays a line in a/etc/grub2.cfg file that starts with at least one whitespace character and is followed by a non-whitespace

    12. Find lines that end with listen followed by 0, 1, or more whitespace characters in the Netstat-tan command result

    13. Add user bash, Testbash, basher, and Nologin (whose shell is/sbin/nologin) and then find the row in the/etc/passwd file for the username and shell name


This article from "Fan tea" blog, declined reprint!

grep command for Linux shell

Related Article

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.