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
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
Practice:
Displays the UID and default shell for the current system root, Runingday, or syslog user
Find the line with a parenthesis followed by a word (including an underscore) in the/etc/rc.d/init.d/functions file
Use Egrep to remove a base name from the/etc/rc.d/init.d/functions
Use Egrep to remove the directory name from the path above
Find the IPV4 address of this machine in the ifconfig command result
Displays lines in the/proc/meminfo file that begin with size s (required: in two ways)
Displays lines in the/etc/passwd file that do not end in/bin/bash
Displays the user name of the user with the largest ID number in the/etc/passwd file
Show user RPC default shell program
Find the two-bit or three-digit number in the/etc/passwd
Displays a line in a/etc/grub2.cfg file that starts with at least one whitespace character and is followed by a non-whitespace
Find lines that end with listen followed by 0, 1, or more whitespace characters in the Netstat-tan command result
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