sed's/this/that /' - 忽略g但仍然替换整个文件

web*_*org 5 shell sed

正如标题所说,我试图只改变第一次出现的单词.通过使用sed's/this/that /'file.txt

虽然我没有使用g选项,但它会替换整个文件.如何解决这个问题.

更新:

$ cat file.txt 
  first line
  this 
  this 
  this
  this
$ sed -e '1s/this/that/;t' file.txt 
  first line
  this  // ------> I want to change only this "this" to "that" :)
  this 
  this
  this
Run Code Online (Sandbox Code Playgroud)

zaf*_*zaf 6

http://www.faqs.org/faqs/editor-faq/sed/

4.3.如何仅更改模式的第一次出现?

sed -e '1s/LHS/RHS/;t' -e '1,/LHS/s//RHS/'
Run Code Online (Sandbox Code Playgroud)

其中LHS = this,RHS =你的例子.

如果您知道第一行上不会出现模式,请省略第一行-e和后面的语句.