Search and Replace Multiple Lines using sed
Overview
sed is my go to Linux command when I want to search and replace multiple words in a file. The syntax for the sed command is:
sed [options] 'command' file
the best way to understand it is with a few examples
Example: Search and Replace a Word in a File
replace the first occurrence of the word pattern on each line with replacement, using the s parameter
sed 's/pattern/replacement/' filename
Example: Search and Replace Multiple Words in a File
add a g to the end to replace all occurrences of the word pattern with replacement
sed 's/pattern/replacement/g' filename
now so far these just print the result. If you want to commit and actually update the file with the changes you need the -i option
Example: Search and Replace and Save the File
here we change enforcing to permissive, and using the -i flag writes the change to the /etc/selinux/config file
sed -i 's/enforcing/permissive/' /etc/selinux/config
Example: Search and Replace and make a backup file
in case you want to role back the change use -i.bak to make a copy of the original file with .bak appended
sed -i.bak 's/enforcing/permissive/' /etc/selinux/config
Summary
so there you have it, search and replace quickly and efficiently on the command line with sed. Because it uses regex there are many more automations that can be done. see the sed man pages for more ideas.