How to insert the lines from a file into another file after a key word?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have one file that contains the following:



10.48.29.68 doggy
10.65.8.184 kitty
10.48.15.104 froggy


I need to insert the contents of this file into my hosts file after the line bc group in the hosts file.



The hostfile of the server already has hundreds of entries. Bc is a group here in our hosts file. I want code that reads the ip addresses and hostnames in my file above and put those entries in the hosts file below the heading bc group.



The new file above should be written in the hosts file after keyword bc group.



For example, if my hosts file contains these lines:



10.59.12.232 bc4
10.48.29.68 xy9
bc group
10.63.71.136 bc2
10.63.71.214 bc3


I need to convert the hosts file to this:



10.59.12.232 bc4
10.48.29.68 xy9
bc group
10.48.29.68 doggy
10.65.8.184 kitty
10.48.15.104 froggy
10.63.71.136 bc2
10.63.71.214 bc3


I need to append all of the lines from my first file after the line bc group in my hosts file, and then continue with the remaining lines in the hosts file.







share|improve this question






















  • Please format your question better - select the file contents and press ctrl+k. Are you wanting to create a new file, or insert into an existing one? How do we decide how to group the lines in general? Do we use all lines or only some?
    – Mark Smith
    Oct 14 '17 at 22:46











  • I think you are also going to need to provide which groups should be created. Should there be one w2k group or a w2k12r2 group and a w2k8r2, do you have w2k8r1 servers? etc...
    – Jesse_b
    Oct 14 '17 at 22:47










  • i need both ip and hostname to read from one file and then add the entry in existing file which is the hostfile of a server and already having hundreds of entries into it. like Bc is one group here and i want that code to read the ip address and hostname which is written above and put that entry in hosts file below the main heading BC group.
    – Varun Dogra
    Oct 14 '17 at 22:55










  • i just need a code to read the both ip & hostname at a time and add both entries in existing host file under/below specific heading.
    – Varun Dogra
    Oct 14 '17 at 22:57










  • no, no need to decide the group , w2k8rs are servers only, and let suppose there is only one group bc, i just need a code to read the ip address and host name from one file, and write it to another file(hosts file) below a key word BC group. so "BC group" is the main heading under which we need to add the ip address & hostname.
    – Varun Dogra
    Oct 15 '17 at 0:49















up vote
0
down vote

favorite












I have one file that contains the following:



10.48.29.68 doggy
10.65.8.184 kitty
10.48.15.104 froggy


I need to insert the contents of this file into my hosts file after the line bc group in the hosts file.



The hostfile of the server already has hundreds of entries. Bc is a group here in our hosts file. I want code that reads the ip addresses and hostnames in my file above and put those entries in the hosts file below the heading bc group.



The new file above should be written in the hosts file after keyword bc group.



For example, if my hosts file contains these lines:



10.59.12.232 bc4
10.48.29.68 xy9
bc group
10.63.71.136 bc2
10.63.71.214 bc3


I need to convert the hosts file to this:



10.59.12.232 bc4
10.48.29.68 xy9
bc group
10.48.29.68 doggy
10.65.8.184 kitty
10.48.15.104 froggy
10.63.71.136 bc2
10.63.71.214 bc3


I need to append all of the lines from my first file after the line bc group in my hosts file, and then continue with the remaining lines in the hosts file.







share|improve this question






















  • Please format your question better - select the file contents and press ctrl+k. Are you wanting to create a new file, or insert into an existing one? How do we decide how to group the lines in general? Do we use all lines or only some?
    – Mark Smith
    Oct 14 '17 at 22:46











  • I think you are also going to need to provide which groups should be created. Should there be one w2k group or a w2k12r2 group and a w2k8r2, do you have w2k8r1 servers? etc...
    – Jesse_b
    Oct 14 '17 at 22:47










  • i need both ip and hostname to read from one file and then add the entry in existing file which is the hostfile of a server and already having hundreds of entries into it. like Bc is one group here and i want that code to read the ip address and hostname which is written above and put that entry in hosts file below the main heading BC group.
    – Varun Dogra
    Oct 14 '17 at 22:55










  • i just need a code to read the both ip & hostname at a time and add both entries in existing host file under/below specific heading.
    – Varun Dogra
    Oct 14 '17 at 22:57










  • no, no need to decide the group , w2k8rs are servers only, and let suppose there is only one group bc, i just need a code to read the ip address and host name from one file, and write it to another file(hosts file) below a key word BC group. so "BC group" is the main heading under which we need to add the ip address & hostname.
    – Varun Dogra
    Oct 15 '17 at 0:49













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have one file that contains the following:



