多数のHTMLファイルで文字列を見つけて置き換える最も簡単な方法

多数のHTMLファイルで文字列を見つけて置き換える最も簡単な方法

私は多くのHTMLファイルを持っていますが、すべて1つのフォルダ全体に含まれる複数のフォルダに入れ子になっています。各HTMLファイルで置き換える必要があります。

/contact/index.html

そして

/contact/index.php

コマンドラインでこれを簡単に行う方法はありますか?

ベストアンサー1

findはい、GNUとGNUがある場合は、sed親フォルダで次を試してください。

find . -type f \( -iname "*.htm" -o -iname "*.html" \) -exec sed -i.bak 's#/contact/index\.html#/contact/index.php#' '{}' +

.html名前がor.HTMLまたは.htmor .HTM(または.HtM...)で終わるすべてのファイルを見つけて、sed次のコマンドを実行します。

sed -i.bak 's#/contact/index\.html#/contact/index.php#g'

foo.htmこれにより、必要な交換が行われ、名前付き生のバックアップが作成されますfoo.htm.bak。バックアップが不要な場合は削除してください.bak


詳細:

find明らかに、このコマンドはファイルまたはフォルダを探します。これらの構文は非常に複雑になる可能性があり、man page以下のいくつかの構文で詳しく説明されています。

一般的な形式はですfind [where] [what]。上記の例ではwhere.現在のディレクトリを示しています。これは拡張子が類似または類似したすべてのファイルであるwhatため、次のものを使用しています。htmliname

   -iname pattern
          Like -name, but the match is case insensitive.
          For example,  the  patterns  `fo*'  and  `F??'
          match  the  file  names  `Foo',  `FOO', `foo',
          `fOo', etc.   

しかし、私は両方とも一致したいので、対応するhtmlフラグをhtm使います-o。これは次のことを意味します。

  expr1 -o expr2
          Or; expr2 is not evaluated if expr1 is true.

これらの構造は括弧でグループ化する必要があります( )。しかし、以下が必要です。脱出する私たちが使用するシェルから\(\)

魔法は次の-exec部分で起こります。

   -exec command ;
          Execute command; true if 0 status is returned.
          All following arguments to find are  taken  to
          be  arguments to the command until an argument
          consisting of `;' is encountered.  The  string
          `{}'  is  replaced  by  the  current file name
          being processed everywhere it  occurs  in  the
          arguments  to  the  command, not just in argu‐
          ments where it is alone, as in  some  versions
          of  find.   [...] The specified command is
          run once for each matched file.   The  command
          is executed in the starting directory.   There
          are unavoidable security problems  surrounding
          use  of  the  -exec action; you should use the
          -execdir option instead.

つまり、このようなコマンドが与えられたら、-exec ls {}設定findした基準に一致するすべてのファイルを見つけて繰り返し、{}現在のファイル名に置き換えて、指定されたコマンドを実行します。また、end callの+代わりに使用します。これにより、できるだけ少ないコマンドを実行しようとします。これは何千ものファイルがない限り小さな最適化にすぎず、これは重要です。\;execfind

   -exec command {} +
          This variant of  the  -exec  action  runs  the
          specified  command  on the selected files, but
          the command line is built  by  appending  each
          selected  file name at the end; the total num‐
          ber of invocations of the command will be much
          less  than  the  number of matched files.  The
          command line is built in  much  the  same  way
          that xargs builds its command lines.  Only one
          instance of `{}' is allowed  within  the  com‐
          mand.  The command is executed in the starting
          directory.

最後に、sedファイルの各行に指定したコマンドを適用するコマンドラインテキストストリームエディタがあります。この場合、コマンドは置き換えられ、デフォルトの形式は次のとおりです。

s#pattern#replacement#flags

区切り文字(#)は任意の特殊文字であり、伝統的にはそうです。それ以外の場合はエスケープする必要があるため、ChrisDownは彼の答えに使用することを選択しました/。これは両方とも同じです。#/|

おすすめ記事