`sed -i.bak' /^x /d' "$SOME_FILE"` の私の解釈は正しいですか?

`sed -i.bak' /^x /d'

いつも恐ろしい理由のため、sed構文が少し変なようです。次の内容が何であるかをよく理解したいと思います。

sed -i.bak '/^x /d' "$SOME_FILE"
  1. $SOME_FILEこれにより、まず${SOME_FILE}.bak()のバックアップが作成されます-i.bak
  2. $SOME_FILE正規表現「」に一致するすべての行^x(つまり、x「」で始まり、後にスペースが続く行)が削除されます。

ベストアンサー1

はい、あなたの理解は正しいです。あなたの形式で判断すると、あなたはGNUを使用していると仮定します(他の実装では、間にsedスペースが必要になる場合があり、一部はまったくサポートされていない可能性があります)。仕組みは次のとおりです。-i.bak-i-iinfo 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.

このdコマンドは、前の式が成功したすべての行を削除します。厳密に言えば、「パターンスペース」を削除しますが、単純なsedスクリプトではそれを減らします。

おすすめ記事