In markdown files, I have strings like [can-be-anything](./can-be-any#vitedevserver) and I want to change them to [can-be-anything](./can-be-any.html#vitedevserver).

I want to insert .html before #.

I tried following but it doesn't work.

link=$(sed -e 's/.*](\.\/\(.*\)#.*/\1/g' $file)
sed -i "s|](./\(.*\)#|](./${link}.html#|g" $file

The first one to get can-be-any and second one to insert with .html.

How can I do this using Bash and sed?

1 Answer 1

echo '[can-be-anything](./can-be-any#vitedevserver)' | sed 's/](\(\.\/.*\)#/](\1.html#/'
  • Since they are markdown files, I need to find a patter, ](./ and #. Otherwise it will insert .html to all #.
    – shin
    Nov 12, 2022 at 0:44
  • @shin updated answer. Nov 12, 2022 at 0:54
  • Thanks. What did I do wrong?
    – shin
    Nov 12, 2022 at 0:57
  • 1
    @shin I had tried your command and seems to work for me. I don't know if your problem might be .. In sed using a . represents any character so if you want to specify the . character you should escape it by using: \.. Nov 12, 2022 at 1:10
  • 1
    However using a \. or . should have the same effect in this case. I think you have more strings whose look like this: '[can-be-anything](@/can-be-any#vitedevserver)'. Instead of . you have any other character. Nov 12, 2022 at 1:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .