gpt4 book ai didi

linux - 在 linux 中过滤文本输出

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:09:13 24 4
gpt4 key购买 nike

我以这种格式从 Junos Switches 获得输出:

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

我只需要在一行中包含接口(interface)名称和丢弃的数据包值,如下所示:

ge-0/0/7 - 1163342

我尝试了 sedawk 的多种不同组合。我试图合并 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

最佳答案

仅凭一 block 样本输入很难猜测,但这可能就是您想要的:

$ awk -v RS= '{print $3, $NF}' file
ge-0/0/7, 1163342

如果没有,请发布您的输入文件的几个 block 而不是一个,这样我们可以更好地了解您要解析的内容。

鉴于您新发布的样本输入,这就是您所需要的:

$ 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

关于linux - 在 linux 中过滤文本输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30983202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com