我以这样的格式从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发布于 2015-06-22 15:32:17
仅用一个样本输入就很难猜测,但这可能是您想要的:
$ 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发布于 2015-06-22 15:02:39
完全脆弱:
$ 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、Drop或空的行中,打印最后一个字段。您还可以使用printf对正在打印的内容进行更多的控制:
printf "%s - %s\n" $(grep -Po "Physical interface: \K[^,]*" test.txt) $(awk '!/Phys/ && !/Drop/ && NF {print $NF}' test.txt)发布于 2015-06-23 11:37:35
好的,经过近两天的挣扎(我是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我意识到这个想法可能不是最理想的,但至少它能满足我的需要;)“优化”的建议是值得赞赏的:)
https://stackoverflow.com/questions/30983202
复制相似问题