Basic usage of SED
Send: Line Editor (Full Screen Editor: VI)
Sed mode space
By default, the source file is not edited, and only the data in the mode space is processed. After the processing, the screen of the mode space is printed.
Sed [Options] 'address' file...
-N: silent mode. Unmatched rows are no longer printed.
-I: directly modify the source file. Check whether the modification is correct before use.
-E script: You can execute multiple operations at the same time.
-F/path/sed_script:
-R: indicates using an extended regular expression.
Address
1. Startline endline
For example, 1,100
$: Last line
2./Regexp/
/^ Root/
3./pattern1/,/pattern2/
The first row matched by pattern1 starts and ends with the first row matched by pattern2.
4. linenumber
Specify row
5. Startline, + n
Start from Startline, N rows backward
Command:
D: delete rows that meet the criteria: sed '1, 2D '/etc/fstab delete 1 2 rows
P: Display qualified rows: sed '/root/P'/etc/fstab print rows containing Root
A \ string: append a new row to the specified row with the content of string
sed ‘/^\//a \#hello world \n#hello world‘ /etc/fstab
I \ string: Add a new line before the specified row with the content of string
R file: add the content of the specified file to a qualified row.
sed ‘2r /etc/issue‘ /etc/fstab #
W file: Save the content within the specified range to another file.
sed ‘/oot/w /tmp/oot.txt‘ /etc/fstab
S/Patten/string/modifier: search for and replace. By default, the string first matched by the pattern in each line
Modifier
G: Global replacement
I: case insensitive
sed ‘s/oot/OOT/‘ /etc/fstab
&: The reference mode matches the entire string.
This article from the "openvpn building" blog, please be sure to keep this source http://lovesource.blog.51cto.com/1454821/1569790
Sed command details