macosperlcommand-linemarkdownfind-replace

find/replace across multiple files (in macOS)


In macOS I find the below command line quite useful and efficient to find/replace across multiple files (I know there are other ways to do it --for instance, using sed-- but this one is good enough for me):

perl -pi -w -e 's/bad/good/g;' ~/Documents/*.md

But I feel completely stuck if what I need to find and replace (in .md files) contains a 'paragraph break' because the following does not work

perl -pi -w -e 's/bad
/good/g;' ~/Documents/*.md

where the word "bad" is followed by a line/paragraph break in the .md files.

Any suggestion welcome. Many thanks


Solution

  • -p implies -n, which causes the program to be executed for every line.

    A common trick is to use -0777 (aka -g in newer versions) to treat the whole file as one line.