• 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: insert word or text after match in middle of the line (search and append a string before or after match)


    I have a file where I want to search for a particular string and append some text after the match in the same line..

    For eg
    This is test
    Here it must become
    This is not test
    Command:
    # echo "This is test" |sed 's/\<is\>/& not/'
    This is
    not test

    NOTE: Here since in the same line I had "is" twice i.e. once in "This" and another "is" so I have used < > to specifically look for "is" and nothing else

    You can also append a text after "This"





    For eg:
    This is test
    Here it must become
    This not is test
    Command:
    # echo "This is test" |sed 's/\<This\>/& not/'
    This
    not is test

    How to do "in place" replacement in a file?




    This can be done using "-i" flag with sed command as shown in below example.
    Command:
    # sed -i 's/\<is\>/& not/' /tmp/filename
    This will do the magic in the same 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: insert word or text after match in middle of the line (search and append a string before or after match) sed: insert word or text after match in middle of the line (search and append a string before or after match) Reviewed by Deepak Prasad on Saturday, June 24, 2017 Rating: 5

    No comments:

    Powered by Blogger.