Linux String Substitute (sed)

Ticker

6/recent/ticker-posts

Linux String Substitute (sed)

Question :  There is some data on Nautilus App Server 3 in Stratos DC. Data needs to be altered in several of the files. On Nautilus App Server 3, alter the /home/BSD.txt file as per details given below:

a. Delete all lines containing word software and save results in /home/BSD_DELETE.txt file. (Please be aware of case sensitivity)

b. Replace all occurrence of word and to is 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.

Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ. 
So please read task carefully before executing. 
All the Best 👍

Solution:  

1. Login on   App server as per the task

thor@jump_host ~$ ssh banner@stapp03

The authenticity of host 'stapp03 (172.16.238.12)' can't be established.

ECDSA key fingerprint is SHA256:jQBzPUeKPaiyjrpKdy6e4PG/2IswOUaZYIogb7o1SHE.

ECDSA key fingerprint is MD5:1f:11:06:a6:ec:6c:f5:37:38:31:79:ad:a6:70:94:37.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'stapp03,172.16.238.12' (ECDSA) to the list of known hosts.

banner@stapp03's password:

[banner@stapp03 ~]$ sudo su -

 We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

     #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

 [sudo] password for banner:

[root@stapp03 ~]#

2. List the existing file and cat the word given in task which need to delete.

[root@stapp03 ~]# ll /home/

total 20

drwx------ 1 ansible ansible 4096 Oct 15  2019 ansible

drwx------ 1 banner  banner  4096 Jan 25  2020 banner

-rw-r--r-- 1 root    root    9919 Jun 27 12:27 BSD.txt

[root@stapp03 ~]#

[root@stapp03 ~]# cat /home/BSD.txt |grep software

3. All advertising materials mentioning features or use of this software

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

3. All advertising materials mentioning features or use of this software

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

3. All advertising materials mentioning features or use of this software

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

3. All advertising materials mentioning features or use of this software

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

3. All advertising materials mentioning features or use of this software

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

3. All advertising materials mentioning features or use of this software

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

[root@stapp03 ~]#

 

Note :- As per the task, BSD.txt file should be intact. But using "sed -i" will change the file in place.So if you want to use "sed -i" , make the copy of the BSD.txt to respective files and do the changes on newly created files.

Another option is which task required is just apply "sed" on original file and redirect the output to respective files. This won't affect the original place. As show below

3. Delete all lines containing word software and save results in /home/BSD_DELETE.txt file

[root@stapp03 ~]# sed '/\<software\>/d' /home/BSD.txt > /home/BSD_DELETE.txt

[root@stapp03 ~]#

[root@stapp03 ~]# cat /home/BSD_DELETE.txt |grep software

[root@stapp03 ~]#

 

4.  Cat the output for the word which going to replace 

[root@stapp03 ~]# cat /home/BSD.txt |grep and

Redistribution and use in source and binary forms, with or without

   notice, this list of conditions and the following disclaimer.

   notice, this list of conditions and the following disclaimer in the

   documentation and/or other materials provided with the distribution.

Redistribution and use in source and binary forms, with or without

   notice, this list of conditions and the following disclaimer.

   notice, this list of conditions and the following disclaimer in the

   documentation and/or other materials provided with the distribution.

Redistribution and use in source and binary forms, with or without

   notice, this list of conditions and the following disclaimer.

   notice, this list of conditions and the following disclaimer in the

   documentation and/or other materials provided with the distribution.

Redistribution and use in source and binary forms, with or without

   notice, this list of conditions and the following disclaimer.

   notice, this list of conditions and the following disclaimer in the

   documentation and/or other materials provided with the distribution.

Redistribution and use in source and binary forms, with or without

   notice, this list of conditions and the following disclaimer.

   notice, this list of conditions and the following disclaimer in the

   documentation and/or other materials provided with the distribution.

Redistribution and use in source and binary forms, with or without

   notice, this list of conditions and the following disclaimer.

   notice, this list of conditions and the following disclaimer in the

   documentation and/or other materials provided with the distribution.

[root@stapp03 ~]#

5.  Now replace  all occurrence of word and redirect to new file

[root@stapp03 ~]# sed 's/\band\b/is/g' /home/BSD.txt > /home/BSD_REPLACE.txt

[root@stapp03 ~]#

6.  Validate the word replaced successfully

[root@stapp03 ~]# cat /home/BSD_REPLACE.txt |grep and

[root@stapp03 ~]# cat /home/BSD_REPLACE.txt |grep is

Redistribution is use in source is binary forms, with or without

1. Redistributions of source code must retain the above copyright

   notice, this list of conditions is the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright

   notice, this list of conditions is the following disclaimer in the

   documentation is/or other materials provided with the distribution.

3. All advertising materials mentioning features or use of this software

   must display the following acknowledgement:

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

Redistribution is use in source is binary forms, with or without

1. Redistributions of source code must retain the above copyright

   notice, this list of conditions is the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright

   notice, this list of conditions is the following disclaimer in the

   documentation is/or other materials provided with the distribution.

3. All advertising materials mentioning features or use of this software

   must display the following acknowledgement:

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

Redistribution is use in source is binary forms, with or without

1. Redistributions of source code must retain the above copyright

   notice, this list of conditions is the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright

   notice, this list of conditions is the following disclaimer in the

   documentation is/or other materials provided with the distribution.

3. All advertising materials mentioning features or use of this software

   must display the following acknowledgement:

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

Redistribution is use in source is binary forms, with or without

1. Redistributions of source code must retain the above copyright

   notice, this list of conditions is the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright

   notice, this list of conditions is the following disclaimer in the

   documentation is/or other materials provided with the distribution.

3. All advertising materials mentioning features or use of this software

   must display the following acknowledgement:

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

Redistribution is use in source is binary forms, with or without

1. Redistributions of source code must retain the above copyright

   notice, this list of conditions is the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright

   notice, this list of conditions is the following disclaimer in the

   documentation is/or other materials provided with the distribution.

3. All advertising materials mentioning features or use of this software

   must display the following acknowledgement:

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

Redistribution is use in source is binary forms, with or without

1. Redistributions of source code must retain the above copyright

   notice, this list of conditions is the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright

   notice, this list of conditions is the following disclaimer in the

   documentation is/or other materials provided with the distribution.

3. All advertising materials mentioning features or use of this software

   must display the following acknowledgement:

   This product includes software developed by the <organization>.

   derived from this software without specific prior written permission.

[root@stapp03 ~]#

 7. Click on Finish & Confirm to complete the task successful

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this , 

please go through and share your comments. Like and share the knowledge



Post a Comment

6 Comments

  1. its showing permission denied after entering the sed command

    ReplyDelete
    Replies
    1. Please switch to root or run with sudo command. Feel free to connect me online support chat

      Delete
  2. /\/d what does \ and <> mean in this regular expression

    ReplyDelete
  3. what does this pattern mean s/\band\b/is/g i.e, what does \b mean

    ReplyDelete
    Replies
    1. Regarding about you question \b in a regular expression means "word boundary". can be at the beginning of a line or at the end, or between two space characters in a sentence. I would recommend for all the newbies to always take help of manual (man command ) for easy understanding If not then I am here to help your queries. Keep Learning

      Delete
  4. \b: Word boundary markers ensure that only whole words are replaced, preventing unintended changes within words.

    ReplyDelete