Lesson 1





練習問題A

1. longleg.txt の各行の先頭に行番号を付けて出力するスクリプトを書きなさい。行番号とレコードの間にはスペース,コロン,タブなどを入れて見やすくなるようにしなさい。

 > perl -ne 'print "$.:\t$_";' longleg.txt | more
          ※ 行番号とレコードの間にコロンとタブを挿入

1:      JEAN WEBSTER
2:      DADDY-LONG-LEGS
3:     
4:      Copyright 1912 by The Century Company
5:     
6:     
7:     
8:      TO YOU
9:     
10:    
11:     Blue Wednesday
12:    
13:     The first Wednesday in every month was a Perfectly Awful Day--a day
14:     to be awaited with dread, endured with courage and forgotten with haste.
15:     Every floor must be spotless, every chair dustless, and every bed
16:     without a wrinkle.  Ninety-seven squirming little orphans must be
17:     scrubbed and combed and buttoned into freshly starched ginghams;
18:     and all ninety-seven reminded of their manners, and told to say,
19:     `Yes, sir,' `No, sir,' whenever a Trustee spoke.
20:    
21:     It was a distressing time; and poor Jerusha Abbott, being the
22:     oldest orphan, had to bear the brunt of it.  But this particular
23:     first Wednesday, like its predecessors, finally dragged itself
24:     to a close.  Jerusha escaped from the pantry where she had been
25:     making sandwiches for the asylum's guests, and turned upstairs
26:     to accomplish her regular work.  Her special care was room F,
27:     where eleven little tots, from four to seven, occupied eleven little
28:     cots set in a row.  Jerusha assembled her charges, straightened their
29:     rumpled frocks, wiped their noses, and started them in an orderly
30:     and willing line towards the dining-room to engage themselves
31:     for a blessed half hour with bread and milk and prune pudding.

......





2. longleg.txt から 'Pendleton' を含む行を,行番号を付けて出力するスクリプトを書きなさい。(cf. grep)

 > perl -ne 'if(/Pendleton/){print "$.:\t$_";}' longleg.txt

350:    Freshmen named Sallie McBride and Julia Rutledge Pendleton.
440:    Rutledge Pendleton the least so.  It's queer what a mixture
444:    that if you are a Pendleton, that fact alone admits you to heaven
576:    Julia Pendleton tried for the team, but she didn't get in.  Hooray!
622:    That wouldn't be an awfully big wardrobe for Julia Rutledge Pendleton,
814:    silk stockings.  Julia Pendleton comes into my room to do geometry,
921:    Julia Pendleton dropped in this evening to pay a social call,
1366:   And with a very superior man--with Mr. Jervis Pendleton of the House
1380:   care much for Pendletons.
1383:   not a Pendleton at all.  We had a beautiful time; I've longed
1387:   Mr. Pendleton reminded me a little of you, Daddy, as you were twenty
1527:   For listen to this:  Mr. Jervis Pendleton used to own this farm,
1535:   in her opinion.  Knowing a member of the Pendleton family
1656:                  Jervis Pendleton
1671:   and brave and truthful.  I'm sorry to think he is a Pendleton;
1720:   Sallie McBride and Julia Rutledge Pendleton.  It's the truth.
1725:   for they are not a bit alike; but the Pendletons are naturally
1728:   rooming with a Pendleton.  This is a democratic country.
2374:   dreamed that a Pendleton could display so much comedy spirit--
2376:   Pendleton though, an more than I consider you a true Trustee.
2779:   Semple from Mr. Pendleton.  He's motoring through the Berkshires,
2879:   true Pendleton, but he isn't in the least.  He is just as simple
2961:   This morning, said Mrs. Semple to Mr. Pendleton, with a very
3022:   Mr. Pendleton if he knew you in New York--I should think he might;
3232:   Julia Pendleton has invited me to visit her for the Christmas holidays.
3239:   I'm rather awed at the prospect of meeting Pendletons EN MASSE,
3396:   Mrs. Pendleton never thinks of anything but jewels and dressmakers
3400:   world would I ever let any children of mine develop into Pendletons.
3819:   A letter comes from Mr. Jervis Pendleton in Paris, rather a short
4632:   My Very Dearest Master-Jervie-Daddy-Long-Legs Pendleton-Smith,





