Wonder given there is an obvious pattern to it can a script (!) one line and ensure even if copy to the dot file still ok to clean it up … if it is so common why not use computer to check and clean it.
For the job itself I wonder whether you can do a test first … eg instead of reading and analysis of dotfile create one .brewpath if file exist path should be there etc or even have a program to do this test and prompt user to run the script.