gpt4 book ai didi

regex - bash + 如何从长输出中捕获单词

转载 作者:行者123 更新时间:2023-12-04 14:31:13 25 4
gpt4 key购买 nike

我有以下命令的以下输出

zookeeper-shell.sh 19.2.6.4  get /brokers/ids/1010
输出是
Connecting to 19.2.6.4

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://kafka1.lulu.com:6667"],"rack":"/default-rack","jmx_port":9997,"port":6667,"host":"kafka1.lulu.com","version":4,"timestamp":"1630507307906"}
主要目标是捕获机器名称 kafka1 – 从上面的输出
所以我成功地执行了以下长命令语法
zookeeper-shell.sh 119.2.6.4  get /brokers/ids/1010 | sed s'/\/\// /g' | sed s'/:/ /g' | sed s'/,/ /g' | sed s'/"/ /g' | sed s'/\./ /g'| awk '{for (i=1;i<=NF;i++) print $i}' | grep -i kafka | sort | uniq
结果是:(如预期的结果)
kafka1
情况是我对我的方法感到不舒服 ,太长了,不够优雅
我们可以得到建议(使用 awk/sed/perl 一个衬垫),从我的语法中得到更好的建议吗?

最佳答案

使用您显示的样本,请尝试关注 awk代码。由于我没有带 zookeeper 命令,因此我编写了此代码并仅根据您显示的输出对其进行了测试。

zookeeper-shell.sh 19.2.6.4  get /brokers/ids/1010 | 
awk '
/WatchedEvent state/{
found=1
next
}
found && match($0,/"PLAINTEXT:\/\/[^:]*/){
print substr($0,RSTART+13,RLENGTH-13)
}
'
说明:为以上添加详细说明 awk代码。
awk '                                         ##Starting awk program from here.
/WatchedEvent state/{ ##Checking condition if line contains WatchedEvent state
found=1 ##Then set found to 1 here.
next ##next will skip all further statements from here.
}
found && match($0,/"PLAINTEXT:\/\/[^:]*/){ ##Checking condition if found is SET then match regex "PLAINTEXT:\/\/[^:]* in match function of awk.
print substr($0,RSTART+13,RLENGTH-13) ##Printing sub string of matched regex used in match function above.
}
'

关于regex - bash + 如何从长输出中捕获单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69034663/

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