Mohammedz.com

For Linux and Shell scripting.

SED: change/insert/append lines after matching a pattern

12 Comments


Do you want to change/insert/append lines after matching a pattern from a file? If yes, you can use sed to do that.

—————————–
I’m pasting the relevant parts from sed manpage followed by some examples.
a \
text – Append text, which has each embedded newline preceded by a backslash.

i \
text – Insert text, which has each embedded newline preceded by a backslash.

c \
text – Replace the selected lines with text, which has each embedded newline preceded by a backslash.
—————————–

Here is an example to show you the usage. You can either use it from command line or from within shell scripts.

Description of the example: The filename.txt contains 3 lines as shown below and I’m gonna do all manipulations by matching the pattern “second line”.

# cat > filename.txt
first line
second line
third line
#

Match “second line” pattern and append “append line” into the matched address.
# sed ‘/second line/a\
append line
‘ filename.txt

Output of the above command:
first line
second line
append line
third line

Match “second line” pattern and insert “insert line” to the matched address.
# sed ‘/second line/i\
insert line
‘ filename.txt

Output of the above command:
first line
insert line
second line
third line

Match “second line” pattern and change that line with “change line”.
# sed ‘/second line/c\
change line
‘ filename.txt

Output of the above command:
first line
change line
third line

Hope this helps :-)

~mohammed

About these ads

12 thoughts on “SED: change/insert/append lines after matching a pattern

  1. Hi,

    Excellent.Nice knowledge sharing. Thanks for same.

  2. How do you this when matching a block of lines? sed -n “/start_match/,/end_match/P” test.file works and prints out the block of lines that match. However trying to add/insert a line after this pattern does not seem to work.

  3. Hi Mark, the SED man page has the answer for you.

    It divides the SED commands into 3 different categories, one which doesn’t require any address while the second one accepts zero or one address and the final one accepts address ranges. And you would find the append / insert commands within the second option, which accepts zero or one address.

  4. Hi Mohammed,

    Suppose I want to insert test some text between “first” and “line”. Is that possible using sed?

    • Hi Vinay,

      Inserting some text BETWEEN “first” and “last” doesn’t make much sense to me. Isn’t it the same as inserting some text after “first” line. Or before “last” line… If I am right with it, you can use the above mentioned append option after matching “first”. Or use insert after matching “last”.

      Let me know if I got it wrong.

      Regards,
      Mohammed.

  5. I’ve just tried to use the command which insert a string before the pattern and it works but it doesn’t made any modifications into the file. After a while I’ve found this solution:

    sed -i ‘/PATTERN/i\TEXT_TO_BE_PREPENDED’ example_of_file.txt

    • Yes, you are right. As this was not like a complete tutorial for SED, I didn’t mention that. The “-i” option is available in Linux versions, but I couldn’t find that in some older versions of solaris. So I had to use output redirect (>) to achieve the results.

      Thank you for the comments. It might help somebody who visits here…

      Regards,
      Mohammed.

    • How to use IGNORE CASE in this case??? Do you have any idea?

      • Yes, you can use an additional I (upper case i) in the command line like:

        sed ‘/pattern/Ii\TEXT_TO_BE_PREPENDED’ example_of_file.txt

        • Nope It does not work.addition I works in case of substitution operation only.

          • It works for me. Here is the sample.

            with I for case insensitive:

            [root@node2 ~]# sed '/pattern/Ii\TEXT_TO_BE_PREPENDED' example_of_file.txt
            file starts here
            TEXT_TO_BE_PREPENDED
            pATtErN
            file ends here

            without I (case sensitive)
            [root@node2 ~]# sed '/pattern/i\TEXT_TO_BE_PREPENDED' example_of_file.txt
            file starts here
            pATtErN
            file ends here

            The above example is for inset (i) option, but the same works for change (c) and append (a) options too.

            which version of sed you are using?

  6. Hello , i need to insert a file in third line after a pattern is matched, can you please let me know, and name of the file will be keep changing which would be getting inserted , so i guess i have to put a loop for i= ? pls guide me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.