序章

The sed ストリームエディタの略であるコマンドは、標準入力またはファイルからのテキストに対して編集操作を実行します。 sed 行ごとに非対話型の方法で編集します。

これは、コマンドを呼び出すときにすべての編集決定を行うことを意味し、 sed 指示を自動的に実行します。 これは紛らわしい、または直感的ではないように思われるかもしれませんが、特にスクリプトまたは自動ワークフローの一部として、テキストを変換するための非常に強力で高速な方法です。

このチュートリアルでは、いくつかの基本的な操作について説明し、このエディターの操作に必要な構文を紹介します。 通常のテキストエディタを次のように置き換えることはほとんどありません。 sed、しかしそれはおそらくあなたのテキスト編集ツールボックスへの歓迎された追加になるでしょう。

:このチュートリアルでは、GNUバージョンの sed Ubuntuおよびその他のLinuxオペレーティングシステムで見つかりました。 macOSを使用している場合は、さまざまなオプションと引数を持つBSDバージョンがあります。 GNUバージョンをインストールできます sed 自作を使用して brew install gnu-sed.

基本的な使用法

sed テキストファイルまたは標準入力(STDIN)から読み取るテキストのストリームを操作します。 これは、別のコマンドの出力を直接sedに送信して編集したり、作成済みのファイルで作業したりできることを意味します。

また、次のことに注意する必要があります sed デフォルトでは、すべてを標準出力(STDOUT)に出力します。 つまり、リダイレクトされない限り、 sed ファイルに保存する代わりに、出力を画面に出力します。

基本的な使用法は次のとおりです。

  1. sed [options] commands [file-to-edit]

このチュートリアルでは、BSDソフトウェアライセンスのコピーを使用して実験します sed. Ubuntuでは、次のコマンドを実行してBSDライセンスファイルをホームディレクトリにコピーし、操作できるようにします。

  1. cd
  2. cp /usr/share/common-licenses/BSD .

BSDライセンスのローカルコピーがない場合は、次のコマンドを使用して自分で作成します。

  1. cat << 'EOF' > BSD
  2. Copyright (c) The Regents of the University of California.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. Neither the name of the University nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  19. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. SUCH DAMAGE.
  26. EOF

使ってみよう sed BSDライセンスファイルの内容を表示します。 sed デフォルトでは、結果が画面に送信されます。つまり、編集コマンドを渡さなくても、ファイルリーダーとして使用できます。 次のコマンドを実行してみてください。

  1. sed '' BSD

画面にBSDライセンスが表示されます。

Output
Copyright (c) The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. ... ...

一重引用符には、渡す編集コマンドが含まれています sed. この場合、あなたはそれを何も渡さなかったので、 sed 受信した各行を標準出力に印刷しました。

sed ファイルではなく標準入力を使用できます。 の出力をパイプします cat にコマンド sed 同じ結果を生成するには:

  1. cat BSD | sed ''

ファイルの出力が表示されます。

Output
Copyright (c) The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. . . . . . .

ご覧のとおり、出力をパイプでパイプするときに生成されるもののように、ファイルまたはテキストのストリームを操作できます。 (|) キャラクター、同じように簡単に。

印刷ライン

前の例では、その入力がに渡されるのを見ました sed 操作を行わないと、結果が直接標準出力に出力されます。

探検しましょう sedの明示的な print コマンドを使用して指定します p 一重引用符で囲まれた文字。

次のコマンドを実行します。

  1. sed 'p' BSD

の各行が表示されます BSD ファイルが2回印刷されました:

Output
Copyright (c) The Regents of the University of California. Copyright (c) The Regents of the University of California. All rights reserved. All rights reserved. Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions modification, are permitted provided that the following conditions are met: are met: . . . . . .

sed デフォルトでは各行が自動的に印刷されます。次に、「p」コマンドを使用して行を明示的に印刷するように指示したため、各行が2回印刷されます。

出力を詳しく調べると、最初の行が2回、次に2番目の行が2回というように表示されます。これは、次のことを示しています。 sed データを1行ずつ操作します。 行を読み取り、操作し、結果のテキストを出力してから、次の行でプロセスを繰り返します。

