替换一个字符串

替换一个字符串

我有一些这种格式的字符串:

"User-Password":"aaaa","Calling-Station-Id":"bbbb","Eltex-AVPair":"cccc","h323-call-type":"dddd","Framed-IP-Address":"eeee","NAS-IP-Address":"ffff","Cisco-AVPair":["xpgk-request-type=gggg","xpgk-src-number-in=hhhh","xpgk-dst-number-in=iiii","xpgk-src-number-out=jjjj","xpgk-dst-number-out=kkkk","h323-gw-address=llll"],"Acct-Session-Id":"mmmm","User-Name":"nnnn","Called-Station-Id":"oooo","h323-gw-id":"pppp","Event-Timestamp":"qqqq","h323-conf-id":"rrrr","h323-call-origin":"ssss","NAS-Port-Type":"tttt","NAS-Port":"uuuu"

我需要把它改造成

User-Password = "aaaa"
Calling-Station-Id = "bbbb"
Eltex-AVPair = "cccc"
h323-call-type = "dddd"
Framed-IP-Address = "eeee"
NAS-IP-Address = "ffff"
Cisco-AVPair = "xpgk-request-type=gggg"
Cisco-AVPair = "xpgk-src-number-in=hhhh"
Cisco-AVPair = "xpgk-dst-number-in=iiii"
Cisco-AVPair = "xpgk-src-number-out=jjjj"
Cisco-AVPair = "xpgk-dst-number-out=kkkk"
Cisco-AVPair = "h323-gw-address=llll"
Acct-Session-Id = "mmmm"
User-Name = "nnnn"
Called-Station-Id = "oooo"
h323-gw-id = "pppp"
Event-Timestamp = "qqqq"
h323-conf-id = "rrrr"
h323-call-origin = "ssss"
NAS-Port-Type = "tttt"
NAS-Port = "uuuu"

使用 vim。

所以应该是这样的

:s/\,/\r/g|:s/^\"//g|(something_for_copying_Cisco-AVPair)|:s/\"\:/\ \=\ /g 

它分为四个部分:

  1. 将 , 转换为 \r
  2. 删除^”
  3. 复制 Cisco-AVPair 并删除方括号
  4. 将 '":' 转换为 '='

我只能写这些替换的第一部分和最后一部分。我该如何与他人相处?

答案1

您可以进行一系列搜索/替换。

:%s/,/\r/g
:%s/^"
:v/:/s/^/Cisco-AVPair":"
:%s/\v":\[?"/ \= "

这翻译成

  1. 将每个替换,为换行符
  2. 删除所有前导引号
  3. 替换行首不是包含一个:Cisco-AVPair":"
  4. 全部替换":["= "

答案2

我找到了该任务的下一个解决方案:

echo '' | jq -r 'paths(scalars) as $p|[([$p[]|tostring ]|join(".->.") ),(getpath($p)|tojson)]|join(" = ")'|grep '^RAD_REQUEST\.\-'|sed -r 's/^[A-Z_]+\.\->\.//'|sed -r 's/\.\->\.[0-9]+//'

您应该在引号之间输入请求才能使其正常工作

答案3

看起来您正在将 JSON 文档(为了解决问题而对其进行了修改,使其不再是有效的 JSON 文档)转换为 TOML。您显示的 TOML 不是相当有效,因为它包含重复的键。

假设您的原始 JSON 对象是

{
  "User-Password": "aaaa",
  "Calling-Station-Id": "bbbb",
  "Eltex-AVPair": "cccc",
  "h323-call-type": "dddd",
  "Framed-IP-Address": "eeee",
  "NAS-IP-Address": "ffff",
  "Cisco-AVPair": [
    "xpgk-request-type=gggg",
    "xpgk-src-number-in=hhhh",
    "xpgk-dst-number-in=iiii",
    "xpgk-src-number-out=jjjj",
    "xpgk-dst-number-out=kkkk",
    "h323-gw-address=llll"
  ],
  "Acct-Session-Id": "mmmm",
  "User-Name": "nnnn",
  "Called-Station-Id": "oooo",
  "h323-gw-id": "pppp",
  "Event-Timestamp": "qqqq",
  "h323-conf-id": "rrrr",
  "h323-call-origin": "ssss",
  "NAS-Port-Type": "tttt",
  "NAS-Port": "uuuu"
}

您可以使用各种工具将其转换为 TOML,yq例如https://kislyuk.github.io/yq/或与yj来自https://github.com/sclevine/yj

yq

yq -t . file

或与yj,

yj -jt <file

在这两种情况下,生成的 TOML 文档将是

User-Password = "aaaa"
Calling-Station-Id = "bbbb"
Eltex-AVPair = "cccc"
h323-call-type = "dddd"
Framed-IP-Address = "eeee"
NAS-IP-Address = "ffff"
Cisco-AVPair = [ "xpgk-request-type=gggg", "xpgk-src-number-in=hhhh", "xpgk-dst-number-in=iiii", "xpgk-src-number-out=jjjj", "xpgk-dst-number-out=kkkk", "h323-gw-address=llll",]
Acct-Session-Id = "mmmm"
User-Name = "nnnn"
Called-Station-Id = "oooo"
h323-gw-id = "pppp"
Event-Timestamp = "qqqq"
h323-conf-id = "rrrr"
h323-call-origin = "ssss"
NAS-Port-Type = "tttt"
NAS-Port = "uuuu"

相关内容