3. 上のスクリプトに手を加え,'Pendleton' にマーキングして出力するようにしなさい。マーキングは '<<Pendleton>>' などのようにする。

 > perl -ne 'if(s/Pendleton/**Pendleton**/g){print "$.:\t$_";}' longleg.txt
          ※ マーキングとして**を使用

350:    Freshmen named Sallie McBride and Julia Rutledge **Pendleton**.
440:    Rutledge **Pendleton** the least so.  It's queer what a mixture
444:    that if you are a **Pendleton**, that fact alone admits you to heaven
576:    Julia **Pendleton** tried for the team, but she didn't get in.  Hooray!
622:    That wouldn't be an awfully big wardrobe for Julia Rutledge **Pendleton**,
814:    silk stockings.  Julia **Pendleton** comes into my room to do geometry,
921:    Julia **Pendleton** dropped in this evening to pay a social call,
1366:   And with a very superior man--with Mr. Jervis **Pendleton** of the House
1380:   care much for **Pendleton**s.
1383:   not a **Pendleton** at all.  We had a beautiful time; I've longed
1387:   Mr. **Pendleton** reminded me a little of you, Daddy, as you were twenty
1527:   For listen to this:  Mr. Jervis **Pendleton** used to own this farm,
1535:   in her opinion.  Knowing a member of the **Pendleton** family
1656:                  Jervis **Pendleton**
1671:   and brave and truthful.  I'm sorry to think he is a **Pendleton**;
1720:   Sallie McBride and Julia Rutledge **Pendleton**.  It's the truth.
1725:   for they are not a bit alike; but the **Pendleton**s are naturally
1728:   rooming with a **Pendleton**.  This is a democratic country.
2374:   dreamed that a **Pendleton** could display so much comedy spirit--
2376:   **Pendleton** though, an more than I consider you a true Trustee.
2779:   Semple from Mr. **Pendleton**.  He's motoring through the Berkshires,
2879:   true **Pendleton**, but he isn't in the least.  He is just as simple
2961:   This morning, said Mrs. Semple to Mr. **Pendleton**, with a very
3022:   Mr. **Pendleton** if he knew you in New York--I should think he might;
3232:   Julia **Pendleton** has invited me to visit her for the Christmas holidays.
3239:   I'm rather awed at the prospect of meeting **Pendleton**s EN MASSE,
3396:   Mrs. **Pendleton** never thinks of anything but jewels and dressmakers
3400:   world would I ever let any children of mine develop into **Pendleton**s.
3819:   A letter comes from Mr. Jervis **Pendleton** in Paris, rather a short
4632:   My Very Dearest Master-Jervie-Daddy-Long-Legs **Pendleton**-Smith,





練習問題B

1. longleg.txt から 'Pendleton' を含む行を以下の形式で出力するスクリプトを書きなさい。
   a) 各行の先頭には,通し番号と行番号を付ける。
   b) 'Pendleton' にはマーキングする。


 > perl -ne 'if(s/Pendleton/**Pendleton**/g){$num = $num + 1; print "$num:\t$.:\t$_";}' longleg.txt
          ※ 通し番号と行番号の間にコロンとタブを、行番号とレコードの間にもコロンとタブを挿入
          ※ マーキングとして**を使用

