手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux Shell 脚本在处理字符文本方面使用实例

[日期:2012-10-06] 来源:Linux社区  作者:Linux [字体:大 中 小]

shell处理文本可能没有perl强大,但是在一些特殊用途下也是很出色的,废话少说,直接看如下实例:

[www.linuxidc.com @linux source]$cat sinadept.txt
Harry Potter:100:Magic School
Alex Jason:200:Sales
Yu Chao:300:SINA Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
Yu Jun:1000:Peking University EECS
[www.linuxidc.com @linux source]$cat sinastaff.sh
#!/bin/sh
#yuchao1@staff.sina.com.cn

IFS=:
echo "Employee Names:"
echo "---------------"
while read name empid dept
do
echo "$name is part of $dept department"
done < ~/source/sinadept.txt


[www.linuxidc.com @linux source]$sh sinastaff.sh
Employee Names:
---------------
Harry Potter is part of Magic School department
Alex Jason is part of Sales department
Yu Chao is part of SINA Product Development department
Sanjay Gupta is part of Support department
Nisha Singh is part of Sales department
Yu Jun is part of Peking University EECS department
[www.linuxidc.com @linux source]$

可以看到,根据我们特地的要求输出了确定的值了。

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款