2017-07-04 56 views
-1

我找不到解析已由awk检索的字段的正确方法。由awk捕获的字段上的awk

用awk我收集,看起来像第n个元素:

bbr:(bw:492.2Mbps,mrtt:0.412,pacing_gain:1.25,cwnd_gain:2) 

我想再次解析为了这个第n个字段来收集数据:

$1 = 492.2; 
$2 = 0.41; 
$3 = 1.25; 
$4 = 2; 

如果你有任何建议,你会很高兴听到它

G.

+1

AWK具有'分裂()'函数。检查手册页了解它是如何工作的。 – Kent

+1

请发布产生该输出的'awk'命令 – hek2mgl

+0

谢谢Kent,那是我一直在寻找的功能。 –

回答

1

在这种情况下的S特林存储到file,读取$0,清洗,split编散列a和outputed在for循环:

$ awk '{gsub(/^.*\(|\)$/,"");n=split($0,a,/[:,]/);for(i=2;i<=n;i+=2)print a[i]}' file 
492.2Mbps 
0.412 
1.25 
2 

解释:

gsub(/^.*\(|\)$/,"")  # clean outside of parentheses, including them 
n=split($0,a,/[:,]/)  # split to a 
for(i=2;i<=n;i+=2)  # further process every other element in a 
    print a[i]   # you could remove the Mbps and what not here 
+1

谢谢詹姆斯,那正是我需要的。 –

+1

关于该脚本没有任何特定的GNU awk。另外,你不需要gsub()的',$ 0' arg,因为这是默认值。 –