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

    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


    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

    0 comments:

    Post a Comment