The format of the SED command line is as follows:
[Root @ localhost ~] # Sed [-nefri] command input text
Common options:
-N: Use silent mode. In general sed usage, all information from stdin is usually listed on the screen. However, if the-n parameter is added, only the row (or action) that has been specially processed by SED will be listed.
-E: directly edit the SED action in the Command column mode;
-F: Write the SED action directly in a file.-f filename can execute the SED action in filename;
-R: sed supports the syntax of extended regular notation. (The default is the basic regular expression syntax)
-I: directly modify the content of the file to be read, rather than output by the screen.
Common commands:
A: new. A can be followed by strings. These strings will appear in a new row (the next row currently )~
C: replace. C can be followed by strings. These strings can replace rows between N1 and N2!
D: delete. Because it is deleted, D is usually not followed by any comment;
I: insert, I can be followed by strings, and these strings will appear in the new line (the previous line currently );
P: print the selected data. Usually P will work with the sed-N parameter ~
S: replace, you can directly replace the work! Generally, this s action can be combined with regular notation! For example, 1, 20 s/old/new/g!
Example (Assume we have a file named AB)
Delete a row
[Root @ localhost Ruby ]# Sed '1d 'AB # Delete the first line
[Root @ localhost Ruby] # Sed '$ d' AB # Delete the last row
[Root @ localhost Ruby] # Sed '1, 2D 'AB # Delete the first row to the second row
[Root @ localhost Ruby] # Sed '2, $ d' AB # Delete the second row to the last row
Show a row
. [Root @ localhost Ruby ]# Sed-n'1p' AB # Display the first line
[Root @ localhost Ruby] # Sed-n' $ P' AB # Display the last line
[Root @ localhost Ruby] # Sed-n'1, 2 p 'AB # Display rows 1 to 2
[Root @ localhost Ruby] # Sed-n'2, $ P' AB # Display the second row to the last row
Use mode for query
[Root @ localhost Ruby] # Sed-n'/Ruby/P' AB # Query all rows including the keyword Ruby
[Root @ localhost Ruby] # Sed-n'/\ $/P' AB # Query all rows with the keyword $. Use the backslash \ To block special meanings.
Add one or more strings
[Root @ localhost Ruby] # Cat AB
Hello!
Ruby is me, welcome to my blog.
End
[Root @ localhost Ruby] # Sed '1a drink tea 'AB # Add the string "drink tea" after the first line"
Hello!
Drink tea
Ruby is me, welcome to my blog.
End
[Root @ localhost Ruby] # Sed '1, 3A drink tea 'AB # Add the string "drink tea" after the first row to the third row"
Hello!
Drink tea
Ruby is me, welcome to my blog.
Drink tea
End
Drink tea
[Root @ localhost Ruby] # Sed '1a drink tea\ NOr coffee 'AB # Add multiple lines after the first line and use the linefeed \ n
Hello!
Drink tea
Or coffee
Ruby is me, welcome to my blog.
End
Replace one or more rows
[Root @ localhost Ruby] # Sed '1c Hi' AB # Replace the first line with hi
Hi
Ruby is me, welcome to my blog.
End
[Root @ localhost Ruby] # Sed '1, 2c Hi' AB # Replace the first and second rows with hi
Hi
End
Insert
[Root @ localhost Ruby] # Sed-I '$ A bye' AB # Enter "bye" in the last line of file AB"
[Root @ localhost Ruby] # Cat AB
Hello!
Ruby is me, welcome to my blog.
End
Bye