广告
返回顶部
首页 > 资讯 > 操作系统 >Linux里awk中split函数的用法小结
  • 813
分享到

Linux里awk中split函数的用法小结

小结函数Linux 2022-06-04 21:06:19 813人浏览 八月长安
摘要

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

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来解释,所以使用了"和"组成的双引号

split函数的用法

he 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]}'`

实例一:

cat a
a:b:c:d:e:f:g:h:i
使用awk将该字符串冒号两边的段输出
cat a |awk -F':' '{split($0,arr,":")}END{for(i=1;i<=NF;i++)printf("%sn",arr[i])}'
输出结果如下
a

c
d
e
f
g
h
i

--结束END--

本文标题: Linux里awk中split函数的用法小结

本文链接: https://www.lsjlt.com/news/18020.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作