10.48.29.68 doggy
10.65.8.184 kitty
10.48.15.104 froggy


I need to insert the contents of this file into my hosts file after the line bc group in the hosts file.



The hostfile of the server already has hundreds of entries. Bc is a group here in our hosts file. I want code that reads the ip addresses and hostnames in my file above and put those entries in the hosts file below the heading bc group.



The new file above should be written in the hosts file after keyword bc group.



For example, if my hosts file contains these lines:



10.59.12.232 bc4
10.48.29.68 xy9
bc group
10.63.71.136 bc2
10.63.71.214 bc3


I need to convert the hosts file to this:



10.59.12.232 bc4
10.48.29.68 xy9
bc group
10.48.29.68 doggy
10.65.8.184 kitty
10.48.15.104 froggy
10.63.71.136 bc2
10.63.71.214 bc3


I need to append all of the lines from my first file after the line bc group in my hosts file, and then continue with the remaining lines in the hosts file.







share|improve this question














I have one file that contains the following:



10.48.29.68 doggy
10.65.8.184 kitty
10.48.15.104 froggy


I need to insert the contents of this file into my hosts file after the line bc group in the hosts file.



The hostfile of the server already has hundreds of entries. Bc is a group here in our hosts file. I want code that reads the ip addresses and hostnames in my file above and put those entries in the hosts file below the heading bc group.



The new file above should be written in the hosts file after keyword bc group.



For example, if my hosts file contains these lines:



10.59.12.232 bc4
10.48.29.68 xy9
bc group
10.63.71.136 bc2
10.63.71.214 bc3


I need to convert the hosts file to this:



10.59.12.232 bc4
10.48.29.68 xy9
bc group
10.48.29.68 doggy
10.65.8.184 kitty
10.48.15.104 froggy
10.63.71.136 bc2
10.63.71.214 bc3


I need to append all of the lines from my first file after the line bc group in my hosts file, and then continue with the remaining lines in the hosts file.









share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 22:43









don_crissti

47k15124154




47k15124154










asked Oct 14 '17 at 22:41









Varun Dogra

105




105











  • Please format your question better - select the file contents and press ctrl+k. Are you wanting to create a new file, or insert into an existing one? How do we decide how to group the lines in general? Do we use all lines or only some?
    – Mark Smith
    Oct 14 '17 at 22:46











  • I think you are also going to need to provide which groups should be created. Should there be one w2k group or a w2k12r2 group and a w2k8r2, do you have w2k8r1 servers? etc...
    – Jesse_b
    Oct 14 '17 at 22:47










  • i need both ip and hostname to read from one file and then add the entry in existing file which is the hostfile of a server and already having hundreds of entries into it. like Bc is one group here and i want that code to read the ip address and hostname which is written above and put that entry in hosts file below the main heading BC group.
    – Varun Dogra
    Oct 14 '17 at 22:55










  • i just need a code to read the both ip & hostname at a time and add both entries in existing host file under/below specific heading.
    – Varun Dogra
    Oct 14 '17 at 22:57










  • no, no need to decide the group , w2k8rs are servers only, and let suppose there is only one group bc, i just need a code to read the ip address and host name from one file, and write it to another file(hosts file) below a key word BC group. so "BC group" is the main heading under which we need to add the ip address & hostname.
    – Varun Dogra
    Oct 15 '17 at 0:49

















  • Please format your question better - select the file contents and press ctrl+k. Are you wanting to create a new file, or insert into an existing one? How do we decide how to group the lines in general? Do we use all lines or only some?
    – Mark Smith
    Oct 14 '17 at 22:46











  • I think you are also going to need to provide which groups should be created. Should there be one w2k group or a w2k12r2 group and a w2k8r2, do you have w2k8r1 servers? etc...
    – Jesse_b
    Oct 14 '17 at 22:47










  • i need both ip and hostname to read from one file and then add the entry in existing file which is the hostfile of a server and already having hundreds of entries into it. like Bc is one group here and i want that code to read the ip address and hostname which is written above and put that entry in hosts file below the main heading BC group.
    – Varun Dogra
    Oct 14 '17 at 22:55










  • i just need a code to read the both ip & hostname at a time and add both entries in existing host file under/below specific heading.
    – Varun Dogra
    Oct 14 '17 at 22:57










  • no, no need to decide the group , w2k8rs are servers only, and let suppose there is only one group bc, i just need a code to read the ip address and host name from one file, and write it to another file(hosts file) below a key word BC group. so "BC group" is the main heading under which we need to add the ip address & hostname.
    – Varun Dogra
    Oct 15 '17 at 0:49
















