-1

I have a linux centos 7 server and i want Below lines to file with name config.xml

<vhostMap>
  <vhost>google</vhost>
  <domain>google.com, www.google.com</domain>
</vhostMap>

i want add this lines after line 8 at config.xml file

how its possible with sed or awk command? its python or perl? i have searched much, but its hard for me as im noob, can someone tell me some example?

Thanks.

  • Maybe [this](https://stackoverflow.com/questions/46431161/add-a-line-in-a-specific-position-with-linux-and-output-to-the-same-file) can help you. – Daniel Ocando Mar 07 '20 at 11:34
  • [sed](http://www.yourownlinux.com/2015/04/sed-command-in-linux-append-and-insert-lines-to-file.html) approach – Polar Bear Mar 07 '20 at 23:36

3 Answers3

1

In Python it is easy:

to_add = """<vhostMap>
  <vhost>google</vhost>
  <domain>google.com, www.google.com</domain>
</vhostMap>
"""
with open("config.xml", "r") as f:
    all_lines = f.readlines()
with open("config.xml", "w") as f:
    for l in all_lines[:8]: f.write(l)
    f.write(to_add)
    for l in all_lines[8:]: f.write(l)
Błotosmętek
  • 12,717
  • 19
  • 29
1

The easiest way for me to do it with shell commands is to show the head 8 lines of original config.xml and deviate the output to a new file. Then append the newfile with your code and finally including the tail of config.xml starting from line 8

$ head -n 8 config.xml > newconfig.xml
$ cat your_code.txt >> newconfig.xml
$ tail -n+8 config.xml >> newconfig.xml

Finally you can substitute original config.xml with newfile. Check the content of config.xml before doing it!

$ mv newconfig.xml config.xml
Oniric
  • 36
  • 2
1

In perl such operation can be easily achieved in numerous ways, feel free to choose one which you find favorable

Substitute approach

use strict;
use warnings;
use feature 'say';

my $filename = 'config.xml';

my $insert = 
'<vhostMap>
  <vhost>google</vhost>
  <domain>google.com, www.google.com</domain>
</vhostMap>';

open my $fh, '<', $filename
    or die "Couldn't open $filename: $!";

my $data = do{ local $/; <$fh> };

close $fh;

$data =~ s/((.*?\n){8})/${1}$insert\n/s;

open $fh, '>', $filename
    or die "Couldn't open $filename: $!";

say $fh $data;

close $fh;

Array slice approach

use strict;
use warnings;
use feature 'say';

my $filename = 'config.xml';

my $insert = '<vhostMap>
  <vhost>google</vhost>
  <domain>google.com, www.google.com</domain>
</vhostMap>';

open my $fh, '<', $filename
    or die "Couldn't open $filename: $!";

my @lines = <$fh>;

close $fh;

open $fh, '>', $filename
    or die "Couldn't open $filename: $!";

say $fh @lines[0..7],$insert,"\n",@lines[8..$#lines];

close $fh;

Array iteration approach

use strict;
use warnings;
use feature 'say';

my $filename = 'config.xml';

my $insert = '<vhostMap>
  <vhost>google</vhost>
  <domain>google.com, www.google.com</domain>
</vhostMap>';

open my $fh, '<', $filename
    or die "Couldn't open $filename: $!";

my @lines = <$fh>;

close $fh;

open $fh, '>', $filename
    or die "Couldn't open $filename: $!";

my $line_count = 0;

for (@lines) {
    chomp;
    $line_count++;      
    say $fh $_; 
    say $fh $insert if $line_count == 8;
}

close $fh;

Input

line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12

Output

line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
<vhostMap>
  <vhost>google</vhost>
  <domain>google.com, www.google.com</domain>
</vhostMap>
line 9
line 10
line 11
line 12
Polar Bear
  • 6,762
  • 1
  • 5
  • 12