Sed
Sed:stream Editor, line editors
Format:
sed [option] ' [address] [Edit command] '
Common options:
-n,--quiet: Do not output the contents of the mode to the screen
-e: Multi-point editing
-F:/path/to/script_file: Read script from the specified file
-R: Support for extended regular expressions
-I: Modifying source files
To develop the world:
(1) Not to address: The full text of the processing;
(2) Single address:
#: The specified row
/pattern/: All rows for pattern matching
(3) Address range:
#,# OK row to OK row
#,+# the start of the line from the left #, plus the number of rows on the right #;
$: Last line
/pat1/,/pat2/mode line to pattern row (first match)
#,/pat1/determining the line-to-first match pattern
(4) ~: Step forward
For example, 2~2
Edit command:
# #模式空间就是匹配到的行
D: Delete compliant lines
P: Print conforming line (d and-n ' p ' opposite effect)
A \text: Append text at the back of the line, enabling multiple row append using \ n
I \text: inserting text in front of line: Support for multi-row insertion with \ n
C \text: Replace a line of behavior or multiple lines (\ n) text
W/path/to/somefile: Save the contents of the mode space to the specified file
R/path/from/somefile: reads the text stream from the specified file into the matching pattern
=: Prints line numbers for lines in pattern space;
! : Reverse condition (for address range)
S//: Replace, support using other separators, [email protected]@@,s###
s/what to look for/replace with content/modifiers
Modifier:
P: Show the rows that were successfully replaced
G: global substitution; By default, each line replaces only the first time
&: Back reference
W:/path/to/somefile to save the successful result to the specified file
Exercise 1: Delete all whitespace characters in the/boot/grub/grub.conf file that begin with whitespace at the beginning of the line;
~]# sed ' [email protected]^[[:space:]]\[email protected]@ '/etc/grub2.cfg
Exercise 2: Remove all # and white space characters from the beginning of the line at the beginning of the/etc/fstab file with #, followed by at least one white space character;
~]# sed ' [email protected]^#[[:space:]]\[email protected]@ '/etc/fstab
Practice 3:echo an absolute path to the SED command, take out its base name, and take out its directory name;
~]# echo "/etc/sysconfig/" | Sed ' [Email protected][^/]\+/\[email protected]@ '
Advanced Editing Commands:
H: Overwrite the contents of the pattern space in the hold space
H: Append the contents of the pattern space to the hold space
G: Remove data coverage mode space from Hold space
G: Remove data Append mode space from Hold space
x: Pattern space and preserve space content interchange
N: The cursor that reads the next line of the matching row to overwrite the pattern space file also moves with it
N: The cursor that reads the next line of the matching line to the pattern space file will also move with it
# #如, N reads the first line immediately after the second line to process, processing completed the cursor moved to the third row,
D: Delete rows in the pattern space
D: Delete all rows in multi-line mode space
Linux System Management-(--sed) command