1.Sed 初識

Sed 指的是 Stream Editor

SED can be used in many different ways, such as:

  • Text substitution, 文字替換
  • Selective printing of text files, 選擇性答應text 文檔
  • In-a-place editing of text files, 編輯文檔
  • Non-interactive editing of text files, and many more.非交互編輯文件

SED 的原理

image

  • READ 從input stream 讀取一行
  • EXECUTE 在一行上 執行sed 命令
  • display 顯示執行結果到 output stream

Read: SED reads a line from the input stream (file, pipe, or stdin) and stores it in its internal buffer called pattern buffer.

Execute: All SED commands are applied sequentially on the pattern buffer. By default, SED commands are applied on all lines (globally) unless line addressing is specified.

Display: Send the (modified) contents to the output stream. After sending the data, the pattern buffer will be empty.

The above process repeats until the file is exhausted.

重點

Points to Note
Pattern buffer is a private, in-memory, volatile storage area used by the SED.

By default, all SED commands are applied on the pattern buffer, hence the input file remains unchanged. GNU SED provides a way to modify the input file in-a-place. We will explore about it in later sections.

There is another memory area called hold buffer which is also private, in- memory, volatile storage area. Data can be stored in a hold buffer for later retrieval. At the end of each cycle, SED removes the contents of the pattern buffer but the contents of the hold buffer remains persistent between SED cycles. However SED commands cannot be directly executed on hold buffer, hence SED allows data movement between the hold buffer and the pattern buffer.

Initially both pattern and hold buffers are empty.

If no input files are provided, then SED accepts input from the standard input stream (stdin).

If address range is not provided by default, then SED operates on each line.

PATTERN BUFFER HOLD BUFFER 的區別
初識都爲空
直接執行的 每一行都會重新覆蓋 是sed 直接操作的內存區域 不會被覆蓋
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章