About grep
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.
(Note: This command focuses on the line, or is read by the row, search, the use of permissions is available to all users)
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 considers all the letters as words, that is, all the command regular expression symbols are also considered characters. 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.
[Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers. -S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text. Pattern Regular Expression Main parameters:
\: Escape character: Ignores the original meaning of special characters in regular expressions.
^: Convex number: Matches the regular expression anchored at the beginning of the line. For example: "^root" indicates that a row with root begins
$: Matches regular expression anchor line end. For example: "login$" indicates that all rows at the end of login
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
*: There are characters, the length can be 0.
Grep-i pattern Files: Search by case-insensitive. The default case is case-sensitive,
Grep-l pattern Files: Lists only matching file names, grep-l pattern files: Lists mismatched file names, grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not Is ' magical '),
Grep-c number pattern files: matching contexts display [number] lines, respectively,
grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.
Grep-n pattern files to display line number information
Grep-c pattern files to find total rows
Below is an example of how grep can be used.
Remove the/etc/passwd, where Root is present.
# 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
Remove the/etc/passwd, which is the root row, and display the line numbers of the rows in/etc/passwd
# grep-n Root/etc/passwd1:root:x:0:0:root:/root:/bin/bash11:operator:x:11:0:operator:/root:/sbin/nologin
The/etc/passwd will be taken, and the row will not appear as root. -V indicates inverse. Because there's not too much root, I just take the last three lines.
# grep-v ROOT/ETC/PASSWD | Tail-3 sshd:x:74:74:privilege-separated ssh:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologinmarco:x : 500:500:marco-chen:/home/marco:/bin/bash
The/etc/passwd will be removed and no root and Nologin rows will appear.
# grep-v ROOT/ETC/PASSWD | Grep-v Nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin :/sbin/haltmarco:x:500:500:marco-chen:/home/marco:/bin/bash
Edit shortcut keys to display special colors for each selected keyword
~] #vi. bashrc# User specific aliases and Functionsalias rm= ' rm-i ' Alias cp= ' cp-i ' Alias mv= ' mv-i ' (Enter alias grep= ' GRE below P--color=auto ' (others do not move.) ESC-Shift+:wq Save exit)
Finding a directory recursively based on file content
# grep ' good ' * #在当前目录搜索带 ' good ' line of files
# grep-r ' good ' * #在当前目录及其子目录下搜索 ' good ' file
# grep-l-R ' good ' * #在当前目录及其子目录下搜索 ' good ' file, but does not display matching rows, only matching files are displayed
Character class Search: In the/etc/passwd file, "N:/sbin" "Nologin" can be found, in fact, they have a common ' N.....N ' exist! So I can search for it:
[[Email protected] ~] #grep "N.....N" etc/passwd | head-4bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/ Nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
$ grep ' Nologin ' p* shows all lines that contain nologin in a file that begins with P.
$ grep ' nologin ' AA bb cc displays lines that match nologin in the aa,bb,cc file.
$ grep ' [a-z]\{5\} '/etc/passwd displays all rows that contain a string of at least 5 consecutive lowercase characters per string.
The level is limited, certainly not whole, hoped can bring you a little help
To elaborate on the grep command