• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • sed :Replace whole line when match found


    Here our requirement is to replace a line with our content when a match is found with in the file

    Our sample file /tmp/file
    This is line one
    This is line two
    This is line three
    This is line four

    Here we will search for line having the string "two" and replace the complete line with "your text"
    # sed '/two/c\your text' /tmp/file

    This is line one
    your text
    This is line three
    This is line four

    To do in place replacement in the same file





    # sed -i '/two/c\your text' /tmp/file
    Similarly if you want to match the line having the string "three" and replace the line with "your text"
    # sed '/three/c\your text' /tmp/file
    This is line one
    This is line two
    your text
    This is line four

    To do in place replacement in the same file
    # sed -i '/three/c\your text' /tmp/file
    IMPORTANT NOTE :  Do not use this unless you are very sure the command will not impact anything else, it is always recommended to take a backup of such file where you plan to do in place replacement

    Deepak Prasad

    is a techie and an author who is still trying to survive in this IT generation with very little knowledge he has on Linux/Unix, VMware, SAN Storage, Automation, networking etc

    You can follow him on Facebook or Google+


    Do you also have something to share here?
    Join GoLinuxHub Team as an Author, Click here for more information
    sed :Replace whole line when match found sed :Replace whole line when match found Reviewed by Deepak Prasad on Sunday, June 25, 2017 Rating: 5

    No comments:

    Powered by Blogger.