Linux String Substitute Task:

https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/

sed -i ‘s/old-text/new-text/g’ input.txt

for our example:
I edited the script as below.

sudo sed ‘s/Random/Sonar/g’ /root/nautilus.xml

Though it said the replacing string is a success, however, I was not confident if this task was expected only this much change.

where did I go wrong?

Have you cross checked if all the occurance of Random is replaced with Sonar in the nautilus.xml file?

yes I did, all occurrences of Random were changed to Sonar.

how could that be possible, as per this command, only Random can be replaced to Sonar right?
sudo sed ‘s/Random/Sonar/g’ /root/nautilus.xml

Hello,

The command sudo sed 's/Random/Sonar/g' /root/nautilus.xml will not actually append/save the changes into target file. You should have used sed command with -i option which is used to actually insert the changes into target file.

Please go through the man pages of sed command for more details.

Thanks
Inderpreet

however, when I re-opened the file for verifying purpose, I was able to see all instances of Random updated with Sonar.
so this would have been a successful task completion?
sudo sed -i ‘s/Random/Sonar/g’ /root/nautilus.xml

Hi,

As per screenshot and details you shared the command you used is sudo sed ‘s/Random/Sonar/g’ /root/nautilus.xml and it was supposed to display the expected output but it won’t save the results into target file so it was expected to fail.

You might be assigned this task again in future you so can give it a try again :slight_smile:

Thanks
Inderpreet

Thanks again for the information.
Eagerly waiting for starting with failed tasks.
happy weekend :slight_smile:

thanks for the solution…faced same issue.
will try the solution in next assignment.

I was able to complete the task using this resource.

It took me a few tries though to get the sed command to work. I was not able to cat the xml file at first or run the sed command. Strange. Maybe, I had too much coffee today. :laughing:

1 Like


Hi All i have replaced the Sample string with cloud on /root/nautilus.xml still my task failed.

command used is sudo sed -i ‘s/Sample/cloud/g’ /root/nautilus.xml.
kindly let me know what mistake i have made.

Hello, @kumarpuvvadi
I believe we can do compare with “Cloud” or “cloud”.

Hey guys,

follow this

sudo sed -i ‘s/String/Software/g’ /home/BSD.txt
(old text/new text)