CyberKeeda In Social Media

Sed Command to find and delete string from a file.




Scenario-:

So I have a huge host entry and configuration file named as hostname.cfg and a part of the content is shown below

 #cat hostname.cfg

        host_name                mixhost44x
        alias                         test machine
        contact_groups          admins
        hostgroups                mix-Users

Here i have a requirement to delete "  mixhost44x " from the file hostname.cfg.

Syntax  -:  sed 's/word-to-find//g' yourfile.txt > output.txt

So using the above command i can use it as.

                   sed 's/ mixhost44x//g' hostname.cfg > output.txt

  • Sed with backup

         # sed -i.bak 's/ mixhost44x//g' hostname.cfg 

  • Sed without backup 
          # sed -i 's/ mixhost44x//g' hostname.cfg 

  • Sed to find and delete lines starting with a pattern 

          # sed -i '/^your_pattern_or_word/d' hostname.cfg 
For Wild character like [ , use the below one liner
               sed -i '/^[[]/d' /tmp/iplists.txt






    No comments:

    Post a Comment

    Designed By Jackuna