如何在Linux中忽略输出的第一行

时间:2019-05-29 10:36:57

标签: linux bash shell scripting centos7

我正在尝试修改以下命令的输出

命令

"
[root@localhost kafka_2.11-0.9.0.0]# bin/kafka-consumer-groups.sh --zookeeper localhost:2181 --describe --group console-consumer-15158
"

输出

OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
GROUP, TOPIC, PARTITION, CURRENT OFFSET, LOG END OFFSET, LAG, OWNER
console-consumer-15158, myt2, 0, 2, 2, 0, console-consumer-15158_localhost.localdomain-1559111187575-ed44cb6f-0

我希望以上输出显示为:

GROUP = console-consumer-15158
TOPIC = myt2
PARTITION = 0
CURRENT OFFSET = 2
LOG END OFFSET = 2 
LAG = 0
OWNER = console-consumer-15158_localhost.localdomain-1559111187575-ed44cb6f-0

它应该忽略所有不必要的行,并且需要将相关字段的值放在它前面

由于我是这个Linux世界的新手,我该怎么办?

2 个答案:

答案 0 :(得分:0)

bin/kafka-consumer-groups.sh --zookeeper localhost:2181 --describe --group console-consumer-15158 --describe可能是多行结果,例如


TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID                                     HOST            CLIENT-ID
afei            0          8               8               0               consumer-1-7a46c647-8221-4aca-b6bf-ed14571fb0f1 /172.18.36.203  consumer-1
afei            4          10              10              0               consumer-1-7a46c647-8221-4aca-b6bf-ed14571fb0f1 /172.18.36.203  consumer-1
afei            1          8               8               0               consumer-1-7a46c647-8221-4aca-b6bf-ed14571fb0f1 /172.18.36.203  consumer-1
afei            3          6               6               0               consumer-1-7a46c647-8221-4aca-b6bf-ed14571fb0f1 /172.18.36.203  consumer-1
afei            2          9               9               0               consumer-1-7a46c647-8221-4aca-b6bf-ed14571fb0f1 /172.18.36.203  consumer-1

如果确定仅一行输出数据,则可能希望这样

bin/kafka-consumer-groups.sh --zookeeper localhost:2181 --describe --group console-consumer-15158 --describe >tmp.txt
awk '{print "GROUP = "$1,"\nTOPIC = "$2,"\nPARTITION ="$.....} tmp.txt

答案 1 :(得分:0)

不喜欢放弃代码,但希望新手知道新语言的技巧也是不公平的。我的解决方案是留一些bug供您查找。尽管比以前的解决方案有更多的代码行,但这是一个非常通用的解决方案。

awk(在Linux环境之外有时会别名为gawk)是一种很好的面向外壳的编程语言,需要学习。手册页确实满足了您的最大需求,尽管有些教程可能会帮助您入门。

kafa | awk -F', *' -vdebug_sw=0 ' /GROUP/ { for ( k = 1; k <= NF; ++k ) { heads[k] = $k; if ( debug_sw ) print( "READ: heads["k"]="$k ); } print_sw = 1; next; }

( print_sw ) {
         for ( k = 1; k < length(heads); ++k ) {
             if ( match( heads[k], "[A-Z ]+$" ) <= 0 ) {
                 break;     #heads are all upper case
             }
             print( heads[k] " = " $k );
         }

( print_sw ) { for ( k = 1; k < length(heads); ++k ) { if ( match( heads[k], "[A-Z ]+$" ) <= 0 ) { break; #heads are all upper case } print( heads[k] " = " $k ); }

额外的功劳:要更改以支持第二行的第二行,该行的第二行朝向文件下方?尤其是如果后面的标题比第一个标题少。

额外的功劳:当前代码查找带有所有大写字母的开头标题以进行打印。你怎么样:

  1. 不保存不打印的小写标题。
  2. 支持在不打印的一两个小写标题之后跟随其他大写标题进行打印。
  3. 更改/ GROUP /,使其适用于任何大写字母标题。在第二个print_sw测试中的重要提示。