首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤linux中的文本输出

过滤linux中的文本输出
EN

Stack Overflow用户
提问于 2015-06-22 14:55:46
回答 3查看 407关注 0票数 0

我以这样的格式从Junos交换机获得输出:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
ge-0/0/7 - 1163342

我尝试了许多sedawk的不同组合。我试图合并3行,只得到我需要的列,但没有工作。这就是我试图合并行的方式:

代码语言:javascript
复制
cat file.txt.out | awk '{getline b; getline c;printf("%s %s %s\n",$0,b,c)}'

但结果似乎太长了,所以我无法得到所需的东西。请帮帮忙

输出如下(但长得多):

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-06-22 15:32:17

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

代码语言:javascript
复制
$ awk -v RS= '{print $3, $NF}' file
ge-0/0/7, 1163342

如果不是,发布几块您的输入文件,而不是仅仅一个块,这样我们就可以更好地了解您要解析的是什么。

考虑到您新发布的示例输入,您需要这样做:

代码语言:javascript
复制
$ 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
票数 2
EN

Stack Overflow用户

发布于 2015-06-22 15:02:39

完全脆弱:

代码语言:javascript
复制
$ 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.txtPhysical interface:之后获取所有文本,并达到逗号。
  • 在那些不包含awk '!/Phys/ && !/Drop/ && NF {print $NF}' test.txtDrop或空的行中,打印最后一个字段。

您还可以使用printf对正在打印的内容进行更多的控制:

代码语言:javascript
复制
printf "%s - %s\n" $(grep -Po "Physical interface: \K[^,]*" test.txt) $(awk '!/Phys/ && !/Drop/ && NF {print $NF}' test.txt)
票数 1
EN

Stack Overflow用户

发布于 2015-06-23 11:37:35

好的,经过近两天的挣扎(我是unix脚本的初学者),我得到了以下内容:

代码语言:javascript
复制
 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 }'

产生这样的结果:

代码语言:javascript
复制
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

我意识到这个想法可能不是最理想的,但至少它能满足我的需要;)“优化”的建议是值得赞赏的:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30983202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档