Search & Replace in a file content with a Bash script

sed -i "" "s/<search_expression>/<replace_expression>/g" "<file_path>"Code language: Bash (bash)

On Mac:

sed -i "" "s/<search_expression>/<replace_expression>/g" "<file_path>"Code language: Bash (bash)

It’s different because MacOS requests one more parameter for backing up the original file. If you enter an extension to the empty parameter below, it backs up the original with that suffix:

sed -i ".bak" "s/<search_expression>/<replace_expression>/g" "<file_path>"Code language: Bash (bash)

This replaces the file and keeps the original in <file_path>.bak file.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.