基本構文

基本構文

SEDの書式

SEDは次のように呼び出します。

sed コマンド (スクリプトコマンド) ファイル名

コマンドについて

書式内にあるコマンドは、特定の行を削除したり、抽出するなど
ファイルへの操作内容を指定するためのものです。
詳しくは次のページで紹介します。

SEDのコマンド

SEDのスクリプトコマンド

サンプルファイルを作成してください。

books.txt

[user01@localhost ~]$ vi books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

ファイル確認

[user01@localhost ~]$ cat books.txt
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864

基本構文の練習①

特定の行のみを削除するようにSEDに指示します。
ここでは3行を除外するために、-eオプションで3つの個別のコマンドを指定しています。

[user01@localhost ~]$ sed -e '1d' -e '2d' -e '5d' books.txt
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
6) A Game of Thrones, George R. R. Martin, 864

基本構文の練習②

テキストファイルに複数のSEDコマンドを記述し、そのテキスファイルをSEDの引数として指定することができます。

[user01@localhost ~]$ echo -e "1d\n2d\n5d" > commands.txt
[user01@localhost ~]$ cat commands.txt
1d
2d
5d

基本構文の練習③

テキストファイルからコマンドを読み取るようにSEDに指示します。
ここでは、前の例と同じ結果が出力されます。

[user01@localhost ~]$ sed -f commands.txt books.txt
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
6) A Game of Thrones, George R. R. Martin, 864

基本構文の練習④

次の演習を実施する前に、簡単にpコマンドと、-nオプションについて復習します。

pコマンドについて

特定の行を出力するために使用します。
行番号とpを併せて記述することで、指定の行が出力されますが((※例:「3p」の場合、3行目が出力されます)、
行番号を指定しない場合、全ての行が出力されます。

-nオプションの役割

「-n」オプションは、出力コマンド以外の出力を行わせないためのオプションです。
pコマンドで特定の行を指定しても、対象外の行がそのまま出力されるので、「-nオプション」を併用することで、特定の行だけを出力することができます。

コマンドnとスクリプトコマンドpを組み合わせることで、テキストファイルの内容を出力しましょう。

[user01@localhost ~]$ sed -n p books.txt
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864

基本構文の練習⑤

以下のように特定の行のみを出力することもできます。

[user01@localhost ~]$ sed -n '3p' books.txt
3) The Alchemist, Paulo Coelho, 197

基本構文の練習⑥

書式:特定の範囲の行を出力する

sed –n ‘開始行,終了行 p’ ファイル名

実践

さらに、任意の行から特定の行まで範囲を指定して出力することもできます。

[user01@localhost ~]$ sed -n '2,5 p' books.txt
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288

基本構文の練習⑦

ファイルの最終行を表示する特殊文字のドル($)を試してみましょう。

[user01@localhost ~]$ sed -n '$ p' books.txt
6) A Game of Thrones, George R. R. Martin, 864

基本構文の練習⑧

ドル($)文字を使用し、アドレス範囲を指定することもできます。
以下の例は、3行目から最終行まで出力します。

[user01@localhost ~]$ sed -n '3,$ p' books.txt
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864

基本構文の練習⑨

書式:開始行から指定の行数までの出力する

開始行から指定した行分までの範囲(例:「3,+3」と指定した場合、3行目から3行先の6行目までの範囲になります。)を出力します。

sed –n ‘開始行,+行数 p’ ファイル名

実践

[user01@localhost ~]$ sed -n '2,+4 p' books.txt
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864

基本構文の練習⑩

書式:開始行から、数行飛ばしで出力する

開始行から指定した行数を飛ばして出力することができます。(例:「2~2」と指定すると、2行を開始行に、2行飛ばしで4行、6行などが表示されます。)

sed –n ‘開始行~行数 p’ ファイル名

実践

[user01@localhost ~]$ sed -n '1~2 p' books.txt
1) A Storm of Swords, George R. R. Martin, 1216
3) The Alchemist, Paulo Coelho, 197
5) The Pilgrimage, Paulo Coelho, 288


NEXT>> パターン範囲