SED basic usage:
Sed:stream Editor Editor, unlike the VI editor, the VI editor is a full-screen editor.
SED: Mode space
By default, the original file is not edited, only the data in the pattern space is processed, and after the processing is finished, the pattern space is printed to the screen;
sed [options] ' addresscommand ' file ...
-N: Silent mode, content not in the default display mode space
-I: Modify the original file directly
-E script-e script: Multiple scripts can be executed at the same time
-f/path/to/sed_script
Sed-f/path/to/scripts File
-R indicates the use of extended regular expressions
Notes on Address:
Startline,endline such as 1,100 $: last line
/regexp//^root/
/pattern1/,/pattern2/the first line that is matched by pattern1 to the end of the line that was first matched to the pattern2, all rows in the middle
LineNumber: The specified row
Startline,+n starts from StartLine and is backward N rows.
A description of the command:
D: Delete rows that match the criteria
P: Show rows that match the criteria
A \string: Appends a new line after the specified line, with the contents of String
I \string: Adds a new row before the specified line, with the contents of String
R file: Adds the contents of the specified file to the qualifying line
W File: Save the row in the range specified by the address as the specified file
s/pattern/string/modifier: Find and Replace, default replaces only the first string in each line that is matched to the pattern
Modifier: G: Global substitution I: ignore character case
In fact S///,s###,[email protected]@@ 都 the same, \ (\), \1,\2
&: The reference pattern matches the entire string.
Practice:
Remove whitespace from the beginning of the/etc/grub.conf file
Sed-r ' s/^[[:space:]]+//g '/etc/grub.conf
Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5
Sed-r ' S#^\ (id:\): [[:d igit:]]\ (: initdefault:\) #\15\2#g '
Delete blank lines in the/etc/inittab file
Sed-r '/^$/d '/etc/inittab
Delete the # number at the beginning of the/etc/inittab file
Sed-r ' s/^#//g '/etc/inittab
Delete the # and trailing white space characters at the beginning of a file (/etc/inittab), but require that the # sign must have a space character after it
Sed-r ' s/^#[[:space:]]+//g '/etc/inittab
Remove a directory name for a file path
Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '
Remove the file name of a file path
Sed-r ' [Email protected]^/.*/([^/]+)/[email Protected]\[email protected] '
sed command in Linux