Linux text processing Three Musketeers sed addition, deletion, modification and query

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 command76b3f44b8e1c38d129dc5536cb7e07e1.png

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

424ec4b46a7a9b1a3567af2655efd08d.png

Awk of the Linux Three Musketeers, a powerful tool that Linux must learn!

2021-12-21

36689d2039a6a368c8f8d430fe69c2a5.png

Common network related commands under linux

2021-08-11

23787d38978ebbd75b85895d4766a4a8.png

Linux must learn file and content search commands

2021-07-20

f48587f587e32802acab929f91d68f92.png

Guess you like

Origin blog.csdn.net/XingLongSKY/article/details/122119607