2

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.

don_crissti
  • 82,805
  • 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 00:49
  • see related question: https://unix.stackexchange.com/questions/32908/how-to-insert-the-content-of-a-file-into-another-file-before-a-pattern-marker – wisbucky Oct 23 '19 at 20:24

2 Answers2

1

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.

RobertL
  • 6,780
  • 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
0

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.

jesse_b
  • 37,005