skip to Main Content

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

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

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

i want add this with a command at centos7, its possible?
i have searched and i saw this is possible with sed or awk command

how i can do this with sed or awk command?

3

Answers


  1. GNU sed:

    sed '10a <vhostMap>n  <vhost>google</vhost>n  <domain>google.com,www.google.com</domain>n</vhostMap>' config.xml
    
    Login or Signup to reply.
  2. Almost the same with awk:

    awk '{ print $0 ; if(NR == 10) printf "<vhostMap>n  <vhost>google</vhost>n  <domain>google.com, www.google.com</domain>n</vhostMap>n" }' config.xml
    
    Login or Signup to reply.
  3. This might work for you (GNU sed and shell):

    cat <<! | sed '10r /dev/stdin' file
    <vhostMap>
      <vhost>google</vhost>
      <domain>google.com, www.google.com</domain>
    </vhostMap>
    !
    

    Place the lines to be appended in a here-doc and append them after line 10 of the file by reading them in as stdin via a pipe.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search