1

I have a JSON file which contain multiple occurrence of replaceme.

I only want to substitute an occurrence based on a specific string found a few line above.

E.g.,

[...]
    "title" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }

    "title2" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }
    "title" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }

    "title2" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }
[...]

In this example, I want to substitute replaceme only for title2.

With awk 'title2/,/replaceme/' myFile I am able to locate the part of my JSON file. Do I need to execute a sed command too, but I don't know how, or if it's even possible that way?

030
  • 5,731
  • 12
  • 61
  • 107
Bastien974
  • 1,824
  • 12
  • 43
  • 61

1 Answers1

1
[vagrant@localhost ~]$ sed -e '/"title2/,/}/{ s/replaceme/title2/; }' test
    "title" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }

    "title2" : {
      "Propertie1" : "xxxx",
      "Propertie2: "title2"
    }

    "title" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }

    "title2" : {
      "Propertie1" : "xxxx",
      "Propertie2: "title2"
    }

Sources

https://stackoverflow.com/questions/268045/multi-line-search-and-replace-tool

http://sed.sourceforge.net/sedfaq4.html#s4.23.3

4.24. How do I address all the lines between RE1 and RE2, excluding the lines themselves?

Normally, to address the lines between two regular expressions, RE1 and RE2, one would do this: '/RE1/,/RE2/{commands;}'. Excluding those lines takes an extra step. To put 2 arrows before each line between RE1 and RE2, except for those lines:

     sed '1,/RE1/!{ /RE2/,/RE1/!s/^/>>/; }' input.fil

The preceding script, though short, may be difficult to follow. It also requires that /RE1/ cannot occur on the first line of the input file. The following script, though it's not a one-liner, is easier to read and it permits /RE1/ to appear on the first line:

     # sed script to replace all lines between /RE1/ and /RE2/,
     # without matching /RE1/ or /RE2/
     /RE1/,/RE2/{
       /RE1/b
       /RE2/b
       s/^/>>/
     }
     #---end of script---

Contents of input.fil: Output of sed script:
      aaa                           aaa
      bbb                           bbb
      RE1                           RE1
      aaa                           >>aaa
      bbb                           >>bbb
      ccc                           >>ccc
      RE2                           RE2
      end                           end
030
  • 5,731
  • 12
  • 61
  • 107
  • Thanks, now my issue is that there's multiple occurrence of "title2" also. I would need to substitute only on the line, found between "title2" and "replaceme". I used solution based on 1h;1!H : http://stackoverflow.com/questions/8164604/replacing-multiple-line-pattern-in-sed – Bastien974 Nov 14 '14 at 20:31
  • @Bastien974 the answer has been updated – 030 Nov 14 '14 at 20:51
  • @Bastien974 the `1h;1!H` solution is NOT specific. If the order changes that solution is useless. This answer matches specifically. – 030 Nov 14 '14 at 20:53