如何将unix文本文件写入预定义的html代码?

时间:2013-04-15 21:09:13

标签: html unix awk

我不懂HTML(HORRIBLY EMBARRASSED但是没有必要)。当涉及到UNIX时,我非常聪明,但是我对这项任务感到非常困惑。我知道我需要做什么,但是我有最艰难的时刻开始。

我的hwk12目录中有以下文件:

  • roster.html
  • roster.txt
  • sample.html
  • sample.txt的

以下是roster.html文件的内容:

<html>
<body>
<table border=2>
<tr><th>Name</th><th>Username</th><th>Email</th></tr>
  <tr>
    <td>Nikhil Banerjee</td>
    <td>nbanerje</td>
    <td><a href="mailto:zetapsi796@hotmail.com">zetapsi796@hotmail.com</a></td>
  </tr>
  <tr>
    <td>Jeff Nazarian</td>
    <td>jnazaria</td>
    <td><a href="mailto:jeff.nazarian@asu.edu">jeff.nazarian@asu.edu</a></td>
  </tr>
  <tr>
    <td>Anna Melzer</td>
    <td>amelzer</td>
    <td><a href="mailto:anna.melzer@asu.edu">anna.melzer@asu.edu</a></td>
  </tr>
  <tr>
    <td>Jose Garcia</td>
    <td>jgarcia</td>
    <td><a href="mailto:garcia-j@msn.com">garcia-j@msn.com</a></td>
  </tr>
  <tr>
    <td>Jillian Testa</td>
    <td>jtesta</td>
    <td><a href="mailto:jillian.testa@asu.edu">jillian.testa@asu.edu</a></td>
  </tr>
  <tr>
    <td>Clayton Lengelzigich</td>
    <td>clengelz</td>
    <td><a href="mailto:clayton.lengel-zigich@asu.edu">clayton.lengel-  
zigich@asu.edu</a></td>               
  </tr>
  <tr>
    <td>Ashley Bennett</td>
    <td>abennett</td>
    <td><a href="mailto:ashley.bennett@asu.edu">ashley.bennett@asu.edu</a></td>
  </tr>
  <tr>
    <td>Ann Frost</td>
    <td>afrost</td>
    <td><a href="mailto:ann.frost@asu.edu">ann.frost@asu.edu</a></td>
  </tr>
  <tr>
    <td>Timothy Whipple</td>
    <td>twhipple</td>
    <td><a href="mailto:tweed@asu.edu">tweed@asu.edu</a></td>
  </tr>
  <tr>
    <td>Wei Shen</td>
    <td>wshen</td>
    <td><a href="mailto:shenwei58@hotmail.com">shenwei58@hotmail.com</a></td>
  </tr>
  <tr>
    <td>Cari Mahon</td>
    <td>cmahon</td>
    <td><a href="mailto:cari.mahon@asu.edu">cari.mahon@asu.edu</a></td>
  </tr>
  <tr>
    <td>Alberto Salas</td>
    <td>asalas</td>
    <td><a href="mailto:alberto2504@msn.com">alberto2504@msn.com</a></td>
  </tr>
  <tr>
    <td>Dorothy Haskett</td>
    <td>dhaskett</td>
    <td><a href="mailto:dorothy.haskett@asu.edu">dorothy.haskett@asu.edu</a></td>
  </tr>
  <tr>
    <td>Criss Bradbury</td>
    <td>cbradbur</td>
    <td><a href="mailto:crissbradbury@hotmaiil.com">crissbradbury@hotmaiil.com</a></td>
  </tr>
  <tr>
    <td>Steve Ellermann</td>
    <td>sellerma</td>
    <td><a href="mailto:cis494@ellermann.com">cis494@ellermann.com</a></td>
  </tr>
  <tr>
    <td>Zewdie Bekele</td>
    <td>zbekele</td>
    <td><a href="mailto:zewdiea@aol.com">zewdiea@aol.com</a></td>
  </tr>
  <tr>
    <td>Frederic Diziere</td>
    <td>fdiziere</td>
    <td><a href="mailto:fsd@asu.edu">fsd@asu.edu</a></td>
  </tr>
  <tr>
    <td>Matt Bowes</td>
    <td>mbowes</td>
    <td><a href="mailto:matt.bowes@asu.edu">matt.bowes@asu.edu</a></td>
  </tr>
  <tr>
    <td>Jasen Meece</td>
    <td>jmeece</td>
    <td><a href="mailto:jasen.meece@sun.com">jasen.meece@sun.com</a></td>
  </tr>
  <tr>
    <td>Aaron Carpenter</td>
    <td>acarpent</td>
    <td><a href="mailto:aaron.carpenter@asu.edu">aaron.carpenter@asu.edu</a></td>
  </tr>
  <tr>
    <td>Binqin Xi</td>
    <td>bxi</td>
    <td><a href="mailto:binqin.xi@asu.edu">binqin.xi@asu.edu</a></td>
  </tr>
  <tr>
    <td>Yinting Chan</td>
    <td>ychan</td>
    <td><a href="mailto:yin.chen@asu.edu">yin.chen@asu.edu</a></td>
  </tr>
  <tr>
    <td>Michael Evans</td>
    <td>mevans</td>
    <td><a href="mailto:michael.evans@asu.edu">michael.evans@asu.edu</a></td>
  </tr>
  <tr>
    <td>Herman Beringer</td>
    <td>hberinge</td>
    <td><a href="mailto:jber@cox.net">jber@cox.net</a></td>
  </tr>
  <tr>
    <td>Andrew Jolley</td>
    <td>ajolley</td>
    <td><a href="mailto:andrew@andrewjolley.com">andrew@andrewjolley.com</a></td>
  </tr>
  <tr>
    <td>Michael Raby</td>
    <td>mraby</td>
    <td><a href="mailto:mike1071@yahoo.com">mike1071@yahoo.com</a></td>
  </tr>
  <tr>
    <td>Hajar Alaoui</td>
    <td>halaoui</td>
    <td><a href="mailto:hajar6@hotmail.com">hajar6@hotmail.com</a></td>
  </tr>
  <tr>
    <td>Anne Lemar</td>
    <td>alemar</td>
    <td><a href="mailto:anne.lemar@asu.edu">anne.lemar@asu.edu</a></td>
  </tr>
  <tr>
    <td>Russell Crotts</td>
    <td>rcrotts</td>
    <td><a href="mailto:Russell.Crotts@asu.edu">Russell.Crotts@asu.edu</a></td>
  </tr>
  <tr>
    <td>Dan Mazzola</td>
    <td>dmazzola</td>
    <td><a href="mailto:dan.mazzola@sun.com">dan.mazzola@sun.com</a></td>
  </tr>
  <tr>
    <td>Bill Boyton</td>
    <td>bboyton</td>
    <td><a href="mailto:boytonb@earthlink.net">boytonb@earthlink.net</a></td>
  </tr>
