2017-07-04 35 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,因爲這是默認值。 –