我对 `sed -i.bak '/^x /d' "$SOME_FILE"` 的解释是否正确?

Cap*_*Man 9 sed

出于某种原因sed,它总是让我感到害怕,我想它的语法有点奇怪。我想确保我了解以下内容。

sed -i.bak '/^x /d' "$SOME_FILE"
Run Code Online (Sandbox Code Playgroud)
  1. 这将首先备份$SOME_FILEat ${SOME_FILE}.bak( -i.bak)。
  2. $SOME_FILE匹配正则表达式“ ^x”的每一行(意思是以“ x”开头,后跟一个空格的行)将被删除。

ter*_*don 10

是的,你的理解是正确的。根据您的格式,我假设您使用的是 GNU sed(其他实现可能需要在-i和之间有一个空格.bak,有些可能根本不支持-i)。它的-i工作原理如下(来自info sed):

-i[SUFFIX]
--in-place[=SUFFIX]
     This option specifies that files are to be edited in-place.  GNU
     `sed' does this by creating a temporary file and sending output to
     this file rather than to the standard output.(1).

 This option implies `-s'.

 When the end of the file is reached, the temporary file is renamed
 to the output file's original name.  The extension, if supplied,
 is used to modify the name of the old file before renaming the
 temporary file, thereby making a backup copy(2)).

 This rule is followed: if the extension doesn't contain a `*',
 then it is appended to the end of the current filename as a
 suffix; if the extension does contain one or more `*' characters,
 then _each_ asterisk is replaced with the current filename.  This
 allows you to add a prefix to the backup file, instead of (or in
 addition to) a suffix, or even to place backup copies of the
 original files into another directory (provided the directory
 already exists).

 If no extension is supplied, the original file is overwritten
 without making a backup.
Run Code Online (Sandbox Code Playgroud)

d命令删除前一个表达式成功的任何行。严格来说,它删除了“模式空间”,但在简单的sed脚本中就是行。