How to Add a Line of Text to the Middle of a File Using Bash

How do I add a line of text to the middle of a file using bash?

Here is a solution using sed:

$ sed -n 'H;${x;s/^\n//;s/nameserver .*$/nameserver 127.0.0.1\n&/;p;}' resolv.conf

# Generated by NetworkManager
domain dhcp.example.com
search dhcp.example.com
nameserver 127.0.0.1
nameserver 10.0.0.1
nameserver 10.0.0.2
nameserver 10.0.0.3

How it works: first, suppress the output of sed with the -n flag. Then, for each line, we append the line to the hold space, separating them with newlines:

H

When we come to the end of the file (addressed by $) we move the content of the hold space to the pattern space:

x

If the first line in pattern space is blank we replace it with nothing.

s/^\n//

Then we replace the first line starting with nameserver by a line containing nameserver 127.0.0.1, a new line (Your version of sed may not support \n, in which case replace the n with a literal newline) and the original line (represented by &):

s/nameserver .*$/nameserver 127.0.0.1\n&/

Now we just need to print the results:

p

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

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

How to insert a text in middle of nth line of file

For your own sanity, just use awk:

$ awk 'NR==2{mid=length($0)/2; $0=substr($0,1,mid) "|" substr($0,mid+1)} 1' file
Hi Hi
Hello | Hello
This This
is Is
it it

To modify the original file you can add > tmp && mv tmp file or use -i inplace if you have GNU awk.

How to insert a string into a line in a file after a key word in bash

A pure sed solution:

sed -r "s:(THEKEYWORD):\1 $(sed ':a;N;$!ba;s/\n/ /g' test.txt) :g" insert.txt

Where;

  • test.txt is the value to be inserted, $(sed ':a;N;$!ba;s/\n/ /g' test.txt) removes any newlines from the file, so it can be inserted on the same line
  • insert.txt the text file where THEKEYWORD exists

If you wish to replace the file, use the -i option;

sed -i -r "s:(THEKEYWORD):\1 $(gsed ':a;N;$!ba;s/\n/ /g' test.txt) :g" insert.txt

As @KamilCuk pointed out, using paste -sd ' ' test.txt could be used to remove the newlines, and insert the file;

sed -r "s:(THEKEYWORD):\1 $(paste -sd ' ' test.txt) :g" insert.txt

Terminal output + sed version

Adding lines in the middle of the file using shell script

I wouldn't recommend doing this with sed/regex. (See related answer here.) That's probably why your question was edited to add the xmlstarlet tag.

If you're able to use xmlstarlet, what I would do is use the xmlstarlet tr command to do the transform with an XSLT.

In addition to an identity transform, you'd need two other templates. One to match an existing QueuePermissionSet and one to match a QueueEntry that doesn't already have a QueuePermissionSet.

Example...

xmlstarlet command line

xmlstarlet tr so.xsl input.xml > output.xml

