sed 字符流编辑器
SED is the abbreviation for the stream editor (character stream editors), referred to as the Liu Editor.
SED is a powerful tool for manipulating, filtering, and transforming text content. Commonly used features include quick additions and deletions to files, where the two most common functions of the query function are filtering (filtering out the string) to fetch rows (take out the specified line)
Syntax format: sed "options" "sed built-in command character" "Input file"
Attention:
1.sed and subsequent options, commands, and output files, with at least one space between each element
Parameter options:
Parameters |
Description |
-I. |
Modify file Contents directly |
-N |
Silent mode, only print and SED commands match the content |
-R |
Extended expressions are supported (default is the basic formal notation French method) |
-E |
Sed action Edit directly in command-line mode, this is the default option |
-F |
Writes the action of SED in a file and executes the SED action within filename with –f filename |
-V |
-V or--version display version information |
Action parameter Description:
Parameters |
Brief Introduction |
Details |
A\ |
New |
Insert text below the current line |
C\ |
Replace |
Change the selected line to a new text |
I\ |
Insert |
Insert text above the current line |
D |
Delete |
Delete a selected row |
P |
Print |
Prints the lines of a template block, usually p is run with the parameter sed-n |
S |
Replace |
Replaces the specified character, such as 1,20 s/old/new/g |
G |
Replace |
Represents a full in-row replacement |
Y |
Update |
Translates a character to another character (but not to regular expressions) |
R |
File |
Read from the target file |
Sed meta Character set
Character Set |
Description |
Case |
\^ |
Start of anchoring lines |
such as: ' ^grep ' matches all lines that begin with grep |
$ |
End of Anchor Line |
For example: ' grep$ ' matches all rows ending with grep |
. |
Match a character that is not a line break |
such as: ' GR.P ' matches gr followed by an arbitrary character, then P |
* |
Match 0 or more of previous characters |
such as: ' *grep ' matches all one or more spaces followed by the grep line. * Together with any character |
[ ] |
Matches a character in a specified range |
such as: ' [gg]rep ' matches grep and grep |
[^ ] |
Matches a character that is not within the specified range |
such as: ' [^a-fh-z]rep ' matches the beginning of a letter that does not contain a-f and h-z, immediately following the line of the rep |
\(.. \) |
Tag matching characters |
such as: ' (Love) ', Love is marked as 1 |
& |
Save search characters to replace other characters |
Like s/love/&/,love this into love |
\< |
Anchoring the beginning of a word |
For example: ' \<grep ' matches a line containing a word that begins with grep |
\> |
Anchoring the end of a word |
For example: ' grep\> ' matches lines that contain words ending with grep |
X{M} |
Consecutive repeating characters x,m times |
For example: ' O{5} ' matches rows containing 5 consecutive O |
X{m,} |
Continuous repetition of character X, at least m times |
For example: ' O{5,} ' matches rows with at least 5 consecutive O |
X{m,n} |
Repeat character X, at least m times, no more than n times |
such as: ' o{5,10} ' matches rows of 5-10 consecutive O |
[:d Igit:] |
All numbers, equivalent to 0-9 |
For example: [0-9]---> [[:d igit:]] |
[: Lower:] |
All lowercase Letters |
such as: [A-z]---> [[: Lower:]] |
[: Upper:] |
All uppercase letters. |
such as: [A-z]---> [[: Upper:]] |
[: Alpha:] |
All the letters |
For example: [a-za-z]---> [[: Alpha:]] |
[: Alnum:] |
Non-special symbols, equivalent to 0-9a-za-z |
For example: [0-9a-za-z]---> [[: Alnum:]] |
[: Space:] |
White space characters |
such as: []---> [[: Space:]] |
[:p UNCT:] |
All punctuation |
For example: [^0-9a-za-z]---> [[:p unct:]] |
Note: Extended regular expressions must be combined with the-r option
常用:命令 sed 替换用法举例
source file
Example:
Serial Number |
instruction |
Description |
① |
Sed-i '/news/c HAPPY ' bak.txt |
Line that matches the news character, replaced with HAPPY |
Ii |
Sed-i ' s#static#stop# ' bak.txt |
Replace the static character with the stop character |
③ |
Sed-i '/server/s#68\.1#76\.3# ' bak.txt |
Match server word lines, replace 68.1 with 76.3 because the dot has a special meaning all need to be escaped |
④ |
Sed-i ' s#lp#lp#2g ' bak.txt |
Replace the next and subsequent on characters in all matching rows with ysg characters |
⑤ |
Sed-i ' s#root#tom#2p ' bak.txt |
Replace all occurrences of the second occurrence of the row in the character bin with the Tom character, and reproduce the same row as the matching row |
⑥ |
Sed-i ' s#games#games#2 ' bak.txt |
Replace all rows that match the character usr and the third occurrence of usr with the USR character |
⑦ |
Sed-i '/sys/{s#/dev#/log#;s#3#6#g} ' bak.txt |
Match the line of the character Sys, replace the character Dev with the log character, and replace 3 with 6 |
⑧ |
Sed-i ' s#nologin# (&) #g ' Bak.txt |
Enclose nologin in parentheses,& to refer to previously matched characters |
⑨ |
Sed-i ' s/sys:x/#&/g ' bak.txt |
Matches the character Sys:x line before adding the # number |
⑩ |
Sed-i '/tcp/s#;# #g ' bak.txt |
Matches the line of the character TCP, replaces the semicolon with empty, and can also be used to annotate the # number |
After the change
常用:命令 sed 简单变量举例
Reference variable
Note: When the SED command also has a default variable, a syntax error occurs when referring to a variable that you define
Note: When there is no default variable in the SED command, you can change the single quotation mark to double quotation marks when there are default variables in the SED command, the variables that you define need to be enclosed in single quotes, and the statements inside the SED must be quoted
进阶:命令 sed 的高级使用
Case One
The content line that is being manipulated is written to the file
Specify line number Add content
Case Two
Get Domain name information
source files and Result styles
Cat yuming.txt|sed ' s#http:\/\/##;s#\/.*## ' |sort|uniq-c|sort-rn
awk-f/' {print $} ' Yuming.txt|sort-r|uniq-c|awk ' {print ' \ t ', $ $ '
命令参数用法举例
Case One
Parameter-n Example
Note: When you do not add-N, the retrieved rows are repeated two times because SED prints all the rows in the file by default
Note: When using multiple sed edit commands, you need to separate {} with a semicolon
Case Two
Action parameter-s example
Add a book character at the end of a matching line
Add Ysg before the is character
Action parameter-i example
Action parameter-a example
Action parameter-d example
Note: When the-I parameter is not applied, the original text is not modified and the text is written and modified when the-I parameter is used, and the parameter-e means that the SED action is edited directly on the command-line mode, which is the default option
Case Three
An example of a regular expression for SED
Example |
Description |
Sed ' 5 q '/etc/passwd |
Print Top 5 lines |
Sed-n ' 1,3p '/etc/passwd |
Print 1-3 lines |
Sed-n '/r*t/p '/etc/passwd |
Print match R has 0 or more lines followed by a T character |
Sed-n '/.r.*/p '/etc/passwd |
Print matches a line with R and R followed by any character |
Sed-n '/o*/p '/etc/passwd |
Print o character repeats any time |
Sed-n '/o\{1,\}/p '/etc/passwd |
Print o repeated occurrences of the word more than once |
Sed-n '/o\{1,3\}/p '/etc/passwd |
Print o Word repeats once to three times between |
Sed-n '/^#/!p '/etc/vsftpd/vsftpd.conf |
Prints lines beginning with #, reversed, and the effect of filtering annotations has been reached |
Sed-n '/^#/! {/^$/!p} '/etc/vsftpd/vsftpd.conf |
Matches lines that begin with #, reverse, and then reverse the line that begins with a space in its result, representing the filter spaces and annotations |
Sed-e '/^#/d '-e '/^$/d '/etc/vsftpd/vsftpd.conf |
Delete matching empty cells lines that begin with the # sign, SED supports multi-conditional operation on individual files |
Linux Common Commands--sed