を渡すことで結果をクリーンアップできます -n オプション sed、自動印刷を抑制します。

  1. sed -n 'p' BSD
Output
Copyright (c) The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. . . . . . .

これで、各行を1回印刷することに戻ります。

これまでの例は、編集とはほとんど考えられません(各行を2回印刷したい場合を除いて…)。 次に、その方法を探ります sed テキストデータの特定のセクションをターゲットにすることで出力を変更できます。

アドレス範囲の使用

アドレスを使用すると、テキストストリームの特定の部分をターゲットにできます。 特定の行または行の範囲を指定することもできます。

持ってみましょう sed ファイルの最初の行を印刷します。 次のコマンドを実行します。

  1. sed -n '1p' BSD

最初の行が画面に印刷されます。

Output
Copyright (c) The Regents of the University of California.

番号を配置することによって 1 印刷コマンドの前に、あなたは言いました sed 操作する行番号。 5行も簡単に印刷できます(「-n」を忘れないでください)。

  1. sed -n '1,5p' BSD

次の出力が表示されます。

Output
Copyright (c) The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions

にアドレス範囲を指定しました sed. あなたが与えるなら sed アドレスの場合、それらの行に続くコマンドのみを実行します。 この例では、1行目から5行目までを印刷するようにsedに指示しました。 これを別の方法で指定するには、最初のアドレスを指定してから、オフセットを使用して、次のように、移動する追加の行の数をsedに通知します。

  1. sed -n '1,+4p' BSD

あなたが言ったので、これは同じ出力になります sed 1行目から開始し、次の4行でも操作します。

1行おきに印刷する場合は、 ~ キャラクター。 次のコマンドは、1行おきに出力します。 BSD 1行目から始まるファイル

  1. sed -n '1~2p' BSD

表示される出力は次のとおりです。

Output
Copyright (c) The Regents of the University of California. modification, are permitted provided that the following conditions 1. Redistributions of source code must retain the above copyright 2. Redistributions in binary form must reproduce the above copyright documentation and/or other materials provided with the distribution. may be used to endorse or promote products derived from this software . . . . . .

使用できます sed 出力からテキストも削除します。

テキストの削除

以前にテキスト印刷を指定していた場所で、を変更することにより、テキストの削除を実行できます。 p にコマンド d 指図。

この場合、あなたはもはや必要ありません -n コマンドのため sed 削除されていないものはすべて印刷されます。 これは、何が起こっているかを確認するのに役立ちます。

前のセクションの最後のコマンドを変更して、最初の行から始まる1行おきに削除するようにします。

  1. sed '1~2d' BSD

その結果、前回ではなくであったすべての行が表示されます。

Output
All rights reserved. Redistribution and use in source and binary forms, with or without are met: notice, this list of conditions and the following disclaimer. notice, this list of conditions and the following disclaimer in the 3. Neither the name of the University nor the names of its contributors without specific prior written permission. . . . . . .

ここで重要なのは、ソースファイルは影響を受けていないということです。 それはまだ無傷です。 編集内容が画面に出力されます。

編集内容を保存する場合は、次のように標準出力をファイルにリダイレクトできます。

  1. sed '1~2d' BSD > everyother.txt

次に、ファイルを開きます cat:

  1. cat everyother.txt

以前に画面に表示されたものと同じ出力が表示されます。

Output
All rights reserved. Redistribution and use in source and binary forms, with or without are met: notice, this list of conditions and the following disclaimer. notice, this list of conditions and the following disclaimer in the 3. Neither the name of the University nor the names of its contributors without specific prior written permission. . . . . . .

The sed コマンドはデフォルトではソースファイルを編集しませんが、この動作を変更するには、 -i オプションは、「インプレースで編集を実行する」ことを意味します。 これにより、ソースファイルが変更されます。

警告-i スイッチは元のファイルを上書きするため、これは注意して使用する必要があります。 なしで操作を実行します -i 最初に切り替えてから、コマンドを再度実行します。 -i 必要なものが揃ったら、元のファイルのバックアップを作成するか、出力をファイルにリダイレクトします。 誤って元のファイルを変更するのは非常に簡単です -i スイッチ。