1:      350:    Freshmen named Sallie McBride and Julia Rutledge **Pendleton**.
2:      440:    Rutledge **Pendleton** the least so.  It's queer what a mixture
3:      444:    that if you are a **Pendleton**, that fact alone admits you to heaven
4:      576:    Julia **Pendleton** tried for the team, but she didn't get in.  Hooray!
5:      622:    That wouldn't be an awfully big wardrobe for Julia Rutledge **Pendleton**,
6:      814:    silk stockings.  Julia **Pendleton** comes into my room to do geometry,
7:      921:    Julia **Pendleton** dropped in this evening to pay a social call,
8:      1366:   And with a very superior man--with Mr. Jervis **Pendleton** of the House
9:      1380:   care much for **Pendleton**s.
10:     1383:   not a **Pendleton** at all.  We had a beautiful time; I've longed
11:     1387:   Mr. **Pendleton** reminded me a little of you, Daddy, as you were twenty
12:     1527:   For listen to this:  Mr. Jervis **Pendleton** used to own this farm,
13:     1535:   in her opinion.  Knowing a member of the **Pendleton** family
14:     1656:                  Jervis **Pendleton**
15:     1671:   and brave and truthful.  I'm sorry to think he is a **Pendleton**;
16:     1720:   Sallie McBride and Julia Rutledge **Pendleton**.  It's the truth.
17:     1725:   for they are not a bit alike; but the **Pendleton**s are naturally
18:     1728:   rooming with a **Pendleton**.  This is a democratic country.
19:     2374:   dreamed that a **Pendleton** could display so much comedy spirit--
20:     2376:   **Pendleton** though, an more than I consider you a true Trustee.
21:     2779:   Semple from Mr. **Pendleton**.  He's motoring through the Berkshires,
22:     2879:   true **Pendleton**, but he isn't in the least.  He is just as simple
23:     2961:   This morning, said Mrs. Semple to Mr. **Pendleton**, with a very
24:     3022:   Mr. **Pendleton** if he knew you in New York--I should think he might;
25:     3232:   Julia **Pendleton** has invited me to visit her for the Christmas holidays.
26:     3239:   I'm rather awed at the prospect of meeting **Pendleton**s EN MASSE,
27:     3396:   Mrs. **Pendleton** never thinks of anything but jewels and dressmakers
28:     3400:   world would I ever let any children of mine develop into **Pendleton**s.
29:     3819:   A letter comes from Mr. Jervis **Pendleton** in Paris, rather a short
30:     4632:   My Very Dearest Master-Jervie-Daddy-Long-Legs **Pendleton**-Smith,




2. longleg.txt の空行以外の各行の先頭に行番号を付けて出力するスクリプトを書きなさい。

 > perl -ne 'if(/.+/){print "$.:\t";} print;' longleg.txt | more

1:      JEAN WEBSTER
2:      DADDY-LONG-LEGS

4:      Copyright 1912 by The Century Company



8:      TO YOU


11:     Blue Wednesday

13:     The first Wednesday in every month was a Perfectly Awful Day--a day
14:     to be awaited with dread, endured with courage and forgotten with haste.
15:     Every floor must be spotless, every chair dustless, and every bed
16:     without a wrinkle.  Ninety-seven squirming little orphans must be
17:     scrubbed and combed and buttoned into freshly starched ginghams;
18:     and all ninety-seven reminded of their manners, and told to say,
19:     `Yes, sir,' `No, sir,' whenever a Trustee spoke.

21:     It was a distressing time; and poor Jerusha Abbott, being the
22:     oldest orphan, had to bear the brunt of it.  But this particular
23:     first Wednesday, like its predecessors, finally dragged itself
24:     to a close.  Jerusha escaped from the pantry where she had been
25:     making sandwiches for the asylum's guests, and turned upstairs
26:     to accomplish her regular work.  Her special care was room F,
27:     where eleven little tots, from four to seven, occupied eleven little
28:     cots set in a row.  Jerusha assembled her charges, straightened their
29:     rumpled frocks, wiped their noses, and started them in an orderly
30:     and willing line towards the dining-room to engage themselves
31:     for a blessed half hour with bread and milk and prune pudding.
......




3.
longleg.txt の各段落(空行で区切られた行の集まり)の改行を取るスクリプトを書きなさい。

 > perl -ne 'if(/.+/){s/\n/ /g;} print;' longleg.txt | more

JEAN WEBSTER DADDY-LONG-LEGS
Copyright 1912 by The Century Company


TO YOU

Blue Wednesday
The first Wednesday in every month was a Perfectly Awful Day--a day to be awaited with dread, endured with courage and forgotten with haste.  Every floor must be spotless, every chair dustless, and every bed without a wrinkle.  Ninety-seven squirming little orphans must be scrubbed and combed and buttoned into freshly starched ginghams; and all ninety-seven reminded of their manners, and told to say, `Yes, sir,' `No, sir,' whenever a Trustee spoke.
It was a distressing time; and poor Jerusha Abbott, being the oldest orphan, had to bear the brunt of it.  But this particular first Wednesday, like its predecessors, finally dragged itself to a close.  Jerusha escaped from the pantry where she had been making sandwiches for the asylum's guests, and turned upstairs to accomplish her regular work.  Her special care was room F, where eleven little tots, from four to seven, occupied eleven little cots set in a row.  Jerusha assembled her charges, straightened their rumpled frocks, wiped their noses, and started them in an orderly and willing line towards the dining-room to engage themselves for a blessed half hour with bread and milk and prune pudding.

......