[小西ホームページ]   [目次・索引]   [前の授業]   [次の授業]

コンピュータIID(UNIXとHTML)第3回

目次
3.1 テキスト処理(2)
3.1.1 テキストの比較
3.1.2 テキストの探索
3.1.3 テキストの置換
3.1.4 テキストの選択
3.2 演習3
3.3 レポート課題
3.4 参考文献
索引

3.1 テキスト処理(2)

3.1.1 テキストの比較

diffコマンド (DIFFerence)は、2つのファイルを比較して、その違いを表示します。 形式は

diff file1 file2

です。

もし、ファイル file1file2 が完全に同じものなら、何も表示されません。

b04a001@AsiaA1:~% cd comp2d
b04a001@AsiaA1:~/comp2d% cat month01.txt
January
February
March
April
May
June
July
August
September
October
November
December
b04a001@AsiaA1:~/comp2d% diff month01.txt month01.txt
b04a001@AsiaA1:~/comp2d%

ファイル file1file2 が違っている場合は、「 file1 のどこをどう直せば file2 になるか」という観点で、ファイルの違いを表示します。 これは記号の羅列で、あまり読みやすくないのですが、一応説明します。

表示の中の記号"<"は file1 を表し、">"は file2 を表します。 また、"a", "c", "d"は、それぞれ「追加(Append)」、「変更(Change)」、「削除(Delete)」を意味します。

例えば、ファイルmonth01.txtから"April"と"May"を取り除いたものをmonth02.txtとします。

b04a001@AsiaA1:~/comp2d% cat month02.txt
January
February
March
June
July
August
September
October
November
December
b04a001@AsiaA1:~/comp2d% diff month01.txt month02.txt
4,5d3
< April
< May
b04a001@AsiaA1:~/comp2d%

これは、month01.txtの4行目から5行目の"April", "May"を削除すると、(これ以降とmonth02.txtの3行目以降が同じくなって、)month02.txtになるという意味です。

次に、ファイルmonth01.txtとmonth02.txtを入れ替えてみます。

b04a001@AsiaA1:~/comp2d% diff month02.txt month01.txt
3a4,5
> April
> May
b04a001@AsiaA1:~/comp2d%

これは、month02.txtの3行目以降に、month01.txtの4行目から5行目までの"April", "May"を追加すると、month01.txtになるという意味です。

最後に、ファイルmonth01.txtの"March", "April", "May"を"Spring"に置き換えたものをmonth03.txtとします。

b04a001@AsiaA1:~/comp2d% cat month03.txt
January
February
Spring
June
July
August
September
October
November
December
b04a001@AsiaA1:~/comp2d% diff month01.txt month03.txt
3,5c3
< March
< April
< May
---
> Spring
b04a001@AsiaA1:~/comp2d%

これは、month01.txtの3行目から5行目までの"March", "April", "May"を、month03.txtの3行目の"Spring"で置き換えると、month03.txtになるという意味です。

3.1.2 テキストの探索

ここで、以下の内容のテキスト・ファイルweather01.txtを作成します。

October 1 Sunday Cloudy
October 2 Monday Fine
October 3 Tuesday Cloudy
October 4 Wednesday Cloudy
October 5 Thursday Rainy
October 6 Friday Rainy
October 7 Saturday Fine

grepコマンド (General Regular Expression Print)は、文字列のパターンを探索して、それが現れる行を表示します。 形式は

grep pattern file

です。 パターン pattern は、単語として探索されるだけでなく、単語の内部にあっても探索されます。

b04a001@AsiaA1:~/comp2d% cat weather01.txt
October 1 Sunday Cloudy
October 2 Monday Fine
October 3 Tuesday Cloudy
October 4 Wednesday Cloudy
October 5 Thursday Rainy
October 6 Friday Rainy
October 7 Saturday Fine
b04a001@AsiaA1:~/comp2d% grep Fine weather01.txt
October 2 Monday Fine
October 7 Saturday Fine
b04a001@AsiaA1:~/comp2d% grep ne weather01.txt
October 2 Monday Fine
October 4 Wednesday Cloudy
October 7 Saturday Fine
b04a001@AsiaA1:~/comp2d%

最初の例では、晴れ(Fine)の日を探索しています。 次の例では、文字列"ne"を探索し、"Fine"と"Wednesday"の行が表示されています。

