Insert a line after match using sed

Ask Ubuntu Asked by Tejas Kulkarni on January 3, 2022

I want to insert a new line after this line:


Can you please guide me on how I can achieve that using the sed command?

One Answer

Brian Z's comment is a good suggestion. Indeed, using the a command in sed will append a string after the match.

However, if you directly use the string in your question as the matching string, you will find that nothing will happen. The problem is the * character inside your string, which should be escaped, since sed uses this character for regex match. In sed you can escape a character by using a backslash () in front of it. You can find some more info about the characters that should be escaped in this U&L question.

So, the sed command you need to run in order to append a string newstring in a file called file.txt after matching your string is (notice the escaped *):

sed -i -e '/set_config_object("m_lin_1_pad_agent*","io_agent_config",m_lin_1_pad_agent_cfg,0);/ a newstring' file.txt

In the above command -i is used to change the file in place. If you want to preview the changes first and ensure that the command works as it should, run the command without the -i flag.

Answered by BeastOfCaerbannog on January 3, 2022

Add your own answers!

Related Questions

Ubuntu menu won’t close after opening

0  Asked on January 27, 2021 by shadowblitz16


How do I install Psiphon on Ubuntu 14.04 LTS (Step by Step)

4  Asked on January 25, 2021 by rahul-raj-rai


How to limit user access to selected applications?

2  Asked on January 25, 2021 by jorrit


kernel panic-not syncing: VFS: unable to mount root fs on unknown block(0,0)

1  Asked on January 24, 2021 by donttakemeseriously


How to torrify the Ubuntu host?

0  Asked on January 23, 2021 by k-madhura-nadh-12a


Mouse work ONLY after suspend

0  Asked on January 23, 2021 by idir


“cannot find -lmpi*” when compiling openmpi code

3  Asked on January 22, 2021 by thomas-ding


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP