Learn how to use sed to find and replace text in files in Linux.
Here are the commands.
Find and Replace Text In A File
BSD/macOS format
sed 's/testing/flying/g' test.txt
GNU/Linux format
sed -e 's/testing/flying/g' test.txt
This is the command if you want to replace multiple text.
sed -e 's/testing/flying/g' -e 's/file/book/g' test.txt
But keep in mind that these command only output to the screen. If you want to output to a file, this is the command you want to use.
sed -e 's/testing/flying/g' -e 's/file/book/g' test.txt output.txt
If you want to overwrite to the same file (test.txt), use the -i flag.
sed -i -e 's/testing/flying/g' -e 's/file/book/g' test.txt
Find and Replace Text In A Variable
Lets create a Linux variable with some value using this command.
str='I am a testing file from Japan'
And then use this command to find and replace a text in a variable.
echo $str | sed -e 's/testing/flying/g' -e 's/file/book/g'
Here is the test file you want to stage it in your Linux box as well (test.txt)
Testing file for fun and demo.
What test?
I am the testing file?
Ah I see, I as in me a testing file? Couldn't be!
Well you are a testing file.
Pfff! Testing file.
I am just give you a heads up, testing file.
Ok Jim.
#linux #ubuntu #bash #shell #script
Watch video Use sed to find and replace text in files in Linux online without registration, duration hours minute second in high quality. This video was added by user Rabi Gurung 06 February 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 522 once and liked it 5 people.