すべてを | |
▼開く |
閉じる▲ |
$ cat desc.txt Sed is a streaming editor that is used to perform basic text transformations on an input streaming (a file or input from a pipeline).
$ sed 's/streaming/stream/' < desc.txt Sed is a stream editor that is used to perform basic text transformations on an input streaming (a file or input from a pipeline).
sedコマンドで、置換文字列を「's/streaming/stream/'」、ファイル「desc.txt」をリダイレクトでコマンドに与えると、各行で最初に見つかった文字列「streaming」を「stream」に変換する。同じ行の中にある2つ目の「streaming」は置換されない。
$ cat desc.txt | sed 's/streaming/stream/'
catコマンドの出力をパイプでsedに送っても同様の結果を得られる。
置換文字列の指定方法 |
---|
's/置換する文字列/新しい文字列/' |
正規表現などがシェルに解釈されないように引用符「'」で囲む
trコマンドに置換を指示する「s」
「/」に続いて置換する文字列を指定
「/」に続いて新しい文字列を指定
「/」で閉じる。
各行が短く改行されている場合、結果としてすべて置換される場合がある。
catコマンドで編集するファイルを見る$ cat desc.txt Sed is a streaming editor that is used to perform basic text transformations on an input streaming (a file or input from a pipeline).
単語を1つだけ置換するつもりでコマンドを実行しても、行が短いために結果的にすべての文字列が変換されてしまう。
sedコマンドで置換パターンを指示し、ファイルを与える$ sed 's/streaming/stream/' < desc.txt Sed is a stream editor that is used to perform basic text transformations on an input stream (a file or input from a pipeline).
Copyright iDesign Inc., 2005-2012