Bash - Search and Replace in File

#!/bin/bash # create file and add content file="$(pwd)/world.txt" touch $file echo "Hello World" > $file # pull the content from the file content=$(cat ${file}) # write content back to file after search and replace search="Hello" replace="Goodbye" echo "${content/$search/$replace}" > $file # show the final result cat $file

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.