« 服务器日志 2004-5-21 | 首 页 | [ZT] Sort 命令集 »

2004 年 05 月 23 日

[ZT]awk: Split a string into an array

| 分类: 服务器技术 Server Tech. |

From:http://www.starlink.rl.ac.uk/star/docs/sc4.htx/node38.html

The awk function split(s,a,sep) splits a string s into an awk array a using the delimiter sep.

set time = 12:34:56
set hr = `echo $time | awk '{split($0,a,":" ); print a[1]}'` # = 12
set sec = `echo $time | awk '{split($0,a,":" ); print a[3]}'` # = 56

# = 12 34 56
set hms = `echo $time | awk '{split($0,a,":" ); print a[1], a[2], a[3]}'`
set hms = `echo $time | awk '{split($0,a,":" ); for (i=1; i<=3; i++) print a[i]}'`


The last two statements are equivalent, but the latter is more convenient for longer arrays as you specify the start index and number of elements to print. Variable hms is also an array so hms[2] is 34.

C-shell Cookbook
Starlink Document sc4
Malcolm J. Currie
1998 January 9
E-mail:ussc@star.rl.ac.uk

(END)

引用

引用本文的 URL:
http://weblog.kreny.com/cgi-bin/mt/mt-tb.cgi/50.

发表评论

(如果您还为在此成功留言,也许是因为需要管理员逐一审核留言内容后放可看到您的评论。请稍后并感谢您的耐心等待。)

阅览更多文章请访问 首页过去的存档