shell脚本返回值

时间:2012-06-29 15:15:51

标签: awk

我有下面的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>

1 个答案:

答案 0 :(得分: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 = ""}'