TransWikia.com

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:

set_config_object("m_lin_1_pad_agent*","io_agent_config",m_lin_1_pad_agent_cfg,0);

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!

Ask a Question

Get help from others!

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