“sed (stream editor, stream editor) is a very powerful non-interactive text editor under linux. Its main function is to perform matching operations on each line of data in the text, such as matching text content by line, by field, and by regular pattern. Then add, delete, modify and check operations.
”
sed principle
When sed processes text, it first stores the currently processed line in a temporary buffer, then uses the sed command to process the content in the buffer, and after the processing is completed, the content of the buffer is sent to the screen. Then process the next line, and repeat until the end of the file.
sed syntax
sed [选项参数] ‘command’ filename
Description of Common Parameters
option parameter | Function |
---|---|
-e |
-e is only required for multiple sed commands; |
-i |
Modify the content directly. When -i is not added, the default is only a preview, and no actual modification will be made to the file |
-f |
followed by the file in which the sed instructions are saved |
-n |
Cancel the default output, sed will output all text content by default, and only display the processed lines after using the -n parameter |
-r ruguler |
Use extended regular expressions, by default sed only recognizes basic regular expressions * |
sed program command
replacement syntax
s/xx/yy/g
s/xx/yy/
+g : Match all characters from the beginning to the end of each line
Without g: start matching at the beginning of each line, match to the first matching field, it will end and skip to the next line
Multiple sed commands use
将sed.txt文件中的第1行删除并将 `abc` 替换为 `xyz`
sed -e '1d' -e 's/acc/xyz/g' sed.txt
Common operation cases of sed
Awk of the Linux Three Musketeers, a powerful tool that Linux must learn!
Common network related commands under linux
Linux must learn file and content search commands