#AWK#split函数

来源:互联网 发布:java开发养成游戏视频 编辑:程序博客网 时间:2024/05/19 19:16
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]}

Q:
name="76868&5676&435&43526&334&12312312&12321"
awk 'BEGIN {print split("$name", filearray, "&")}'
为什么是1

awk 'BEGIN {print split("76868&5676&435&43526&334&12312312&12321", filearray, "&")}'
则返回正确的结果,应该是7,有没有人解答一下?

A:
变量引用错误,这样做试试
awk 'BEGIN {print split('"\"$name\""', filearray, "&")}'

awk规定引用系统变量必须使用单引号加双引号,即'"$sysvar"'这样的格式,但是split函数也需要双引号来定界,但这个双引号又不能让sh解释,而应留给awk来解释,所以使用了\"\"组成的双引号


转载:http://blog.sina.com.cn/s/blog_4d1f40c00100r7y3.html

原创粉丝点击