パターン pattern には、アルファベットや数字だけでなく、スペースや記号を書くこともできます。 ただし、スペースは引数の区切りと見なされますし、たいていの記号には特別な意味があります。 スペースや記号の意味を無くすには、パターンをシングルクオート(')で囲みます。

例えば、10月1日(October 1)を探索するには、次のようにします。

b04a001@AsiaA1:~/comp2d% grep 'October 1' weather01.txt
October 1 Sunday Cloudy
b04a001@AsiaA1:~/comp2d%

パターン pattern が現れない行を探索することもできます。 形式は

grep -v pattern file

です。

例えば、雨(Rainy)でない日を探索するには、次のようにします。

b04a001@AsiaA1:~/comp2d% grep -v Rainy weather01.txt
October 1 Sunday Cloudy
October 2 Monday Fine
October 3 Tuesday Cloudy
October 4 Wednesday Cloudy
October 7 Saturday Fine
b04a001@AsiaA1:~/comp2d%

3.1.3 テキストの置換

sedコマンド (Stream EDitor)は、テキストを一括して変換します。 形式は

sed script file

です。 スクリプト script は記号の羅列であり、sedコマンドはこれを解釈して、テキストの追加や削除などを行います。

ここでは、特にテキストの置換について説明します。 テキストを置換する場合のsedコマンドの形式は、

sed s/pattern/replacement/ file

です。 このコマンドで、テキストの中のパターン pattern が、文字列 replacement で置き換えられます。

例えば、"October"を"10"で置き換えるには、次のようにします。

b04a001@AsiaA1:~/comp2d% sed s/October/10/ weather01.txt
10 1 Sunday Cloudy
10 2 Monday Fine
10 3 Tuesday Cloudy
10 4 Wednesday Cloudy
10 5 Thursday Rainy
10 6 Friday Rainy
10 7 Saturday Fine
b04a001@AsiaA1:~/comp2d%

実は、上記の形式では、一行一行の最初の一か所しか置き換えません。 すべてを置き換えるには、

sed s/pattern/replacement/g file

と書きます。

b04a001@AsiaA1:~/comp2d% sed 's/ /, /' weather01.txt
October, 1 Sunday Cloudy
October, 2 Monday Fine
October, 3 Tuesday Cloudy
October, 4 Wednesday Cloudy
October, 5 Thursday Rainy
October, 6 Friday Rainy
October, 7 Saturday Fine
b04a001@AsiaA1:~/comp2d% sed 's/ /, /g' weather01.txt
October, 1, Sunday, Cloudy
October, 2, Monday, Fine
October, 3, Tuesday, Cloudy
October, 4, Wednesday, Cloudy
October, 5, Thursday, Rainy
October, 6, Friday, Rainy
October, 7, Saturday, Fine
b04a001@AsiaA1:~/comp2d%

最初の例では、スペースをコンマ・スペースで置き換えていますが、置き換えられるのは一つだけです。 次の例では、"g"を書いて、すべてのスペースを置き換えています。 ここで、シングルクオートで囲んで、スペースや記号の意味をなくしていることに注意してください。

3.1.4 テキストの選択

awkコマンド (開発者Aho, Weinberger, Kernighanの頭文字)は、パターン・マッチによってテキストを処理します。 形式は

awk 'pattern {action}' file

です。 パターン pattern とアクション action は記号の羅列であり、awkコマンドはこれを解釈して、さまざまなテキスト処理を行います。 パターンやアクションには色々な記号を書きますので、あらかじめシングルクオートで囲んでおきます。

ここでは、特に列の選択について説明します。 列を選択する場合のawkコマンドの形式は、

awk '{print $n}' file

です。 このコマンドで、テキストの n 列目が取り出されます。

例えば、4列目を取り出すには、次のようにします。

b04a001@AsiaA1:~/comp2d% awk '{print $4}' weather01.txt
Cloudy
Fine
Cloudy
Cloudy
Rainy
Rainy
Fine
b04a001@AsiaA1:~/comp2d%

複数の列を取り出すには、printの後に、列番号をコンマ区切りで書きます。

例えば、3列目、2列目、1列目を取り出すには、次のようにします。

b04a001@AsiaA1:~/comp2d% awk '{print $3, $2, $1}' weather01.txt
Sunday 1 October
Monday 2 October
Tuesday 3 October
Wednesday 4 October
Thursday 5 October
Friday 6 October
Saturday 7 October
b04a001@AsiaA1:~/comp2d%

3.2 演習3

まず、以下の内容をファイルpresident01.txtに保存してください。

34 Dwight Eisenhower Republican 1953
35 John Kennedy Democratic 1961
36 Lyndon Johnson Democratic 1963
37 Richard Nixon Republican 1969
38 Gerald Ford Republican 1974
39 James Carter Democratic 1977
40 Ronald Reagan Republican 1981
41 George Bush Republican 1989
42 William Clinton Democratic 1993
43 George Bush Republican 2001

問1. コマンドを使って、共和党(Republican)の歴代大統領を表示してください。

問2. コマンドを使って、歴代大統領の名字を表示してください。

余力のある人は、次の問題にも答えてください。

問3. コマンドを使って、"George Bush"という名前の歴代大統領を表示してください。

問4. コマンドを使って、歴代大統領の就任した年と名字を表示してください。


3.3 レポート課題

今日の演習3の答案(「ターミナル」画面のコピー)をメールでkonishi@twcu.ac.jp宛に提出してください。 メールを送るときは、大学のパソコンを使うか、大学のメール・サーバに接続するかして、差出人が大学のメール・アドレスになるようにしてください。 メールの本文には、学生番号、氏名、科目名、授業日(10月11日)を明記してください。


3.4 参考文献


[小西ホームページ]   [目次・索引]   [前の授業]   [次の授業]

2006年10月14日更新
小西 善二郎 <konishi@twcu.ac.jp>
Copyright (C) 2006 Zenjiro Konishi. All rights reserved.