博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

CalmArrow

【打好基础】全力以赴,顺其自然【每天进步一点点】
  piaoxiang.cublog.cn

关于作者
姓名:CalmArrow(lqm)
职业:硕士在读
位置:山东济南
研究:嵌入式系统设计
联系:calmarrow@gmail.com

信仰:
    1、永远保持积极向上(积极的心态,积极的思考,积极的行动),享受生活给予的一切!
    2、做正确的事,正确的做事;用心去做,做到最好!
    3、如果你觉得不幸福,那么请你把幸福的门槛降低一些,不要把幸福拒之门外。。。

目标:
    软硬结合,打好基础,提高学习能力,完善知识体系,建立核心优势。

方法:
    理论与实践相结合
    深度与广度相结合
    理解与记忆相结合

说明:
    本Blog仅供学习之用,转载文章如涉及版权,请通知。原创作品如转载,请注明出处。
|| << >> ||
我的分类


shell基础学习系列(3):关于awk的两个练习
习题1(2007-02-08)

第一个练习,你的输入是按照下列形式的行:

Username:Firstname:Lastname:Telephone number

写一个把这样的行转换成一个如下LDAP记录格式的 awk 脚本:

dn: uid=Username, dc=example, dc=com
cn: Firstname Lastname
sn: Lastname
telephoneNumber: Telephone number

建立并检查一个包含一系列测试记录的文件。

完成awk脚本文件如下:

BEGIN { FS=":" }
{ print "dn: uid=" $1 ", dc=example, dc=com" }
{ print "cn: " $2, $3 }
{ print "sn: " $3 }
{ print "telephoneNumber: " $4 }

测试:

[armlinux@lqm practice]$ awk -f testawk1 test
dn: uid=Username, dc=example, dc=com
cn: Firstname Lastname
sn: Lastname
telephoneNumber: Telephone number

awk脚本还可以更改为:

[armlinux@lqm practice]$ cat testawk
BEGIN { FS=":" }
{ print "dn: uid=" $1 ", dc=example, dc=com\ncn: " $2, $3 "\nsn: " $3 "\ntelephoneNumber: " $4 }

习题二(2007-02-08)

以下列格式为准从 Tab分割列表中建立XML风格输出:

Meaning very long line with a lot of description
 
meaning another long line
 
othermeaning    more longline
 
testmeaning     loooline, but i mean really ooong.
 

输出像这样:

<row>
<entry>Meaning</entry>
<entry>
very long line
</entry>
</row>
<row>
<entry>meaning</entry>
<entry>
long line
</entry>
</row>
<row>
<entryothermeaning</entry>
<entry>
more longline
</entry>
</row>
<row>
<entrytestmeaning</entry>
<entry>
loooline, but i mean really ooong.
</entry>
</row>

[armlinux@lqm practice]$ cat xml
Meaning very long line with a lot of description
meaning another long line
othermeaning more longline
testmeaning loooooooooooooooooooooline, but I mean really looooooooo
[armlinux@lqm practice]$ cat testxml
BEGIN { FS="\t" }
{ print "<row>" }
{ print "<entry>" $1 "</entry>" }
{ print "<entry>" }
{ print $2 }
{ print "</entry>"}
{ print "</row>" }
[armlinux@lqm practice]$ awk -f testxml xml
<row>
<entry>Meaning</entry>
<entry>
very long line with a lot of description
</entry>
</row>
<row>
<entry>meaning</entry>
<entry>
another long line
</entry>
</row>
<row>
<entry>othermeaning</entry>
<entry>
more longline
</entry>
</row>
<row>
<entry>testmeaning</entry>
<entry>
loooooooooooooooooooooline, but I mean really looooooooo
</entry>
</row>

发表于: 2007-02-08,修改于: 2007-11-19 17:00,已浏览1240次,有评论0条 推荐 投诉


网友评论
 发表评论