Sed commands are commonly used in Linux or Shell programming to filter and replace commands, and if you are proficient in using SED, it can be very helpful for people who use it frequently.
The main usage of SED is listed below (you can correct it in the wrong place):
P command
Print only the third row, no-n will print the third line after the third line:
Sed-n ' 3p ' filename
Print only the last line:
Sed-n ' $p ' filename
Prints only 10 to 20 rows (including 10 and 20 lines, and if the subsequent number is less than the preceding number, prints only the lines of the preceding numbers):
Sed-n ' 10,20p ' filename
Print the lines that contain my line endings to the lines that contain you:
Sed-n '/my$/,/you/p ' filename
Print the matching line and the next 5 lines:
Sed-n '/my/,+5p ' datafile
Print the matching line and the line with the following line number 5 until the end:
Sed-n '/my/,~5p ' datafile
Print 5 times times the number of lines:
Sed-n ' 0~5p ' file
Sed ' 0~5!d ' file
----------------------------------------------------------
P command
The contents of the first line break in print mode space
In fact, it's the odd print.
---------------------------------------------------------
d command
Delete line fifth and print another line:
Sed ' 5d ' filename
Delete the line containing my to the line that contains you, not print:
Sed-n '/my/,/you/d ' filename
Delete the line that contains my to line eighth and do not print another line:
Sed-n '/my/,8d ' filename
---------------------------------------------------------
d command
Delete the contents of the first line break in a pattern space
is to print even more rows.
---------------------------------------------------------
s command
One string to replace another
Sed ' s/^my/you/g ' datafile
g denotes inline global substitution and can also use numeric values instead
3g represents the beginning of a third match to the global substitution
Sed-n ' 1,20s#my$ #You #gp ' datafile
Print rows from 1 to 20 in lines with the end of my
Note: The symbol immediately following S is a delimiter, except for line break backslash
Sed ' s/^my/you/g;s/are/are/g ' datafile
Replace multiple
Sed '/abc/!s/^my/you/g;s/are/are/g ' datafile
Replace lines that do not contain ABC
---------------------------------------------------------
R command
Sed '/^my/r datafile1 ' datafile2
Insert all the contents of Datafile1 after the line that starts with my in Datafile2
---------------------------------------------------------
W command
Sed '/^my/w datafile1 ' datafile2
Writes lines that begin with my in Datafile2 to Datafile1
---------------------------------------------------------
a command
Appended after the found row, with \, directly with the text
Sed '/my/a\hi,world\nhow is you ' datafile
With the R command, just write a paragraph of text
---------------------------------------------------------
I command
Inserts a new text before the current line
Sed '/my/i\hi,world\nhow is you ' datafile
Use the same a\, just insert the text into the front
---------------------------------------------------------
C command
Change the text you've found into a new text
Sed '/my/c\hi,world\nhow is you ' datafile
Usage with a\ and i\
---------------------------------------------------------
n command
Append the next line of input to the mode space to \ n separate
Sed '/my/{n;s/my/your/;} ' datafile
If the last behavior is an odd line, then the n command does not execute
---------------------------------------------------------
n command
Reads the next line of input into the mode space
---------------------------------------------------------
Y command
Sed ' 1,5y/abcd/abcd/' datafile
Convert ABCD to uppercase, same as s command/can change
Usage with tr command
---------------------------------------------------------
Q command
Exit command
Sed '/my/{q;s/my/your/;} ' datafile
Does not execute s/my/your/
---------------------------------------------------------
h/h Command and g/g command
H Clear Buffer contents, save the most recently executed H mode content
H put match to content append save in buffer
G Replace the buffer contents with the schema contents
G Append buffer contents to the Pattern space line
Sed-e '/my/h '-e ' $g ' datafile
Sed-e '/my/h '-e ' $G ' datafile
G: Copy contents of hold space to pattern space, the contents of the original pattern space are cleared
G: After append the contents of hold space to the pattern space\n
H: Copy the contents of the pattern space into hold space, and the contents of the original keep space are erased.
H: Append the contents of the pattern space to the hold space\n
X: Exchanging the contents of pattern space and hold space
---------------------------------------------------------
X command
Exchanging content that preserves space and pattern space
---------------------------------------------------------
l command
Display special characters, do not display garbled
Sed-n ' 1, $l ' F1
---------------------------------------------------------
-F parameter
Sed-n-F Script datafile
---------------------------------------------------------
-I parameter
Directly modify the file, will not print, such as:
Sed-i G datafile
Add a blank line after each line in the DataFile (G)
Sed-i ' s/^my/you/g ' datafile
Replace directly in text
Sed-i '/my/{x;p;x} ' datafile
Add a blank line to the front of my Line
Sed-i ' $! N;/^\ (. *\) \n\1$/! P;d ' datafile
Delete adjacent duplicate rows, leaving only the first row
---------------------------------------------------------
-i.bak
Modify the original file and create a copy of the original file
Sed-i.bak ' s/^my/you/g ' datafile
Modify the datafile and create a Datafile.bak backup
---------------------------------------------------------
-e parameter
Multiple edits in SED commands
Sed-n-E '/my$/'-e ' s/my/you/gp ' datafile
Print the line ending with my, then execute the replacement statement
---------------------------------------------------------
= Command
Print line number:
Sed-n ' = ' F1
$= count Lines of text
Sed-n ' $= ' datafile1 datafile2 ...
---------------------------------------------------------
$! except for the last line
4! In addition to line fourth
Sed ' 4! S/my/your/g ' datafile
Replace all except line fourth
---------------------------------------------------------
b command
tags, you can jump to tag execution, such as
Sed '/^my/byyc;s/^/#/;b;:yyc;s/^my/your/datafile
If it is my beginning, change to your, otherwise the line is preceded by #
---------------------------------------------------------
SED regular expression
\ Do not escape, the meta-character prints as usual
^ starts with a string, such as a line that begins with My/^my/
$ ends with a string, such as a line ending with My/my$/
. Matches a character, such as/M. There are two characters between y/m and Y.
* Match 0 or more of the preceding characters
[] matches the single character inside, such as/[mh]y/matches my and Hy
[^] matches characters not in the inside
such as/[^mh]y/match not my and Hy
\(.. \) Save the characters that have been matched, and use \ n to Exhale
such as 1,20s/\ (you\) self/\1r/with the \1 exhale you,
Save up to 9, Mark from left
& Save a lookup string, such as s/my/you&you/,& for My
\< the first locator, and ^ similar, such as/\ \> ending locators, and $ similar, such as/my\>/
X\{m\} consecutive m x, such as/a\{5\}/find 5 consecutive a
X\{m,\} at least m x, such as/a\{5,\}/at least 5 a
x\{m,n\} at least m x, up to n x
\w matches each letter and number
\w\+ match every word
\s Match each space
\x\+ x represents a character or a word that is manipulated when matched to one or more of the
Use of SED commands in Linux (Absolute Essence Edition)