sed [OPTION] ... ' Script ' [Input-file] ...
Script
Address Delimitation Edit Command
Common options:
-N: does not output the contents of the mode space to the screen;
-e script,--expression=script: multi-point editing;
-f/path/to/sed_script_file
One edit command per line;
-R,--regexp-extended: supports the use of extended regular expressions;
-i[suffix],--in-place[=suffix]: Directly edit the original file;
~]# sed-e ' [email protected]^#[[:space:]]*@@ '-e '/^uuid/d '/etc/fstab
Address delimitation:
(1) Empty address: To deal with the full text;
(2) Single address:
#: Specify line;
/pattern/: Each row that is matched by this pattern;
(3) Address range
#,#:
#,+#:
#,/pat1/
/pat1/,/pat2/
$: last line;
(4) Step forward: ~
: All Odd lines
2~2: All even lines
Edit command:
D: Delete;
P: Displays the contents of the mode space;
A \text: Append the text "text" After the line, support using \ n to implement multi-row append;
I \text: Insert text "text" in front of line, support using \ n to implement multiline insertion;
C \text: Replace the matched line with the text "text" specified here;
W/path/to/somefile: Saves the line of pattern space to the specified file;
R/path/from/somefile: Reads the contents of the specified file to the current file by the pattern to match the line, file merge;
=: Prints the line number for the line to which the pattern matches;
!: conditional inversion;
Address delimitation! Edit command;
s///: Find replacement, its delimiter can be self-specified, commonly used have [email protected]@@, s## #等;
Replace tag:
G: global substitution;
W/path/to/somefile: Saves the result of the substitution success to the specified file;
P: Shows the successful replacement line;
Exercise 1: Remove all white-space characters from the beginning of all lines in the/boot/grub/grub2.cfg file that begin with a blank character;
~]# sed ' [email protected]^[[:space:]]\[email protected]@ '/etc/grub2.cfg
Exercise 2: Remove all white-space characters from the beginning of the line at the beginning of the lines in the/etc/fstab file, preceded by #, and #;
~]# sed ' [email protected]^#[[:space:]]*@@ '/etc/fstab
Exercise 3: Output an absolute path to the SED command and take out its directory, which behaves like dirname;
~]# echo "/var/log/messages/" | Sed ' [Email protected][^/]\+/\[email protected]@ '
~]# echo "/var/log/messages" | Sed-r ' [Email protected][^/]+/[email protected]@ '
Advanced Editing Commands:
H: The content of the pattern space is covered in the holding space;
H: Append the contents of the pattern space to the holding space;
G: To cover the contents of the holding space in the pattern space;
G: Append the contents of the holding space to the pattern space;
x: Swap the content in the pattern space with the content in the hold space;
N: Overwrites the next line of the row to the pattern space in the read match;
N: Append the next line of the row to the pattern space to read;
D: Delete rows in the pattern space;
D: Delete all rows in multi-line mode space;
Example:
Sed-n ' n;p ' FILE: shows even rows;
Sed ' 1! G;h;$!d ' file: Displays the contents of the files in reverse order;
Sed ' $!d ' FILE: Take out the last line;
Sed ' $! n;$! D ' file: Two lines after removing the file;
Sed '/^$/d; G ' FILE: Delete all of the original blank lines, and then add a blank line after all the non-blank lines;
Sed ' n;d ' FILE: Displays odd lines;
Sed ' G ' FILE: Adds a blank line behind the original line;
This article is from the "11284919" blog, please be sure to keep this source http://11294919.blog.51cto.com/11284919/1783193
Linux Text Management tool SED