Shell programming --- detailed explanation of grep commands
Grep is a powerful text search tool that uses regular expressions to search for text and print matching rows.
Grep [option] [mode] [file]
-C # Only output the number of matched rows
-I # case insensitive during search
-H # When querying multiple files, the file name is not displayed.
-L # Only list matching file names, not specific Matching lines
-N # list all matched rows and display row numbers
-S # Do Not Display error messages that do not exist or do not match the text
-V # display all rows that do not contain matched text
-W # match the entire word
-X # match the entire line
-R # recursive search not only searches for the current working directory, but also subdirectories
-Q # disable output of any results. Exit indicates whether the search is successful.
-B # print the offset of the header of the matching line spacing file, in its own unit
-O # used in combination with the-B Option to print the offset of the matching word from the header, in bytes
-E # support for extended Regular Expressions
-F # regular expressions are not supported and match strings based on their literal meanings.
Grep-c root/etc/passwd # There are two root matches in the text, that is, the number of lines displayed is 2
Grep-I xxx file # The text contains XXX and xxx. After-I is used, all uppercase and lowercase are ignored and output.
Grep-h root/etc/passwd/etc/shadow # When multiple files are originally queried, the matching file name and matching row are displayed. After-h is added, only matching rows are displayed, the matching file is not displayed.
Grep-l root/etc/passwd/etc/shadow # after adding-l, only the matching file names will be displayed, without listing specific Matching lines
Grep-n root/etc/passwd #-n adds a matched row number before the output matching result
Grep-s root/etc/passwd # No error message is displayed, for example: grep root/etc/passwdwdwd
Grep-v root/etc/passwd # list all rows except matching row root
Grep-w root/etc/passwd # list the rows of a single root word in the file
Grep-x root/etc/passwd # list the rows with a single root row in the file
Grep-r root/tmp # list root and root in sub-directory files under/tmp and recursively query all root Characters
Grep-q root/tmp/root # Do not output any information. In the exit mode, the success value is 0, and the Failure value is another value.
Grep-vc root/etc/passwd/etc/shadow # shows the number of rows in the file pass and sha that do not contain the root row.
1. Match the beginning of a row
(1) Search for the rows starting with "root" in "/etc/passwd" and print the travel number
Grep-n ^ root/etc/passwd
(2) Search for empty rows in the "/etc/services" file and list the row numbers. The number of empty rows to be retrieved again
Grep-n ^ $/etc/services
Grep-c ^ $/etc/services
(3) Search for a file starting with "-" and repeat it for any time, followed by matching lines of the D character
Search for any character in the file starting with "/", with 4 in the middle. The matching line with the character "6" is still "/"
Grep ^-* D filename
Grep ^/.../filename
(4) Search for matched rows of "sed.edu.cn"
Grep sed \. edu \. cn
(5) Search for text lines with the "-" symbol repeated 5 times
Search for "the" exact matched rows
Grep '\-\ {5 \} 'filename # test the features of' and \ {\} expressions
Grep '\ <the \> 'filename # test the features of' and \ <\> expressions
Grep # standard grep command. Regular Expressions supported
Egrep # Extend the grep command. Supports basic and extended Regular Expressions
Fgrep # Quick grep command. Regular expressions are not supported. Matching is performed based on the literal meaning of the string.
The egrep command is equivalent to grep-E.
The fgrep command is equivalent to grep-F.
Analyze what the regular expression below expresses
(1) kK * grep kK * test # list the lines in which the test file contains kK and any number of characters.
(2) k \ {6, 8 \} grep 'K \ {6, 8 \} 'test # list the rows with 6 to 8 duplicate k words in the file test
(3), k \ {6, \} grep 'K \ {6, \} 'test # list the rows with more than 6 k words in the file test
(4) k \ {10 \} grep 'K \ {6 \} 'test # list the rows with 6 k words in the test file.
(5), ^ new year $
(6) ^ $ # Empty rows
(7), [0-9] [0-9] [a-z] grep [0-9] [0-9] [a-z] file # list the first two characters in the file: number, matching line with the next character as a letter
(8), [A-H] \ {1, 3 \}, [0-9] \ {5 \} # grep '[a-h] \ {1, 3 \} 'test | grep' [0-9] \ {5 \}'# 1-3 a-h words are listed, character of 5 digits
(9), ^ \...
(10), [^ p-z] * \. # list rows with any character other than p-z
2. Use the wildcard function to list all file names starting with numbers and ending with periods and 2 arbitrary letters in a directory.
[0-9] * [a-z] \ {2 \}...
3. view the following three commands:
Grep-c ^ $ filename # list the number of empty rows
Grep-c ^ [^ $] filename # list the number of non-empty rows
Grep-c ^ $ filename
4. count the number of lines containing blank lines in all files in the current directory and subdirectory.
Grep-r ^ $/root/| wc-l
5. count the number of rows containing non-blank rows in all files in the current directory and subdirectory
Grep-r ^ [^ $]/root/| wc-l
6. Check whether the pair-symbol is correct or not.
Grep-n-\ {5, \} file # Missing quotation marks, missing Escape Character \-
Grep-N'-\ {5, \} 'file # The Escape Character is missing. The system does not know by default.-Yes
Grep-n' \-\ {5, \} 'file # correct