• 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.

  • Did you find what you needed?
    Show your support and gratitude by making a contribution!!
    Life always offers you a second chance ... Its called tomorrow !!!

    Saturday, June 24, 2017

    sed: insert word or text after match in middle of the line

    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
    # 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
    # echo "This is test" |sed 's/\<This\>/& not/'
    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.
    # 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

    Follow the below links for more tutorials

    How to find the path of any command in Linux
    How to configure a Clustered Samba share using ctdb in Red Hat Cluster
    How to delete an iscsi-target from openfiler and Linux
    How to perform a local ssh port forwarding in Linux
    How to use yum locally without internet connection using cache?
    What is umask and how to change the default value permanently?
    Understanding Partition Scheme MBR vs GPT
    How does a successful or failed login process works in Linux
    How to find all the process accessing a file in Linux
    How to exclude multiple directories from du command in Linux
    How to configure autofs in Linux and what are its advantages?
    How to resize software raid partition in Linux
    How to configure Software RAID 1 mirroring in Linux
    How to prevent a command from getting stored in history in Linux


    Post a Comment