Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

12
  • If you want to actually edit the file in place (rather than just print the modified version to stdout), use perl's -i option. e.g. perl -i.bak -pe '...' Commented Jan 16, 2022 at 12:14
  • Also worth mentioning - if your input text might contain other words that contain "world" within them (e.g. "underworld" or "worldliness" or "worldwide") that you don't want to be counted and possibly replaced, use the word-boundary marker (\b) in your match regex. e.g. /\bworld\b/ instead of just /world/ Commented Jan 16, 2022 at 12:18
  • thank you for your answer but unfortunately im not allowed to neither awk or perl. How could this be done with sed. i saw there were questions here with appending so adding it after the text so im guessing it can be added before it also. Commented Jan 16, 2022 at 12:28
  • @itnera That seems like a wholly irrational and arbitrary restriction. What Unix are you using where you are not "allowed" to use standard tools? What sed can't do is counting. Therefore, any sed-only solution would be very much more complicated and quite likely also much more fragile and unmaintainable. Commented Jan 16, 2022 at 12:32
  • 1
    Your example makes it an even worse idea to use sed....and even using a perl regex isn't appropriate. json is structured text, and can't be correctly parsed or edited with regexes alone. You need a json parser. Perl has one of those (its JSON library module), as does python and many other languages. Or you can use a command-line json parser like jq Commented Jan 16, 2022 at 12:47