I have one file that contains the following: doggy kitty 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: bc4 xy9
bc group bc2 bc3

I need to convert the hosts file to this: bc4 xy9
bc group doggy kitty froggy bc2 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.

  • 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. 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. 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. Oct 15 '17 at 0:49

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.

  • 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 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

It sounds like you need something like this:

GROUP_NAME='# bc group'

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.