Insert some lines before a specific line with sed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I've the following file



<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
</fontconfig>


and I've to add the following lines:



 <dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>


before the closing tag /fontconfig>. I'm not sure that it's always on 7th line, so I must look for it as a string. I've some troubles in these strings with <> and / ... How can I solve with sed? thanx







share|improve this question


























    up vote
    4
    down vote

    favorite












    I've the following file



    <?xml version="1.0"?>
    <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
    <fontconfig>
    <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
    <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
    <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
    </fontconfig>


    and I've to add the following lines:



     <dir>/usr/local/texlive/texmf-local</dir>
    <dir>/usr/local/share/fonts</dir>


    before the closing tag /fontconfig>. I'm not sure that it's always on 7th line, so I must look for it as a string. I've some troubles in these strings with <> and / ... How can I solve with sed? thanx







    share|improve this question
























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I've the following file



      <?xml version="1.0"?>
      <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
      <fontconfig>
      <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
      <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
      <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
      </fontconfig>


      and I've to add the following lines:



       <dir>/usr/local/texlive/texmf-local</dir>
      <dir>/usr/local/share/fonts</dir>


      before the closing tag /fontconfig>. I'm not sure that it's always on 7th line, so I must look for it as a string. I've some troubles in these strings with <> and / ... How can I solve with sed? thanx







      share|improve this question














      I've the following file



      <?xml version="1.0"?>
      <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
      <fontconfig>
      <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
      <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
      <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
      </fontconfig>


      and I've to add the following lines:



       <dir>/usr/local/texlive/texmf-local</dir>
      <dir>/usr/local/share/fonts</dir>


      before the closing tag /fontconfig>. I'm not sure that it's always on 7th line, so I must look for it as a string. I've some troubles in these strings with <> and / ... How can I solve with sed? thanx









      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 25 at 18:28









      RomanPerekhrest

      22.4k12144




      22.4k12144










      asked Feb 25 at 18:07









      user41063

      1234




      1234




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          Don't use sed, awk and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.

          The right way with xmlstarlet tool:



          xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local' 
          -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml


          The output:



          <?xml version="1.0"?>
          <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
          <fontconfig>
          <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
          <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
          <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
          <dir>/usr/local/texlive/texmf-local</dir>
          <dir>/usr/local/share/fonts</dir>
          </fontconfig>



          To modify/edit the file in-place - add -L option: xmlstarlet ed -L ....




          For more details type: xmlstarlet ed --help






          share|improve this answer






















          • Thanx, it works very nice. It's the first time I meet xmlstartlet...
            – user41063
            Feb 25 at 18:29










          • @user41063, you're welcome
            – RomanPerekhrest
            Feb 25 at 18:57

















          up vote
          1
          down vote













          You can overcome the issues of / in the pattern text by using a different delimiter such as # e.g.



          sed '#^</fontconfig>#i
          <dir>/usr/local/texlive/texmf-local</dir>
          <dir>/usr/local/share/fonts</dir>
          ' file
          <?xml version="1.0"?>
          <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
          <fontconfig>
          <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
          <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
          <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
          <dir>/usr/local/texlive/texmf-local</dir>
          <dir>/usr/local/share/fonts</dir>
          </fontconfig>





          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426505%2finsert-some-lines-before-a-specific-line-with-sed%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            7
            down vote



            accepted










            Don't use sed, awk and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.

            The right way with xmlstarlet tool:



            xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local' 
            -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml


            The output:



            <?xml version="1.0"?>
            <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
            <fontconfig>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
            <dir>/usr/local/texlive/texmf-local</dir>
            <dir>/usr/local/share/fonts</dir>
            </fontconfig>



            To modify/edit the file in-place - add -L option: xmlstarlet ed -L ....




            For more details type: xmlstarlet ed --help






            share|improve this answer






















            • Thanx, it works very nice. It's the first time I meet xmlstartlet...
              – user41063
              Feb 25 at 18:29










            • @user41063, you're welcome
              – RomanPerekhrest
              Feb 25 at 18:57














            up vote
            7
            down vote



            accepted










            Don't use sed, awk and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.

            The right way with xmlstarlet tool:



            xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local' 
            -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml


            The output:



            <?xml version="1.0"?>
            <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
            <fontconfig>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
            <dir>/usr/local/texlive/texmf-local</dir>
            <dir>/usr/local/share/fonts</dir>
            </fontconfig>



            To modify/edit the file in-place - add -L option: xmlstarlet ed -L ....




            For more details type: xmlstarlet ed --help






            share|improve this answer






















            • Thanx, it works very nice. It's the first time I meet xmlstartlet...
              – user41063
              Feb 25 at 18:29










            • @user41063, you're welcome
              – RomanPerekhrest
              Feb 25 at 18:57












            up vote
            7
            down vote



            accepted







            up vote
            7
            down vote



            accepted






            Don't use sed, awk and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.

            The right way with xmlstarlet tool:



            xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local' 
            -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml


            The output:



            <?xml version="1.0"?>
            <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
            <fontconfig>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
            <dir>/usr/local/texlive/texmf-local</dir>
            <dir>/usr/local/share/fonts</dir>
            </fontconfig>



            To modify/edit the file in-place - add -L option: xmlstarlet ed -L ....




            For more details type: xmlstarlet ed --help






            share|improve this answer














            Don't use sed, awk and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.

            The right way with xmlstarlet tool:



            xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local' 
            -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml


            The output:



            <?xml version="1.0"?>
            <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
            <fontconfig>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
            <dir>/usr/local/texlive/texmf-local</dir>
            <dir>/usr/local/share/fonts</dir>
            </fontconfig>



            To modify/edit the file in-place - add -L option: xmlstarlet ed -L ....




            For more details type: xmlstarlet ed --help







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 25 at 18:24

























            answered Feb 25 at 18:19









            RomanPerekhrest

            22.4k12144




            22.4k12144











            • Thanx, it works very nice. It's the first time I meet xmlstartlet...
              – user41063
              Feb 25 at 18:29










            • @user41063, you're welcome
              – RomanPerekhrest
              Feb 25 at 18:57
















            • Thanx, it works very nice. It's the first time I meet xmlstartlet...
              – user41063
              Feb 25 at 18:29










            • @user41063, you're welcome
              – RomanPerekhrest
              Feb 25 at 18:57















            Thanx, it works very nice. It's the first time I meet xmlstartlet...
            – user41063
            Feb 25 at 18:29




            Thanx, it works very nice. It's the first time I meet xmlstartlet...
            – user41063
            Feb 25 at 18:29












            @user41063, you're welcome
            – RomanPerekhrest
            Feb 25 at 18:57




            @user41063, you're welcome
            – RomanPerekhrest
            Feb 25 at 18:57












            up vote
            1
            down vote













            You can overcome the issues of / in the pattern text by using a different delimiter such as # e.g.



            sed '#^</fontconfig>#i
            <dir>/usr/local/texlive/texmf-local</dir>
            <dir>/usr/local/share/fonts</dir>
            ' file
            <?xml version="1.0"?>
            <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
            <fontconfig>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
            <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
            <dir>/usr/local/texlive/texmf-local</dir>
            <dir>/usr/local/share/fonts</dir>
            </fontconfig>





            share|improve this answer
























              up vote
              1
              down vote













              You can overcome the issues of / in the pattern text by using a different delimiter such as # e.g.



              sed '#^</fontconfig>#i
              <dir>/usr/local/texlive/texmf-local</dir>
              <dir>/usr/local/share/fonts</dir>
              ' file
              <?xml version="1.0"?>
              <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
              <fontconfig>
              <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
              <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
              <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
              <dir>/usr/local/texlive/texmf-local</dir>
              <dir>/usr/local/share/fonts</dir>
              </fontconfig>





              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                You can overcome the issues of / in the pattern text by using a different delimiter such as # e.g.



                sed '#^</fontconfig>#i
                <dir>/usr/local/texlive/texmf-local</dir>
                <dir>/usr/local/share/fonts</dir>
                ' file
                <?xml version="1.0"?>
                <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
                <fontconfig>
                <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
                <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
                <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
                <dir>/usr/local/texlive/texmf-local</dir>
                <dir>/usr/local/share/fonts</dir>
                </fontconfig>





                share|improve this answer












                You can overcome the issues of / in the pattern text by using a different delimiter such as # e.g.



                sed '#^</fontconfig>#i
                <dir>/usr/local/texlive/texmf-local</dir>
                <dir>/usr/local/share/fonts</dir>
                ' file
                <?xml version="1.0"?>
                <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
                <fontconfig>
                <dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
                <dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
                <dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
                <dir>/usr/local/texlive/texmf-local</dir>
                <dir>/usr/local/share/fonts</dir>
                </fontconfig>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 25 at 18:16









                steeldriver

                31.5k34978




                31.5k34978






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426505%2finsert-some-lines-before-a-specific-line-with-sed%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay