-1

我有下面的shell脚本,可以根据需要产生输出。

RuleNum=$1    

cat input.txt |awk -v var=$RuleNum '$1==var {out=$1; for(i=NF;i >=0;i--)if($i~/bps/){sub("bps","",$i);out=out" "$i} print out;out=""}' 

./downup.sh 20
20 BW-IN:2560000 BW-OUT:2048000

我想要输出如下

./downup.sh 20    
256000 2048000
./downup.sh 36
2560000 2048000

下面是input.txt

20 name:abc addr:203.45.247.247/255.255.255.255 WDW-THRESH:12 BW-OUT:10000000bps BW-IN:15000000bps STATSDEVICE:test247 STATS:Enabled (4447794/0) <IN OUT>
25 name:xyz160 addr:203.45.233.160/255.255.255.224 STATSDEVICE:test160 STATS:Enabled priority:pass-thru (1223803328/0) <IN OUT>
37 name:testgrp2 <B> WDW-THRESH:8 BW-BOTH:192000bps STATSDEVICE:econetgrp2 STATS:Enabled (0/0) <Group> START:NNNNNNN-255-0 STOP:NNNNNNN-255-0
62 name:blahblahl54 addr:203.45.225.54/255.255.255.255 WDW-THRESH:5 BWLINK:cbb256 BW-BOTH:256000bps STATSDEVICE:hellol54 STATS:Enabled (346918/77) <IN OUT>
4

1 回答 1

0

sub("BW.*:", "", $i)在现有的sub().

而且cat没有必要。只需将文件名放在行尾:

awk ... input.txt

要从输出中消除规则编号,请删除out = $1;.

这是一个添加的结果,以避免在每行的开头打印一个空格:

awk -v var=$RuleNum '$1==var {for(i = NF; i >= 0; i--) if ($i ~ /bps/) {sub("bps","",$i); sub("BW.*:", "", $i); out = out delim $i; delim = OFS} print out; out = delim = ""}'
于 2012-06-29T16:50:44.970 回答