Linuxgrep command learning and summary
-Grep description
The grep command is a powerful text search tool in linux. How does grep name it?
Grep:G(Globally) search forRe(Regular expression) andP(Print) the results
From the English explanation above, we can see that the grep command uses a regular expression to search for text and print the matched rows.
Ii. grep format
Grep [OPTION]... PATTERN [FILE]...
Iii. OPTION-I: matching is case-insensitive.
-W: match only the entire word, not a part of the string. (for example, if PATTERN = 'she', match the word "she" instead of "shell)
-H: the file name is not displayed at the beginning of each line during output.
-H: the file name is displayed at the beginning of each line during output.
-N: the row number is displayed on each line during output.
-C: only the matching row count of each file is displayed in the output.
-L: only file names that do not contain matching items are displayed in the output.
-L: only names containing matching items are displayed in the output.
-O: only matching items are displayed in each line during output.
-S: Ignore the error message.
-V: reverse match. Select unmatched rows.
-A: After indicates that the data in the n rows After the match is displayed.
-B: before indicates that the data of the first n rows of the matching item is displayed.
-C: displays data of n rows before and after matching strings.
-- Color: highlight the Matching content in a specific color.
The -- color option is a very good option that lets you know which characters are matched.
You 'd better add the following to your. bashrc file:
alias grep='grep --color=auto'
Use source. bashrc to take effect immediately. After each grep search, the matching effect is automatically highlighted.
grep 'she' test.txtgrep -i 'she' test.txtgrep -c 'she' test.txtgrep -in 'she' test.txtgrep -v 'she' test.txtgrep -w 'she' test.txtgrep -o 'she' test.txtgrep -C 2 'she' test.txt
-O Parameters
Assume that grep 'she' test.txt output is
She is beautiful
I like she
Then grep-o 'she' test.txt output is
She
She
This means that only the matching part is displayed, and other parts in the row are not displayed.
In addition, PATTERN is a regular expression. To avoid the influence of shell metacharacters on regular expressions, enclose it with single quotation marks ('') instead of double quotation marks ("") or not included.
4. Basic Regular Expression PATTERN4.1
\: Escape Character.
^: Start of the anchor row. For example, '^ help' matches rows starting with 'help'.
$: The end of the anchor row. For example, 'help $ 'matches rows ending with 'help'.
.: Match any non-linefeed character. For example, 'he. P' matches help and heap, but cannot match
*: The preceding characters must be repeated 0 to multiple times. For example, 'he * P' matches hp, heep, and so on.
[List]: match a character in the character set. For example, '[hk] elp' matches help and kelp.
[N1-n2]: match a character in the character range. For example, '[a-z] elp' matches aelp, belp,..., zelp
[^ List]: matches a character other than the character set. For example, '[^ a-d] elp' does not match aelp, belp, celp, and delp. Matching eelp, help, elp, etc.
\ {N1, n2 \}: the previous characters repeat the n1-n2 times. For example, 'H \ {1, 3 \} elp' matches help, hhelp, and hhhelp.
\ <: Specifies the start of a word. For example, '\ <H' matches words starting with h. Matches help, but does not match while
\>: Specifies the end of a word. For example, 'P \> 'matches words ending with p. Matched with help, but not person
Note: The occurrence of ^ at the beginning of PATTERN indicates the start of the anchor row. The occurrence of other positions is only a ^ character.
For example:
Grep '^ help' test.txt indicates matching rows starting with "help"
Grep 'help ^ 'test.txt indicates matching rows containing the string help ^
Similarly, $ appears at the end of PATTERN to indicate the end of the anchor row. Other positions appear only as the $ character.
The PATTERN is '^ $', indicating empty rows, because only the beginning and end of the row are allowed.
N2 in \ {n1, n2 \} can be omitted, indicating that the previous characters repeat n1 multiple times, and there is no upper limit on repeated times
4.2 extend the Regular Expression
Grep generally supports basic regular expressions. You can use the-E parameter to support extended regular expressions. In addition, grep provides an extension command called egrep to support extended regular expressions, this command is equivalent to grep-E.
+: The preceding characters are repeated for one to multiple times. For example, if 'he + P' matches with the type of heep, heep, or heeep, but does not match the type of hp
? : Match the first character of 0 or 1 time. For example, 'he? P' matches hp
|: Or matches multiple strings. For example, 'help | heap 'matches help and heap.
(): Match the string in the brackets. For example, '(help) +' matches help and helphelp. Note the difference with 'help + '.