skip to Main Content

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.

3

Answers


  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)
    
    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
  3. 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}$insertn/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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search