0

I am using sed to replace patterns in multiple files recursively in a directory. Simple command:

find . -name '*.md' -print0 | xargs -0 sed -i "" "s/+++/---/g"

This one replaces all +++ with --- in all files named *.md.


Now I want to do the same for something with two wildcards in MacOS:

find . -name '*.md' -print0 | xargs -0 sed -i "" 's/{{% link "\(.*\)" "\(.*\)" %}}/[\1](\2)/g'

My desired result would be a string change from

{{% link "My Link" "https://example.com" %}}

to

[My Link](https://example.com)

in all files. It **works for one occurrence per line, but not for multiple occurrences. For instance,

This {{% link "Link One" "https://domainone.com" %}} and that {{% link "Link Two" "https://domaintwo.com" %}} ...

becomes

This [Link One" "https://domainone.com" %}} and that {{% link "Link Two" "https://domaintwo.com) ...

But I want

This [Link One](https://domainone.com) and that [Link Two](https://domaintwo.com) ...

How can I fix this behavior? Would be super grateful for your help.

Robin Wieruch
  • 14,900
  • 10
  • 82
  • 107
  • 2
    `'s/{{% link "\([^"]*\)" "\([^"]*\)" %}}/[\1](\2)/g'` – Wiktor Stribiżew Aug 19 '19 at 10:52
  • @WiktorStribiżew you are a hero. I searched for this problem a while, but it was difficult to find something with the intersection of multiple occurrences per line + wildcard. I thought I would have to wait for an answer a couple of days, but thanks to you, I can just continue my work. Thank you so much! You made my day :) – Robin Wieruch Aug 19 '19 at 10:55
  • Please submit your comment as answer, so that I can mark it as the solution! – Robin Wieruch Aug 19 '19 at 10:56
  • 1
    It is a very common and well-known issue, asked almost every day, I'd prefer to close the question as a duplicate. – Wiktor Stribiżew Aug 19 '19 at 10:59

0 Answers0