Basic operation of sed editor

sed editor

sed editor

  • Sed is a stream editor, the stream editor will edit the data stream based on a set of rules provided in advance before the editor processes the data.
  • The sed editor can process the data in the data stream according to commands, which are either input from the command line or stored in a command text file.
    The workflow of
    sed The workflow of sed mainly includes three processes of reading, executing and displaying:
  • Read: sed reads a line of content from the input stream (file, pipe, standard input) and stores it in a temporary buffer (also known as pattern space).
  • Execution: By default, all sed commands are executed sequentially in the pattern space. Unless the address of the line is specified, the sed command will be executed sequentially on all lines.
  • Display: Send the modified content to the output stream. After sending the data, the pattern space will be cleared. Before all file contents are processed, the above process will be repeated until all contents are processed.
    Before all file contents are processed, the above process will be repeated until all contents are processed.
    Note: By default, all sed commands are executed in the pattern space, so the input file will not change in any way, unless redirection is used to store the output.

Command format:

sed -e'操作'文件1文件2 ...
sed -n -e'操作'文件1文件2 ...
sed -f 脚本文件 文件1 文件2 ...
sed -e  '操作'文件1 文件2 ...

sed -e 'n {
操作1
操作2
...
}' 文件1 文件2 ...

Common options:

-e或--expression=:表示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一般在执行
多个操作命令使用
-f 或--file=:表示用指定的脚本文件来处理输入的文本文件。
-h或--help:显示帮助。
-n、--quiet或silent:禁止sed编辑器输出,但可以与p命令一起使用完成输出。-i:直接修改目标文本文件。

Common operations:

s:替换,替换指定字符。d:删除,删除选定的行。
a:增加,在当前行下面增加一行指定内容。i:插入,在选定行上面插入一行指定内容。c:替换,将选定行替换为指定内容。
y:字符转换,转换前后的字符长度必须相同。
p:打印,如果同时指定行,表示打印指定行:如果不指定行,则表示打印所有内容:如果有非打印字符,
则以ASCII
码输出。其通常与"-n"选项一起使用。
=:打印行号。
1(小写L):打印数据流中的文本和不可打印的AsCII字符(比如结束符$、制表符\t)

Guess you like

Origin blog.csdn.net/weixin_51614581/article/details/111660726