</table>
</body>
</html>

以下是roster.txt文件的内容:

Whipple Timothy tweed@asu.edu Shen    Wei     shenwei58@hotmail.com
Mahon   Cari    cari.mahon@asu.edu Salas   Alberto alberto2504@msn.com
Haskett Dorothy dorothy.haskett@asu.edu Bradbury        Criss  
crissbradbury@hotmaiil.com Ellermann       Steve  
cis494@ellermann.com Bekele  Zewdie  zewdiea@aol.com Diziere Frederic 
fsd@asu.edu Bowes   Matt    matt.bowes@asu.edu Meece   Jasen  
jasen.meece@sun.com  Carpenter       Aaron   aaron.carpenter@asu.edu
Xi      Binqin  binqin.xi@asu.edu Chan    Yinting yin.chen@asu.edu
Evans   Michael michael.evans@asu.edu Beringer        Herman 
jber@cox.net Jolley  Andrew  andrew@andrewjolley.com Raby    Michael
mike1071@yahoo.com Alaoui  Hajar   hajar6@hotmail.com Lemar   Anne   
anne.lemar@asu.edu Crotts  Russell Russell.Crotts@asu.edu Mazzola Dan 
dan.mazzola@sun.com Boyton  Bill    boytonb@earthlink.net

以下是sample.html文件的内容:

<html>
<body>
<table border=2>
<tr><th>Name</th><th>Username</th><th>Email</th></tr>
  <tr>
    <td>Michael Raby</td>
    <td>mraby</td>
    <td><a href="mailto:mike1071@yahoo.com">mike1071@yahoo.com</a></td>
  </tr>
  <tr>
    <td>Hajar Alaoui</td>
    <td>halaoui</td>
    <td><a href="mailto:hajar6@hotmail.com">hajar6@hotmail.com</a></td>
  </tr>
  <tr>
    <td>Anne Lemar</td>
    <td>alemar</td>
    <td><a href="mailto:anne.lemar@asu.edu">anne.lemar@asu.edu</a></td>
  </tr>
  <tr>
    <td>Russell Crotts</td>
    <td>rcrotts</td>
    <td><a href="mailto:Russell.Crotts@asu.edu">Russell.Crotts@asu.edu</a></td>
  </tr>
  <tr>
    <td>Dan Mazzola</td>
    <td>dmazzola</td>
    <td><a href="mailto:dan.mazzola@sun.com">dan.mazzola@sun.com</a></td>
  </tr>
  <tr>
    <td>Bill Boyton</td>
    <td>bboyton</td>
    <td><a href="mailto:boytonb@earthlink.net">boytonb@earthlink.net</a></td>
  </tr>
</table>
</body>
</html>

以下是sample.txt文件的内容:

Raby    Michael mike1071@yahoo.com
Alaoui  Hajar   hajar6@hotmail.com
Lemar   Anne    anne.lemar@asu.edu
Crotts  Russell Russell.Crotts@asu.edu
Mazzola Dan     dan.mazzola@sun.com
Boyton  Bill    boytonb@earthlink.net

我不是要求有人为我这样做,因为我喜欢UNIX,我想自己学习。每次我看这个HTML代码,我都会混淆#$$#&amp;出于自己。我需要帮助才能开始。

作业提示如下:

您将编写一个名为〜/ hwk12 / mk_html.awk的nawk(1)脚本,该脚本将文本文件(sample.txt和roster.txt)转换为Web浏览器可以读取的html页面。我已经在文件sample.html中给出了输出,该文件在下面再现(请注意每个级别的缩进是如何深度为两个空格):

同样,我不希望有人为我这样做。我只是混淆了文本文件中的数据如何附加到HTML表而没有实际的HTML代码。有人可以帮我开始吗?

1 个答案:

答案 0 :(得分:1)

看起来您需要在脚本中定义必要的HTML标记。 html文件的内容将是这些行:

<tr>
    <td>$first $last</td>
    <td>$username</td>
    <td><a href="mailto:$email">$email</a></td>
</tr>

这些标签定义了一个表格行。您可以使用awk解析文本文件中的变量,并使用它们填写html。另一个html标记可以作为静态文本复制粘贴到输出html文件中。

编辑:您可以执行此操作以获取名字和姓氏并打印到html文件。

last = $1
first = $2
print "  <tr>"
print "    <td>" first " " last "</td>"
print "  </tr>"

您只需将其展开即可获取电子邮件和用户名。