How do I search and replace a common, but different, phrase to a single phrase in multiple files from the terminal.

For instance, I want to change the following:

File1, File2, File3:
--Link2-- --Link2-- --Link2--
--Link3-- --Link3-- --Link3--
--Link4-- --Link4-- --Link4--




Depending on whether you could represent the "common but different phrases" by regex, you could use the following from the folder containing the files:

Search n replace with Perl
$ perl -p -i -e 's/search_for/replace_with/g' filename(s)

  • -e is for "execute" – It allows you to define Perl code to be executed by the compiler
  • -p is for "process" – It adds a loop around your -e code so that it is applied to each line of any specified files
  • -i creates a backup file before making your changes.
  • -i.bak suffixes .bak to the filename. If -i has no arguments, the file names don’t change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.