Please format your question better - select the file contents and press ctrl+k. Are you wanting to create a new file, or insert into an existing one? How do we decide how to group the lines in general? Do we use all lines or only some?
– Mark Smith
Oct 14 '17 at 22:46





Please format your question better - select the file contents and press ctrl+k. Are you wanting to create a new file, or insert into an existing one? How do we decide how to group the lines in general? Do we use all lines or only some?
– Mark Smith
Oct 14 '17 at 22:46













I think you are also going to need to provide which groups should be created. Should there be one w2k group or a w2k12r2 group and a w2k8r2, do you have w2k8r1 servers? etc...
– Jesse_b
Oct 14 '17 at 22:47




I think you are also going to need to provide which groups should be created. Should there be one w2k group or a w2k12r2 group and a w2k8r2, do you have w2k8r1 servers? etc...
– Jesse_b
Oct 14 '17 at 22:47












i need both ip and hostname to read from one file and then add the entry in existing file which is the hostfile of a server and already having hundreds of entries into it. like Bc is one group here and i want that code to read the ip address and hostname which is written above and put that entry in hosts file below the main heading BC group.
– Varun Dogra
Oct 14 '17 at 22:55




i need both ip and hostname to read from one file and then add the entry in existing file which is the hostfile of a server and already having hundreds of entries into it. like Bc is one group here and i want that code to read the ip address and hostname which is written above and put that entry in hosts file below the main heading BC group.
– Varun Dogra
Oct 14 '17 at 22:55












i just need a code to read the both ip & hostname at a time and add both entries in existing host file under/below specific heading.
– Varun Dogra
Oct 14 '17 at 22:57




i just need a code to read the both ip & hostname at a time and add both entries in existing host file under/below specific heading.
– Varun Dogra
Oct 14 '17 at 22:57












no, no need to decide the group , w2k8rs are servers only, and let suppose there is only one group bc, i just need a code to read the ip address and host name from one file, and write it to another file(hosts file) below a key word BC group. so "BC group" is the main heading under which we need to add the ip address & hostname.
– Varun Dogra
Oct 15 '17 at 0:49





no, no need to decide the group , w2k8rs are servers only, and let suppose there is only one group bc, i just need a code to read the ip address and host name from one file, and write it to another file(hosts file) below a key word BC group. so "BC group" is the main heading under which we need to add the ip address & hostname.
– Varun Dogra
Oct 15 '17 at 0:49











2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Try this:



sed '/^bc group$/ r file1' hostfile


This sed command copies the lines from hostfile to the output and reads (inserts) the lines of file1 after any (and all) bc group line(s) in hostfile.



To save the output in another file, append > newhostfile to the command:



sed '/^bc group$/ r file1' hostfile > newhostfile


However, I recommend this way, which makes a backup with the suffix .bak, and edits the file in place:



sed -i.bak '/^bc group$/ r file1' hostfile


If the bc group line appears more than once in the hostfile, the lines of file1 will be inserted more than once.






share|improve this answer






















  • this is really nice... i was in confusion that 'sed' command will read either ip address or hostname at a time because there is a space in between, but it's reading the whole line and appending the new hostfile. Thanks a lot
    – Varun Dogra
    Oct 15 '17 at 13:28










  • @VarunDogra You're welcome. Please accept the answer by clicking the check mark. This lets other people know that you have received a good answer to your question, and that we have helped you. Thanks!
    – RobertL
    Oct 15 '17 at 15:29

















up vote
0
down vote













It sounds like you need something like this:



INPUT_FILE='/path/to/file'
GROUP_NAME='# bc group'
HOSTS_FILE='/etc/hosts'

echo "$GROUP_NAME" >> "$HOSTS_FILE"
while read line; do

echo "$line" >> "$HOSTS_FILE"

done < "$INPUT_FILE"


