• 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 character in the beginning or end of line with matched pattern


    In my last articles I had shared the arguments with sed which can be used to perform case insensitive actions (search, replace..) in a file and to delete all blank lines from the file.

    Before starting with some command examples let us take a file sample output where we want to perform our sed operation.

    Sample File "/tmp/file" with below content
    # Port rpc.statd should listen on.
    STATD_PORT=662
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2020
    Specify callout program
    STATD_HA_CALLOUT="/usr/local/bin/foo"

    Add content at the beginning of the line






    Example 1
    Add a "#" comment hash in the beginning of line containing "STATD_PORT"
    Solution
    # sed '/STATD_PORT/ s/^/#/' /tmp/file

    # Port rpc.statd should listen on.
    #STATD_PORT=662
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2020
    Specify callout program
    STATD_HA_CALLOUT="/usr/local/bin/foo"

    To do in place replacement
    # sed -i '/STATD_PORT/ s/^/#/' /tmp/file
    Example 2
    What if the text to be matched in somewhere in the middle of the line

    Here match "callout" and add a "#" comment hash in the beginning of the line

    Solution
    # sed '/callout/ s/^/#/' /tmp/file

    # Port rpc.statd should listen on.
    STATD_PORT=662
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2020
    #Specify callout program
    STATD_HA_CALLOUT="/usr/local/bin/foo"

    To do in place replacement use below command
    # sed -i '/callout/ s/^/#/' /tmp/file
    Example 3
    You can use the same command to add in some text or word in the beginning of the line of the matched pattern, just replace the "#" with "your text"

    For eg
    # sed '/callout/ s/^/your text /' /tmp/file

    # Port rpc.statd should listen on.
    STATD_PORT=662
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2020
    your text Specify callout program
    STATD_HA_CALLOUT="/usr/local/bin/foo"




    Add content at the end of the line


    Example 1
    Add 'your text' at the end of the line which matches 'callout'
    # sed '/callout/ s/$/ your text/' /tmp/file

    # Port rpc.statd should listen on.
    STATD_PORT=662
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2020
    Specify callout program your text
    STATD_HA_CALLOUT="/usr/local/bin/foo"

    To do the same "in place" replacement
     # sed -i '/callout/ s/$/ your text/' /tmp/file
    Example 2

    Match any text starting with "STATD" and add "your text" at the end of all the matching lines
    # sed '/^STATD/ s/$/ your text/' /tmp/file

    # Port rpc.statd should listen on.
    STATD_PORT=662 your text
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2020 your text
    Specify callout program
    STATD_HA_CALLOUT="/usr/local/bin/foo" your text

    To do "in place" replacement
    # sed -i '/^STATD/ s/$/ 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: Insert character in the beginning or end of line with matched pattern sed: Insert character in the beginning or end of line with matched pattern Reviewed by Deepak Prasad on Saturday, June 24, 2017 Rating: 5

    No comments:

    Powered by Blogger.