gpt4 book ai didi

linux - awk 在 bash 脚本中使用引号和空格

转载 作者:IT王子 更新时间:2023-10-29 00:58:10 32 4
gpt4 key购买 nike

我在接收到的 snmp 陷阱的 bash 变量集中有以下输出:

echo $var

Nov 27 16:20:34 witness logger: trap: vars: DISMAN-EVENT-MIB::sysUpTimeInstance = 0:6:10:29.06,  SNMPv2-MIB::snmpTrapOID.0 = SNMPv2-SMI::enterprises.11.2.29.2.90.0.10000002, SNMPv2 SMI::enterprises.11.2.29.2.90.1 = "Finished Number", SNMPv2-SMI::enterprises.11.2.29.2.90.2 = "Filter Cirteria: [called='3333']", SNMPv2-SMI::enterprises.11.2.29.2.90.3 = "Cleared", SNMPv2     SMI::enterprises.11.2.29.2.90.4 = "major Over-Flow alert on Finished Number for ['3333']", SNMPv2 SMI::enterprises.11.2.29.2.90.5 = "The Corresponding Metric Value is: 0.5", SNMPv2- SMI::enterprises.11.2.29.2.90.6 = "Over-Flow", SNMPv2-SMI::enterprises.11.2.29.2.90.7 = "Tue Nov 27 16:20:05 CET 2012" 

我试图在变量中获得以下输出:

var1 = "Tue Nov 27 16:20:05 CET 2012"
var2 = "Finished Number"
var3 = "The Corresponding Metric Value is: 0.5"
var4 = "Cleared"
var5 = "major Over-Flow alert on Finished Number for ['3333']"

我正在考虑通过 awk 来做这件事

基于 snmp OID:enterprises.11.2.29.2.90.4、enterprises.11.2.29.2.90.5、11.2.29.2.90.6 等...

但似乎不能只提取引用内容“”的内容

最佳答案

似乎你想匹配双引号内的所有字符串,使用 grep 最简单:

$ echo $var | grep -o '"[^"]*"'

"Finished Number"
"Filter Cirteria: [called=3333]"
"Cleared"
"major Over-Flow alert on Finished Number for [3333]"
"The Corresponding Metric Value is: 0.5"
"Over-Flow"
"Tue Nov 27 16:20:05 CET 2012"

解释:

-o 只打印行中匹配的部分。

"     # Match opening double quote
[^"]* # Match anything not a double quote
" # Match closing double quote

希望这可以帮助您入门。

关于linux - awk 在 bash 脚本中使用引号和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13588188/

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