About SED
Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on. The GNU version of SED 3.02 is described below.
-i option: Direct Action source file, source file will be modified.
sed commands and options:
A\ |
Add one or more rows after the current line |
C\ |
Replace text in the current line with new text |
D |
Delete Row |
I\ |
Insert text before the current line |
H |
Copy the contents of the schema space to the staging buffer |
H |
Add the contents of the schema space to the buffer |
G |
Remove the contents of the staging buffer and copy it to the pattern buffer |
G |
Remove the contents of the staging buffer and append it to the pattern buffer |
L |
List non-printable characters |
P |
Print Line |
N |
Reads the next line of input and processes it from the next rather than the first command |
Q |
End or exit sed |
R |
Reading input rows from a file |
! |
Apply all commands to a row other than the selected row |
S |
Replace another string with one string |
Replace flag:
G |
Global substitution within a row |
P |
Print Line |
W |
Writing rows to a file |
X |
Swapping the contents of a staging buffer and pattern space |
Y |
Convert a character to another character |
Examples of sed:
Print: P command
Sed '/abc/p ' file |
Prints the line containing ABC in file. By default, sed prints all rows to the screen, and if a row matches the pattern, print the line again |
Sed-n '/abc/p ' file |
Just like above, only the default behavior of SED is removed, and only matching rows are printed |
Delete: D command
Sed ' 3, $d ' file |
Deletes the contents from line 3rd to the last row. |
Sed ' $d ' file |
Delete the contents of the last row |
Sed '/abc/d ' |
Delete the line that contains the ABC. |
Sed ' 3d ' file |
Delete the contents of the third row |
Replace: s command
Sed ' s/abc/def/g ' file |
Replace all ABC in the line with Def, and if there is no G, replace only the first ABC in the row |
Sed-n ' s/abc/def/p ' file |
Print only those rows where the substitution occurred |
Sed ' s/abc/&def/' file |
Add Def (& to match content) behind all ABC |
Sed-n ' s/abc/def/gp ' file |
Replace all ABC with DEF and print the lines where the substitution occurred |
Sed ' s#abc#def#g ' file |
Replace all ABC with DEF, followed by the character behind S to find the string and Replace the split character between the strings, in this case try # |
Specify the range of rows: comma
Sed-n '/abc/,/def/p ' file |
Print mode ABC to DEF line |
Sed-n ' 5/,/def/p ' file |
Prints the line from line fifth to the containing Def line. |
sed/abd/,/def/s/aaa/bbb/g |
Modify the line between pattern ABC and mode DEF to replace AAA with Def |
Multiple edits-E
Sed-e ' 1,3d '-e ' s/abc/def/g ' file |
Delete 1-3 lines and replace the remaining lines of ABC with DEF |
Read file: R command
Sed '/abc/r newfile ' file |
Read the contents of the NewFile after the line containing ABC |
Write file: w command
Sed '/abc/w newfile ' file |
Write newfile on lines that contain ABC |
Append: a command
Sed '/abc/a\def ' file |
A new line after the line containing ABC, written to Def |
insert: I command
Sed '/abc/i\def ' file |
A new line before the line containing ABC, written to Def |
Modify: C command
Sed '/abc/c\def ' file |
Replace the line containing ABC with DEF and the old text is overwritten |
Read Next line: N command
Sed '/abc/{n; s/aaa/bbb/g;} ' file |
Reads the next line of lines containing ABC, replacing AAA for BBB |
convert: Y command
Sed ' y/abc/abc ' file |
Replace A with a, a, a, a, or a b,c with a C (regular expression metacharacters do not work) |
exit: Q command
Sed '/abc/{s/aaa/bbb/; q;} ' file |
Include ABC in a line, replace AAA with BBB, and then exit sed. |
Staging and fetching: H Command (store pattern rows to staging buffer) and g (remove row from staging buffer and overwrite pattern buffer) g (row to remove temporary buffer) command
H and G are replication behaviors (Overrides), and H and G represent append.
Sed-e '/abc/h '-e ' $G ' file |
Rows containing ABC are saved to the staging buffer by the H command, and in the second command sink, when Sed reads the last line of $, the G command reads a row from the staging buffer, appended to the pattern buffer. That is, the last line of all lines that contain ABC is copied to the end of the file. |
Sed-e '/abc/{h; D;} ' -E '/def/{g;} ' file |
Rows containing the ABC are moved to the line containing Def and overwritten. |
Staging and interchange: H and X Commands
Sed-e '/abc/h ' -E '/def/x ' file |
Lines that contain ABC are converted to def rows. |
Shell sed Command