First, Grep,egrep,fgrep
1) grep
Format: grep [options] [mode] [file name]
Common options:-N: Display line numbers and matching rows
-V: Reverse match
-C: No matching rows are displayed, only the number of matching rows is displayed
-I: Ignore case
-R: Recursive search
-E: Supports extended regular expressions
-P: Support for Perl regular expressions
-F: Regular expressions are not supported, and patterns are matched by literal meaning
Example:
grep sample
|
Description
|
grep ' \<tom>\ ' file
|
Show rows that contain the word Tom
|
grep ' Tom Jerry ' file
|
Show a line containing ' Tom Jerry '
|
grep ' ^tommy ' file
|
Display lines starting with the Tommy string
|
grep ' \.bak$ ' file
|
Displays lines ending with. bak at the end of the line
|
grep ' [Pp]hoto ' *
|
Displays the file name of a photo or photo in all the file names of the current working directory
|
grep ' [A-z] ' file
|
Display a line with at least 1 uppercase letters
|
grep ' [0-9] ' file
|
Display a line with at least 1 digits
|
grep ' [A-z] ... [0-9] ' file
|
Displays lines that start with a capital letter and end with a number of 5 string patterns
|
Grep-w ' [tt]est ' file
|
Show rows that contain but test and test
|
Grep-s ' TY Kim ' file |
Search for rows containing ' TY Kim ', but not displayed on screen
|
Grep-v ' Jerry ' file |
Show all lines that do not contain ' Jerry '
|
Grep-i ' Sam ' file |
Ignore uppercase and lowercase letters, showing all rows containing Sam
|
Grep-l ' Dear Boss ' * |
Displays all file directories that contain ' Dear Boss '
|
Grep-n 'to M ' File |
Display rows that contain Tom, along with line numbers
|
grep "$name" file |
Displays all rows that have the value of the name variable. Double quotation marks ("") must be used when using variables
|
grep ' $ ' file
|
Displays the line that contains the ' $ $ ' character. The $ character within the single quotation mark is recognized as its own
|
PS aux | grep ' ^*multi '
|
Connect the display results of PS with the pipe character and grep, displaying all lines with a space at the beginning of the line and containing multi
|
2) Egrep
Egrep is the extension of grep and can be used with other wildcard characters
+: More than 1 characters
? : There are 0 or 1 characters
A|b: Matches the character of A or b
(): Character Group
3) Fgrep
Similar to grep, the special characters and the $ characters are recognized as themselves
Ii. SED
Non-interactive text file editor
Common options:
-N: Cancel default output
-e: Allow multiple scripts to execute
-I: Modify the original file directly
-R: Using extended regular expressions
Example:
sed example
|
Meaning
|
8d
|
Delete line 8th of input
|
/^$/d
|
Delete all empty rows
|
1,/^$/d
|
Delete from the first line until a blank line is found
|
/jones/p
|
Show only rows that contain "Jones" (using the-n option)
|
s/windows/linux/
|
Replace the first occurrence of "Windows" with "Linux" on each line entered
|
S/windows/linux/g |
Replace each occurrence of "Windows" with "Linux" on each line entered |
s/*$//
|
Remove all whitespace at the end of a line
|
S/00*/0/g
|
Replace all consecutive occurrences of 0 with 0
|
/gui/d
|
Delete all rows that contain "GUI"
|
S/gui//g
|
Remove the "GUI" three characters from each line entered
|
This article is from the "Mylinux" blog, make sure to keep this source http://mylinuxlife.blog.51cto.com/4706737/1631181
Linux Learning Notes-The 12th lesson-shell Script's regular expression (i)