• 请不要在回答技术问题时复制粘贴 AI 生成的内容
css3
V2EX  ›  程序员

替换文本,遇到相同的,该怎么处理

  •  
  •   css3 · Apr 18, 2019 · 2395 views
    This topic created in 2757 days ago, the information mentioned may be changed or developed.

    如下有个 json 文件,想把其中 abc 节点中的 IP 给替换成 192.168.1.2, 如何替换?

    • 只想替换 abc 节点的 IP,不能把 def 节点的也替换了
    • 用 a nsible 怎么替换?
    • 如果用不了 an sible,用 sed 如何替换?
    abc":{
      "a":true,
      "IP":"192.168.1.1",
      "c":10011,
      "d":5,
      "e":5
           },
    
    "def": {
    	"a": true,
    	"Server": [{
    		"Is": false,
    		"IP": "192.168.1.1",
    		"Port": 5577
    	}]
    },
    
    "ghi": {
    	"a": true,
    	"ka": {
    		"ServerIP": "192.168.11.236",
    		"SPort": 9092,
    		"Top": "ifaas-secondStructure",
    		"Gr": "ifaas-engine",
    		"Us": "",
    		"Pa": "",
    		"Sa": false
    	},
    
    
    7 replies  •  2019-04-19 13:06:05 +08:00
    ly4572615
    ly4572615
    Apr 18, 2019
    1
    sed -n '/^ "IP"/p' test
    css3
    css3
    OP
    Apr 18, 2019
    2
    @ly4572615 假如文件名是 test

    sed -n '/^ "192.168.1.1"/192.168.1.2' test ?????这个意思吗
    ly4572615
    ly4572615
    Apr 18, 2019
    3
    test 没错,可是你关键字没理解
    truehyp
    truehyp
    Apr 18, 2019
    4
    sed -n ":a;p;/\"abc\":{/{:c;n;/\"IP\":/{s/192.168.1.1/192.168.1.2/;ba};p;bc}" txt
    geelaw
    geelaw
    Apr 18, 2019 via iPhone❤️1
    5
    为什么不能用自然的方法解决呢?自然的方法是把文件解析好之后做结构化的操作,再序列化成文本。

    不要指望不能处理 CF 语言的程序处理 CF 语言。用一大堆 adhoc 的性质去操作,虽然可以对目前手上的文件有效,但并不是长远的策略,每次先观察文件的特殊结构再写一段新的代码去处理,和手工处理只不过是五十步和百步的区别。
    css3
    css3
    OP
    Apr 19, 2019 via iPhone
    7
    @onetown 官方文档没有找到我先要的解决办法呀😭
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1391 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 18:57 · PVG 02:57 · LAX 10:57 · JFK 13:57
    ♥ Do have faith in what you're doing.