我以这种格式从Junos交换机获得输出:
Physical interface: ge-0/0/7, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 42210774942 1163342
我只需要将接口名称和丢弃的数据包值放在一行中,如下所示:
ge-0/0/7 - 1163342
我尝试了sed
和awk
的许多不同组合。我试图合并3行只获得我需要的列,但它没有用。
这就是我尝试合并线条的方式:
cat file.txt.out | awk '{getline b; getline c;printf("%s %s %s\n",$0,b,c)}'
但似乎结果线太长了,所以我无法得到所需的东西。请帮忙
输出看起来像这样(但更长):
Physical interface: ge-0/0/0, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 4582206 0
Physical interface: ge-0/0/1, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 14419826529 112564
Physical interface: ge-0/0/2, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 67593521901 1675707
Physical interface: ge-0/0/3, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 44283738671 977315
Physical interface: ge-0/0/4, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 98998665742 5065245
Physical interface: ge-0/0/5, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 56932179711 1446413
Physical interface: ge-0/0/6, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 34955222648 578513
答案 0 :(得分:2)
只用一个样本输入块就很难猜到,但这可能是你想要的:
$ awk -v RS= '{print $3, $NF}' file
ge-0/0/7, 1163342
如果没有,请发布几个输入文件块而不是一个,这样我们就可以更好地了解您正在尝试解析的内容。
鉴于您新发布的样本输入,这就是您所需要的:
$ awk '(NR%3)==1{p=$3} (NR%3)==0{print p, $NF}' file
ge-0/0/0, 0
ge-0/0/1, 112564
ge-0/0/2, 1675707
ge-0/0/3, 977315
ge-0/0/4, 5065245
ge-0/0/5, 1446413
ge-0/0/6, 578513
答案 1 :(得分:1)
完全脆弱:
$ cat test.txt
Physical interface: ge-0/0/7, Enabled, Physical link is Up
Queue counters: Queued packets Transmitted packets Dropped packets
0 N4M-Q1 0 42210774942 1163342
$ echo $(grep -Po "Physical interface: \K[^,]*" test.txt) "-" \
$(awk '!/Phys/ && !/Drop/ && NF {print $NF}' test.txt)
ge-0/0/7 - 1163342
这样的工作原理如下:
grep -Po "Physical interface: \K[^,]*" test.txt
获取Physical interface:
之后的所有文字以及逗号。awk '!/Phys/ && !/Drop/ && NF {print $NF}' test.txt
在不包含Phys
,Drop
也不为空的行中,打印最后一个字段。您还可以使用printf
更好地控制正在打印的内容:
printf "%s - %s\n" $(grep -Po "Physical interface: \K[^,]*" test.txt) $(awk '!/Phys/ && !/Drop/ && NF {print $NF}' test.txt)
答案 2 :(得分:0)
好的,经过近2天的挣扎(我是unix脚本的初学者),我得到了这个:
cat myfile | sed -n -e '/Physical/,$p' | egrep -v 'Dropped|master' | awk '{gsub(/Physical interface:|Enabled,|Physical link is|0 N4M-Q1/,"")}1' | sed '/^\s*$/d' | sed -e 's/ \+/ /g' | xargs -n 4 | awk '{ print $1" "$4 }'
产生这样的结果:
ge-0/0/0, 0
ge-0/0/1, 112564
ge-0/0/2, 1675707
ge-0/0/3, 977315
ge-0/0/4, 5065245
ge-0/0/5, 1446413
ge-0/0/6, 578513
ge-0/0/7, 1163342
ge-0/0/8, 1297
ge-0/0/9, 1604987
我意识到这个问题可能不是最优的,但至少它可以满足我的需要;)“优化”建议值得赞赏:)