How to replace paired square brackets with other syntax with sed?

前端 未结 4 1631
无人及你
无人及你 2020-12-06 01:57

I want to replace all pairs of square brackets in a file, e.g., [some text], with \\macro{some text}, e.g.:

This is some [text].
Th         


        
4条回答
  •  难免孤独
    2020-12-06 02:08

    sed -e 's/\[\([^]]*\)\]/\\macro{\1}/g' file.txt
    

    This looks for an opening bracket, any number of explicitly non-closing brackets, then a closing bracket. The group is captured by the parens and inserted into the replacement expression.

提交回复
热议问题