編集して試してみましょう everyother.txt 作成したファイルをインプレースで。 1行おきにもう一度削除して、ファイルをさらに減らしましょう。

  1. sed -i '1~2d' everyother.txt

使用する場合 cat ファイルを表示するには cat everyother.txt、ファイルが編集されていることがわかります。

The -i オプションは危険にすることができます。 ありがたいことに、 sed 編集前にバックアップファイルを作成する機能を提供します。

編集前にバックアップファイルを作成するには、「-i」オプションの直後にバックアップ拡張子を追加します。

  1. sed -i.bak '1~2d' everyother.txt

これにより、バックアップファイルが作成されます。 .bak 拡張子を付けてから、元のファイルをインプレースで編集します。

次に、使用方法を見ていきます sed 検索および置換操作を実行します。

テキストの置換

おそらく最もよく知られている用途 sed テキストを置き換えています。 sed 正規表現を使用してテキストパターンを検索し、見つかったテキストを別のものに置き換えることができます。

LinuxでのGrep正規表現を使用したテキストパターンの検索に従って、正規表現の詳細を学ぶことができます。

最も基本的な形式では、次の構文を使用して1つの単語を別の単語に変更できます。

's/old_word/new_word/'

The s 代替コマンドです。 3つのスラッシュ(/)は、異なるテキストフィールドを区切るために使用されます。 さらに役立つ場合は、他の文字を使用してフィールドを区切ることができます。

たとえば、Webサイト名を変更しようとしている場合、URLにはスラッシュが含まれているため、別の区切り文字を使用すると便利です。

次のコマンドを実行して、次のURLを印刷します。 echo で変更します sed、アンダースコアを使用(_)区切り文字としての文字:

  1. echo "http://www.example.com/index.html" | sed 's_com/index_org/home_'

これは置き換えます com/indexorg/home. 出力には、変更されたURLが表示されます。

Output
http://www.example.org/home.html

最後の区切り文字を忘れないでください、または sed 不平を言うでしょう。 このコマンドを実行した場合:

  1. echo "http://www.example.com/index.html" | sed 's_com/index_org/home'

次の出力が表示されます。

Output
sed: -e expression #1, char 20: unterminated `s' command

いくつかの置換を練習するために新しいファイルを作成しましょう。 次のコマンドを実行して、という新しいテキストファイルを作成します。 song.txt:

  1. echo "this is the song that never ends
  2. yes, it goes on and on, my friend
  3. some people started singing it
  4. not knowing what it was
  5. and they'll continue singing it forever
  6. just because..." > song.txt

それでは、式を置き換えましょう onforward. 次のコマンドを使用します。

  1. sed 's/on/forward/' song.txt

出力は次のようになります。

Output
this is the sforwardg that never ends yes, it goes forward and on, my friend some people started singing it not knowing what it was and they'll cforwardtinue singing it forever just because...

ここでいくつかの注目すべき点を見ることができます。 まず、それは sed 単語ではなく、パターンを置き換えました。 The on 内部 song に変更されます forward.

もう1つ注意すべき点は、2行目で2番目のことです。 on に変更されませんでした forward.

これは、デフォルトでは、 s コマンドは、行の最初の一致で動作し、次の行に移動します。 作る sed のすべてのインスタンスを置き換えます on 各行の最初の行だけでなく、代替コマンドにオプションのフラグを渡す必要があります。

提供する g 置換セットの後に配置することにより、置換コマンドにフラグを立てます。

  1. sed 's/on/forward/g' song.txt

次の出力が表示されます。

Output
this is the sforwardg that never ends yes, it goes forward and forward, my friend some people started singing it not knowing what it was and they'll cforwardtinue singing it forever just because...

これで、substituteコマンドはすべてのインスタンスを変更します。

のみが、sedが各行で検出した「on」の2番目のインスタンスを変更したい場合は、番号を使用します 2 の代わりに g:

  1. sed 's/on/forward/2' song.txt

今回は、2回目の出現がないため、他の行は変更されていません。

Output
this is the song that never ends yes, it goes on and forward, my friend some people started singing it not knowing what it was and they'll continue singing it forever just because...

置換された行のみを確認したい場合は、 -n 自動印刷を抑制するオプションを再度選択します。

その後、合格することができます p 置換が行われた行を印刷するためのsubstituteコマンドのオプション。

  1. sed -n 's/on/forward/2p' song.txt

変更された行が画面に印刷されます。

Output
yes, it goes on and forward, my friend

ご覧のとおり、コマンドの最後でフラグを組み合わせることができます。

検索プロセスで大文字と小文字を区別しないようにする場合は、「i」フラグを渡すことができます。

  1. sed 's/SINGING/saying/i' song.txt

表示される出力は次のとおりです。

Output
this is the song that never ends yes, it goes on and on, my friend some people started saying it not knowing what it was and they'll continue saying it forever just because...

一致したテキストの置き換えと参照

正規表現を使用してより複雑なパターンを見つけたい場合は、置換テキストで一致したパターンを参照するためのさまざまな方法があります。

たとえば、行の先頭から at、次のコマンドを使用します。

  1. sed 's/^.*at/REPLACED/' song.txt

次の出力が表示されます。

Output
REPLACED never ends yes, it goes on and on, my friend some people started singing it REPLACED it was and they'll continue singing it forever just because...

ワイルドカード式が行の先頭からの最後のインスタンスまで一致していることがわかります。 at.

検索文字列で一致する正確なフレーズがわからないため、 & 置換文字列で一致したテキストを表す文字。

一致したテキストを括弧で囲みましょう。

  1. sed 's/^.*at/(&)/' song.txt

次の出力が表示されます。

Output
(this is the song that) never ends yes, it goes on and on, my friend some people started singing it (not knowing what) it was and they'll continue singing it forever just because...

一致したテキストを参照するより柔軟な方法は、エスケープされた括弧を使用して、一致したテキストのセクションをグループ化することです。

括弧でマークされた検索テキストのすべてのグループは、エスケープされた参照番号で参照できます。 たとえば、最初の括弧グループは次のように参照できます。 \1、2番目の \2 等々。

この例では、各行の最初の2つの単語を切り替えます。

  1. sed 's/\([a-zA-Z0-9][a-zA-Z0-9]*\) \([a-zA-Z0-9][a-zA-Z0-9]*\)/\2 \1/' song.txt

次の出力が表示されます。

Output
is this the song that never ends yes, goes it on and on, my friend people some started singing it knowing not what it was they and'll continue singing it forever because just...

ご覧のとおり、結果は完全ではありません。 たとえば、2行目は、文字セットにリストされていない文字が含まれているため、最初の単語をスキップします。 同様に、それは扱いました they'll 5行目の2つの単語として。

より正確になるように正規表現を改善しましょう。

  1. sed 's/\([^ ][^ ]*\) \([^ ][^ ]*\)/\2 \1/' song.txt

次の出力が表示されます。

Output
is this the song that never ends it yes, goes on and on, my friend people some started singing it knowing not what it was they'll and continue singing it forever because... just

これは前回よりもはるかに優れています。 これにより、句読点が関連する単語とグループ化されます。

かっこ内で式を繰り返す方法に注意してください(1回は * 文字、そしてそれから一度)。 これは、 * 文字は、0回以上前にある文字セットと一致します。 これは、パターンが見つからない場合でも、ワイルドカードとの一致が「一致」と見なされることを意味します。

それを確保するために sed 少なくとも1回はテキストを検索します。ワイルドカードを使用する前に、ワイルドカードなしで1回一致させる必要があります。

結論

このチュートリアルでは、 sed 指図。 ファイルから特定の行を印刷し、テキストを検索し、行を削除し、元のファイルを上書きし、正規表現を使用してテキストを置き換えました。 適切に構築されたsedコマンドを使用してテキストドキュメントをすばやく変換する方法をすでに理解できるはずです。

このシリーズの次の記事では、いくつかのより高度な機能について説明します。