Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Inserting a line in a file at a specific location

Tags:

linux

bash

I am writing a script that will require me to add lines in a specific part of a config file. For example

Before:

ServerActors=IpServer.UdpServerUplink MasterServerAddress=unreal.epicgames.com MasterServerPort=27900 ServerActors=IpServer.UdpServerUplink MasterServerAddress=master0.gamespy.com MasterServerPort=27900 ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.mplayer.com MasterServerPort=27900 ServerActors=UWeb.WebServer 

After:

ServerActors=IpServer.UdpServerUplink MasterServerAddress=unreal.epicgames.com MasterServerPort=27900 ServerActors=IpServer.UdpServerUplink MasterServerAddress=master0.gamespy.com MasterServerPort=27900 ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.mplayer.com MasterServerPort=27900 ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.qtracker.com MasterServerPort=27900 ServerActors=UWeb.WebServer 

As you can see there is a new line added. How can my bash script insert the line? I'm guessing I will need to use sed.

like image 401
dgibbs Avatar asked Aug 16 '13 11:08

dgibbs


People also ask

How do I add a line to a file in bash Linux?

Using '>>' with 'echo' command appends a line to a file. Another way is to use 'echo,' pipe(|), and 'tee' commands to add content to a file.

How do I add text to a file in bash?

In Linux, to append text to a file, use the >> redirection operator or the tee command.

How do you add a line to a file in Unix?

There are different ways to insert a new line in a file using sed, such as using the “a” command, the “i” command, or the substitution command, “s“. sed's “a” command and “i” command are pretty similar.


2 Answers

If you want to add a line after a specific string match:

$ awk '/master.mplayer.com/ { print; print "new line"; next }1' foo.input ServerActors=IpServer.UdpServerUplink MasterServerAddress=unreal.epicgames.com MasterServerPort=27900 ServerActors=IpServer.UdpServerUplink MasterServerAddress=master0.gamespy.com MasterServerPort=27900 ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.mplayer.com MasterServerPort=27900 new line ServerActors=UWeb.WebServer 
like image 198
Adrian Frühwirth Avatar answered Sep 22 '22 17:09

Adrian Frühwirth


You can use something like this:

Note that the command must be entered over multiple lines because sed does not allow coding a newline with "\n" or the Ctrl-V/Ctrl-M key combination like some tools. The backslash says "Ignore my hitting the return key, I'm not done with my command yet".

sed -i.bak '4i\ This is the new line\ ' filename 

This should do the trick (It will insert it between line 3 and 4).

If you want to put this command itself into a shell script, you have to escape the backslashes so they don't get eaten by bash and fail to get passed to sed. Inside a script, the command becomes:

sed -i.bak '4i\\ This is the new line\\ ' filename 
like image 39
MPrazz Avatar answered Sep 23 '22 17:09

MPrazz