Issue
I know many similar questions exist, but I couldn't figure this particular pattern out and hence posting here (after spending lot of time and giving up). Please help :)
There's a string that I need to match and replace in all files in a given directory.
What it looks:
tag:
["https://example.com"]
What it should be converted to:
tag: "https://example.com"
I need to match the patterns: tag \n [ and "] and replace them from all files.
This is what I tried but it didn't work:
find . -type f -name "*.md" -print0 | xargs -0 sed -i '' -e 's/tag: \\n \[\"/tag: /g'
find . -type f -name "*.md" -print0 | xargs -0 sed -i '' -e 's/\"\]/\"/g'
Solution
sed -e '/^tag: $/!b' -e '$b' -e 'N;s/\n *\["/"/;s/"]/"/' file
Tip: don't use -i
until edits are correct.
Answered By - rowboat