This script first appends your $HOSTS_FILE with the $GROUP_NAME (Commented out). Then it loops through each line of your input file and appends them to the hosts file under your group name.






share|improve this answer




















  • using sed command as above, how can i add the new entries after a gap of n rows from Group name(Main Heading)
    – Varun Dogra
    Oct 15 '17 at 23:14










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398170%2fhow-to-insert-the-lines-from-a-file-into-another-file-after-a-key-word%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Try this:



sed '/^bc group$/ r file1' hostfile


This sed command copies the lines from hostfile to the output and reads (inserts) the lines of file1 after any (and all) bc group line(s) in hostfile.



To save the output in another file, append > newhostfile to the command:



sed '/^bc group$/ r file1' hostfile > newhostfile


However, I recommend this way, which makes a backup with the suffix .bak, and edits the file in place:



sed -i.bak '/^bc group$/ r file1' hostfile


If the bc group line appears more than once in the hostfile, the lines of file1 will be inserted more than once.






share|improve this answer






















  • this is really nice... i was in confusion that 'sed' command will read either ip address or hostname at a time because there is a space in between, but it's reading the whole line and appending the new hostfile. Thanks a lot
    – Varun Dogra
    Oct 15 '17 at 13:28










  • @VarunDogra You're welcome. Please accept the answer by clicking the check mark. This lets other people know that you have received a good answer to your question, and that we have helped you. Thanks!
    – RobertL
    Oct 15 '17 at 15:29














up vote
1
down vote



accepted










Try this:



sed '/^bc group$/ r file1' hostfile


This sed command copies the lines from hostfile to the output and reads (inserts) the lines of file1 after any (and all) bc group line(s) in hostfile.



To save the output in another file, append > newhostfile to the command:



sed '/^bc group$/ r file1' hostfile > newhostfile


However, I recommend this way, which makes a backup with the suffix .bak, and edits the file in place:



sed -i.bak '/^bc group$/ r file1' hostfile


If the bc group line appears more than once in the hostfile, the lines of file1 will be inserted more than once.






share|improve this answer






















  • this is really nice... i was in confusion that 'sed' command will read either ip address or hostname at a time because there is a space in between, but it's reading the whole line and appending the new hostfile. Thanks a lot
    – Varun Dogra
    Oct 15 '17 at 13:28










  • @VarunDogra You're welcome. Please accept the answer by clicking the check mark. This lets other people know that you have received a good answer to your question, and that we have helped you. Thanks!
    – RobertL
    Oct 15 '17 at 15:29












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Try this:



sed '/^bc group$/ r file1' hostfile


This sed command copies the lines from hostfile to the output and reads (inserts) the lines of file1 after any (and all) bc group line(s) in hostfile.



To save the output in another file, append > newhostfile to the command:



sed '/^bc group$/ r file1' hostfile > newhostfile


However, I recommend this way, which makes a backup with the suffix .bak, and edits the file in place:



sed -i.bak '/^bc group$/ r file1' hostfile


If the bc group line appears more than once in the hostfile, the lines of file1 will be inserted more than once.






share|improve this answer














Try this:



sed '/^bc group$/ r file1' hostfile


This sed command copies the lines from hostfile to the output and reads (inserts) the lines of file1 after any (and all) bc group line(s) in hostfile.



To save the output in another file, append > newhostfile to the command:



sed '/^bc group$/ r file1' hostfile > newhostfile


However, I recommend this way, which makes a backup with the suffix .bak, and edits the file in place:



sed -i.bak '/^bc group$/ r file1' hostfile


If the bc group line appears more than once in the hostfile, the lines of file1 will be inserted more than once.







share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 15 '17 at 23:43

























answered Oct 15 '17 at 2:00









RobertL

4,685523




4,685523











  • this is really nice... i was in confusion that 'sed' command will read either ip address or hostname at a time because there is a space in between, but it's reading the whole line and appending the new hostfile. Thanks a lot
    – Varun Dogra
    Oct 15 '17 at 13:28










  • @VarunDogra You're welcome. Please accept the answer by clicking the check mark. This lets other people know that you have received a good answer to your question, and that we have helped you. Thanks!
    – RobertL
    Oct 15 '17 at 15:29
















  • this is really nice... i was in confusion that 'sed' command will read either ip address or hostname at a time because there is a space in between, but it's reading the whole line and appending the new hostfile. Thanks a lot
    – Varun Dogra
    Oct 15 '17 at 13:28










  • @VarunDogra You're welcome. Please accept the answer by clicking the check mark. This lets other people know that you have received a good answer to your question, and that we have helped you. Thanks!
    – RobertL
    Oct 15 '17 at 15:29