XML Input (input.xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Manager name="Manager1" exportDate="2020-01-13-05:00" comment=" configuration from 0.0.0.0">
<ChannelSet>
<ChannelEntry>
<ChannelAttributesEntry name="/Pub/Sub/commands" TTL="0" capacity="0" EID="15" clusterWide="true" jmsEngine="true" mergeEngine="false" type="RELIABLE_TYPE"/>
<StorePropertiesEntry CacheOnReload="false" Priority="1"/>
<ChannelPermissionSet>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" getLastEID="false" purgeEvents="false" subscribe="false" publish="false" useNamedSubcription="false" groupname="Admin Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="false" publish="true" useNamedSubcription="false" groupname="Web Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="true" publish="false" useNamedSubcription="false" groupname="Messenger Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" getLastEID="false" purgeEvents="false" subscribe="false" publish="false" useNamedSubcription="false" groupname="Retry Service Group"/>
</ChannelPermissionSet>
</ChannelEntry>
<ChannelEntry>
<StorePropertiesEntry CacheOnReload="false" Priority="1"/>
<ChannelPermissionSet>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" getLastEID="false" purgeEvents="false" subscribe="false" publish="false" useNamedSubcription="false" groupname="Admin Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="true" publish="false" useNamedSubcription="true" groupname="Highland Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="false" publish="true" useNamedSubcription="false" groupname="Aus Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="false" publish="true" useNamedSubcription="false" groupname="Q Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" getLastEID="false" purgeEvents="false" subscribe="false" publish="false" useNamedSubcription="false" groupname="Retry Service Group"/>
</ChannelPermissionSet>
</ChannelEntry>
<QueueSet>
<QueueEntry>
<ChannelAttributesEntry name="/3m/receive/events" TTL="0" capacity="0" EID="0" clusterWide="true" jmsEngine="false" mergeEngine="false" type="PERSISTENT_TYPE"/>
<StorePropertiesEntry HonorCapacityWhenFull="false" CacheOnReload="false"/>
<QueuePermissionSet>
<QueueACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="true" pop="false" host="*" name="lijhlkjnlkjhlk"/>
<QueueACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="true" push="true" pop="false" host="*" name="eeff456987987"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="false" pop="true" groupname="3m Group"/>
</QueuePermissionSet>
</QueueEntry>
<QueueEntry>
<ChannelAttributesEntry name="/7m/receive/fault/events" TTL="0" capacity="0" EID="0" clusterWide="true" jmsEngine="false" mergeEngine="false" type="PERSISTENT_TYPE"/>
<StorePropertiesEntry CacheOnReload="false"/>
</QueueEntry>
<QueueEntry>
<ChannelAttributesEntry name="/Orchestration/bigData/fault/gen" TTL="0" capacity="0" EID="0" clusterWide="true" jmsEngine="false" mergeEngine="false" type="PERSISTENT_TYPE"/>
<StorePropertiesEntry CacheOnReload="false"/>
<QueuePermissionSet>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="true" pop="false" groupname="Big Data Group"/>
</QueuePermissionSet>
</QueueEntry>
</QueueSet>
</ChannelSet>
</Manager>

XSLT 1.0 (so.xsl)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" encoding="UTF-8" standalone="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="entries">
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" purge="false" peek="false" push="false" pop="false" groupname="Admin Group"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="true" pop="false" groupname="SBGroup"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="true" push="false" pop="false" groupname="SMS Admin"/>
</xsl:variable>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="QueuePermissionSet">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="$entries"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="QueueEntry[not(QueuePermissionSet)]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<QueuePermissionSet>
<xsl:copy-of select="$entries"/>
</QueuePermissionSet>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

XML Output (output.xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Manager name="Manager1" exportDate="2020-01-13-05:00" comment=" configuration from 0.0.0.0">
<ChannelSet>
<ChannelEntry>
<ChannelAttributesEntry name="/Pub/Sub/commands" TTL="0" capacity="0" EID="15" clusterWide="true" jmsEngine="true" mergeEngine="false" type="RELIABLE_TYPE"/>
<StorePropertiesEntry CacheOnReload="false" Priority="1"/>
<ChannelPermissionSet>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" getLastEID="false" purgeEvents="false" subscribe="false" publish="false" useNamedSubcription="false" groupname="Admin Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="false" publish="true" useNamedSubcription="false" groupname="Web Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="true" publish="false" useNamedSubcription="false" groupname="Messenger Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" getLastEID="false" purgeEvents="false" subscribe="false" publish="false" useNamedSubcription="false" groupname="Retry Service Group"/>
</ChannelPermissionSet>
</ChannelEntry>
<ChannelEntry>
<StorePropertiesEntry CacheOnReload="false" Priority="1"/>
<ChannelPermissionSet>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" getLastEID="false" purgeEvents="false" subscribe="false" publish="false" useNamedSubcription="false" groupname="Admin Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="true" publish="false" useNamedSubcription="true" groupname="Highland Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="false" publish="true" useNamedSubcription="false" groupname="Aus Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" getLastEID="true" purgeEvents="false" subscribe="false" publish="true" useNamedSubcription="false" groupname="Q Group"/>
<ChannelGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" getLastEID="false" purgeEvents="false" subscribe="false" publish="false" useNamedSubcription="false" groupname="Retry Service Group"/>
</ChannelPermissionSet>
</ChannelEntry>
<QueueSet>
<QueueEntry>
<ChannelAttributesEntry name="/3m/receive/events" TTL="0" capacity="0" EID="0" clusterWide="true" jmsEngine="false" mergeEngine="false" type="PERSISTENT_TYPE"/>
<StorePropertiesEntry HonorCapacityWhenFull="false" CacheOnReload="false"/>
<QueuePermissionSet>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" purge="false" peek="false" push="false" pop="false" groupname="Admin Group"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="true" pop="false" groupname="SBGroup"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="true" push="false" pop="false" groupname="SMS Admin"/>
<QueueACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="true" pop="false" host="*" name="lijhlkjnlkjhlk"/>
<QueueACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="true" push="true" pop="false" host="*" name="eeff456987987"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="false" pop="true" groupname="3m Group"/>
</QueuePermissionSet>
</QueueEntry>
<QueueEntry>
<ChannelAttributesEntry name="/7m/receive/fault/events" TTL="0" capacity="0" EID="0" clusterWide="true" jmsEngine="false" mergeEngine="false" type="PERSISTENT_TYPE"/>
<StorePropertiesEntry CacheOnReload="false"/>
<QueuePermissionSet>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" purge="false" peek="false" push="false" pop="false" groupname="Admin Group"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="true" pop="false" groupname="SBGroup"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="true" push="false" pop="false" groupname="SMS Admin"/>
</QueuePermissionSet>
</QueueEntry>
<QueueEntry>
<ChannelAttributesEntry name="/Orchestration/bigData/fault/gen" TTL="0" capacity="0" EID="0" clusterWide="true" jmsEngine="false" mergeEngine="false" type="PERSISTENT_TYPE"/>
<StorePropertiesEntry CacheOnReload="false"/>
<QueuePermissionSet>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="true" purge="false" peek="false" push="false" pop="false" groupname="Admin Group"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="true" pop="false" groupname="SBGroup"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="true" push="false" pop="false" groupname="SMS Admin"/>
<QueueGroupACLEntry listACLEntries="false" modifyACLEntries="false" fullControl="false" purge="false" peek="false" push="true" pop="false" groupname="Big Data Group"/>
</QueuePermissionSet>
</QueueEntry>
</QueueSet>
</ChannelSet>
</Manager>

Add text to file at certain line in Linux

You can use sed to solve this:

sed "15i avatar" Makefile.txt

or use the -i option to save the changes made to the file.

sed -i "15i avatar" Makefile.txt

To change all the files beginning that start Makefile:

sed "15i avatar" Makefile*

Note: In the above 15 is your line of interest to place the text.

Append a file in the middle of another file in bash

Could you please try following and let me know if this helps you.

awk 'FNR==3{system("cat file2.txt")} 1' file1.txt

Output will be as follows.

I
am
a
black
dog
named
Cookie

Explanation: Checking here if line number is 3 while reading Input_file named file1.txt, if yes then using system utility of awk which will help us to call shell's commands, then I am printing the file2.txt with use of cat command. Then mentioning 1 will be printing all the lines from file1.txt. Thus we could concatenate lines from file2.txt into file1.txt.

Insert line in the middle of file with standard unix tools

Perhaps you are looking for sed's r command?

sed '123r file.txt' main.txt

inserts the contents of file.txt at line 123 of main.txt, printing everything to standard output.

(If your sed has the -i option, you can make it modify main.txt directly; otherwise, it will not modify its input files.)

Insert text in the middle of a text file after the first occurence of a search string using sed

You could say:

sed '/Hello/{s/.*/&\nSomething on the next line/;:a;n;ba}' filename

in order to insert a line after the first occurrence of the desired string, e.g. Hello as in your question.

For your sample data, it'd produce:

Hello John, How are you?
Something on the next line
....
....
....
Hello Mary, How are you doing?


Related Topics



Leave a reply



Submit