• 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 multiple lines before or after pattern match

    In my last articles I had shown you the arguments to be used with sed to add or append any character in the beginning or end of the line and to ignore whitespace while grepping for a pattern so that the grep does not fails if there is an extra whitespace character between two words or sentence.

    Here our requirement is to add multiple lines after a pattern is matched in a newline which can be before or after the match based upon the requirement

    Below is our sample file /tmp/file
    This is line one
    This is line two
    This is line three
    This is line four

    Here in we have to put below content
    your text 1
    your text 2
    your text 3

    as per below requirement
    1. one line before the string is matched
    2. one line after the string is matched

    Example 1
    Here we will add our content one line before the string is matched

    Solution
    # sed '/This is line two/i\your text 1\nyour text 2\nyour text 3' /tmp/file

    This is line one
    your text 1
    your text 2
    your text 3

    This is line two
    This is line three
    This is line four

    To do in place replacement
    # sed -i '/This is line two/i\your text 1\nyour text 2\nyour text 3' /tmp/file
    Example 2
    Here we will add our content one line after the string is matched

    Solution
    # sed '/This is line two/a\your text 1\nyour text 2\nyour text 3' /tmp/file

    This is line one
    This is line two
    your text 1
    your text 2
    your text 3

    This is line three
    This is line four

    To do in place replacement
    # sed -i '/This is line two/a\your text 1\nyour text 2\nyour text 3' /tmp/file





    Example 3
    You can add your content to a file for eg create a file /tmp/mydata with our content i.e.
    your text 1
    your text 2
    your text 3

    Now you can use below 'sed' command to append the content of "mydata" to /tmp/file"
    # sed '/This is line two/r /tmp/mydata' /tmp/file

    This is line one
    This is line two
    your text 1
    your text 2
    your text 3

    This is line three
    This is line four

    To do in place replacement
    # sed -i '/This is line two/r /tmp/mydata' /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
    I hope the article was useful.
    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?

    Become an author and be a part of our GoLinuxHub Team, Click here for more information
    sed: Insert multiple lines before or after pattern match sed: Insert multiple lines before or after pattern match Reviewed by Deepak Prasad on Sunday, June 25, 2017 Rating: 5

    No comments:

    Powered by Blogger.