this is really nice... i was in confusion that 'sed' command will read either ip address or hostname at a time because there is a space in between, but it's reading the whole line and appending the new hostfile. Thanks a lot
– Varun Dogra
Oct 15 '17 at 13:28




this is really nice... i was in confusion that 'sed' command will read either ip address or hostname at a time because there is a space in between, but it's reading the whole line and appending the new hostfile. Thanks a lot
– Varun Dogra
Oct 15 '17 at 13:28












@VarunDogra You're welcome. Please accept the answer by clicking the check mark. This lets other people know that you have received a good answer to your question, and that we have helped you. Thanks!
– RobertL
Oct 15 '17 at 15:29




@VarunDogra You're welcome. Please accept the answer by clicking the check mark. This lets other people know that you have received a good answer to your question, and that we have helped you. Thanks!
– RobertL
Oct 15 '17 at 15:29












up vote
0
down vote













It sounds like you need something like this:



INPUT_FILE='/path/to/file'
GROUP_NAME='# bc group'
HOSTS_FILE='/etc/hosts'

echo "$GROUP_NAME" >> "$HOSTS_FILE"
while read line; do

echo "$line" >> "$HOSTS_FILE"

done < "$INPUT_FILE"


This script first appends your $HOSTS_FILE with the $GROUP_NAME (Commented out). Then it loops through each line of your input file and appends them to the hosts file under your group name.






share|improve this answer




















  • using sed command as above, how can i add the new entries after a gap of n rows from Group name(Main Heading)
    – Varun Dogra
    Oct 15 '17 at 23:14














up vote
0
down vote













It sounds like you need something like this:



INPUT_FILE='/path/to/file'
GROUP_NAME='# bc group'
HOSTS_FILE='/etc/hosts'

echo "$GROUP_NAME" >> "$HOSTS_FILE"
while read line; do

echo "$line" >> "$HOSTS_FILE"

done < "$INPUT_FILE"


This script first appends your $HOSTS_FILE with the $GROUP_NAME (Commented out). Then it loops through each line of your input file and appends them to the hosts file under your group name.






share|improve this answer




















  • using sed command as above, how can i add the new entries after a gap of n rows from Group name(Main Heading)
    – Varun Dogra
    Oct 15 '17 at 23:14












up vote
0
down vote










up vote
0
down vote









It sounds like you need something like this:



INPUT_FILE='/path/to/file'
GROUP_NAME='# bc group'
HOSTS_FILE='/etc/hosts'

echo "$GROUP_NAME" >> "$HOSTS_FILE"
while read line; do

echo "$line" >> "$HOSTS_FILE"

done < "$INPUT_FILE"


This script first appends your $HOSTS_FILE with the $GROUP_NAME (Commented out). Then it loops through each line of your input file and appends them to the hosts file under your group name.






share|improve this answer












It sounds like you need something like this:



INPUT_FILE='/path/to/file'
GROUP_NAME='# bc group'
HOSTS_FILE='/etc/hosts'

echo "$GROUP_NAME" >> "$HOSTS_FILE"
while read line; do

echo "$line" >> "$HOSTS_FILE"

done < "$INPUT_FILE"


This script first appends your $HOSTS_FILE with the $GROUP_NAME (Commented out). Then it loops through each line of your input file and appends them to the hosts file under your group name.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 15 '17 at 1:38









Jesse_b

10.5k22659




10.5k22659











  • using sed command as above, how can i add the new entries after a gap of n rows from Group name(Main Heading)
    – Varun Dogra
    Oct 15 '17 at 23:14
















  • using sed command as above, how can i add the new entries after a gap of n rows from Group name(Main Heading)
    – Varun Dogra
    Oct 15 '17 at 23:14















using sed command as above, how can i add the new entries after a gap of n rows from Group name(Main Heading)
– Varun Dogra
Oct 15 '17 at 23:14




using sed command as above, how can i add the new entries after a gap of n rows from Group name(Main Heading)
– Varun Dogra
Oct 15 '17 at 23:14

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398170%2fhow-to-insert-the-lines-from-a-file-into-another-file-after-a-key-word%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?