| |
 |
|
 |
 |
|
 |
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条
推荐
投诉
|
|
 |
|
 |
|  |
|
 |
|