SED replace task failing


There is some data on Nautilus App Server 2 in Stratos DC. Data needs to be altered in several of the files. On Nautilus App Server 2, alter the /home/BSD.txt file as per details given below:
a. Delete all lines containing word copyright and save results in /home/BSD_DELETE.txt file. (Please be aware of case sensitivity)
b. Replace all occurrence of word the to them and save results in /home/BSD_REPLACE.txt file.
Note: Let’s say you are asked to replace word to with from. In that case, make sure not to alter any words containing this string; for example upto, contributor etc.

My attempt to solve:
sed ‘/copyright/d’ ./BSD.txt - copy output and save in /home/BSD_DELETE.txt
sed ‘s/ the / them /g’ BSD.txt - copy output and save in /home/BSD_REPLACE.txt file

Can someone guide me what is wrong in my approach ?

hello @nisleshgore25
the problem here is that you are trying to copy the output of the original file after making the changes, which is not the correct method or pattern as per the question.
do it directly example:-
sudo sed -e ‘/abcd/d’ /home/XYZ.txt > /home/XYZ_DELETE.txt
this will help you to save the result directly to the BSD_DELETE.txt file which is aslo the requirement of this question.
i hope this this will solve the error of “correct pattern” .

Thank you for your feedback.
However I get the following error.

  • Make sure to replace all the occurrences of ‘the’ to ‘them’ in ‘/home/BSD_REPLACE.txt’ on stapp02.
    So it seems that the deletion task is successful even when following the same method of copying and pasting in the BSD_DELETE.txt file.
    May be there is error on doing the replacement task.
    I also attempted your suggested method, but I get permission denied.
    [steve@stapp02 ~]$ sudo sed -e ‘/copyright/d’ /home/BSD.txt > /home/BSD_DELETE.txt
    -bash: /home/BSD_DELETE.txt: Permission denied

sed ‘/copyright/d’ ./BSD.txt - copy output and save in /home/BSD_DELETE.txt
sed ‘s/ the / them /g’ BSD.txt - copy output and save in /home/BSD_REPLACE.txt file

i used these above commands, but still get an error for either of one.

Hi @nileshgore25 , I am also facing issue in completing the task. Did you got solution ?

try to do the same as root user using command $sudo su
$sed -e ‘/copyright/d’ /home/BSD.txt > /home/BSD_DELETE.txt
$sed -e ‘s/OR/AND/g’ /home/BSD.txt > /home/BSD_REPLACE.txt

  1. try removing space before and after the and them.
  2. i think you are missing -i after sed.
    -i is used to makes changes in the original file, to take a backup of the original file and make changes in it, use -i .bak

thank you for your assistance, running the commands after “sudo su” fixed the issue

The following solved the problem for me.

sudo su
sed ‘/software/d’ ./BSD.txt > /BSD_DELETE.txt
sed ‘s/ from / is /g’ BSD.txt > /BSD_REPLACE.txt