sed command to rplace a line with multiple lines without changing the format of the file [closed]

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











up vote
0
down vote

favorite












I have the below xml file



cat example.xml
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>


In this file, I would like to change the line 5 from



 <function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>


to



 <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>






share|improve this question














closed as unclear what you're asking by Jeff Schaller, jayhendren, mdpc, Archemar, G-Man Jan 4 at 6:58


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
    – francois P
    Jan 3 at 21:47











  • How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with <function .* />?
    – Michael Daffin
    Jan 3 at 22:06










  • Sorry for the confusion, in this example it is line 5.
    – Shravankumar Daddolu
    Jan 3 at 22:24






  • 2




    Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
    – jayhendren
    Jan 4 at 0:20










  • Your question title mentions “without changing the format of the file”, but your question body doesn’t elaborate. Is your concern that the new lines should dynamically match the indentation of the existing line(s)? If so, see How can I tag text based on its indentation?
    – G-Man
    Jan 4 at 6:57














up vote
0
down vote

favorite












I have the below xml file



cat example.xml
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>


In this file, I would like to change the line 5 from



 <function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>


to



 <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>






share|improve this question














closed as unclear what you're asking by Jeff Schaller, jayhendren, mdpc, Archemar, G-Man Jan 4 at 6:58


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
    – francois P
    Jan 3 at 21:47











  • How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with <function .* />?
    – Michael Daffin
    Jan 3 at 22:06










  • Sorry for the confusion, in this example it is line 5.
    – Shravankumar Daddolu
    Jan 3 at 22:24






  • 2




    Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
    – jayhendren
    Jan 4 at 0:20










  • Your question title mentions “without changing the format of the file”, but your question body doesn’t elaborate. Is your concern that the new lines should dynamically match the indentation of the existing line(s)? If so, see How can I tag text based on its indentation?
    – G-Man
    Jan 4 at 6:57












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the below xml file



cat example.xml
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>


In this file, I would like to change the line 5 from



 <function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>


to



 <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>






share|improve this question














I have the below xml file



cat example.xml
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>


In this file, I would like to change the line 5 from



 <function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>


to



 <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>








share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 21:50









αғsнιη

15.3k92462




15.3k92462










asked Jan 3 at 21:45









Shravankumar Daddolu

82




82




closed as unclear what you're asking by Jeff Schaller, jayhendren, mdpc, Archemar, G-Man Jan 4 at 6:58


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Jeff Schaller, jayhendren, mdpc, Archemar, G-Man Jan 4 at 6:58


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
    – francois P
    Jan 3 at 21:47











  • How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with <function .* />?
    – Michael Daffin
    Jan 3 at 22:06










  • Sorry for the confusion, in this example it is line 5.
    – Shravankumar Daddolu
    Jan 3 at 22:24






  • 2




    Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
    – jayhendren
    Jan 4 at 0:20










  • Your question title mentions “without changing the format of the file”, but your question body doesn’t elaborate. Is your concern that the new lines should dynamically match the indentation of the existing line(s)? If so, see How can I tag text based on its indentation?
    – G-Man
    Jan 4 at 6:57
















  • I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
    – francois P
    Jan 3 at 21:47











  • How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with <function .* />?
    – Michael Daffin
    Jan 3 at 22:06










  • Sorry for the confusion, in this example it is line 5.
    – Shravankumar Daddolu
    Jan 3 at 22:24






  • 2




    Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
    – jayhendren
    Jan 4 at 0:20










  • Your question title mentions “without changing the format of the file”, but your question body doesn’t elaborate. Is your concern that the new lines should dynamically match the indentation of the existing line(s)? If so, see How can I tag text based on its indentation?
    – G-Man
    Jan 4 at 6:57















I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
– francois P
Jan 3 at 21:47





I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
– francois P
Jan 3 at 21:47













How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with <function .* />?
– Michael Daffin
Jan 3 at 22:06




How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with <function .* />?
– Michael Daffin
Jan 3 at 22:06












Sorry for the confusion, in this example it is line 5.
– Shravankumar Daddolu
Jan 3 at 22:24




Sorry for the confusion, in this example it is line 5.
– Shravankumar Daddolu
Jan 3 at 22:24




2




2




Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
– jayhendren
Jan 4 at 0:20




Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
– jayhendren
Jan 4 at 0:20












Your question title mentions “without changing the format of the file”, but your question body doesn’t elaborate. Is your concern that the new lines should dynamically match the indentation of the existing line(s)? If so, see How can I tag text based on its indentation?
– G-Man
Jan 4 at 6:57




Your question title mentions “without changing the format of the file”, but your question body doesn’t elaborate. Is your concern that the new lines should dynamically match the indentation of the existing line(s)? If so, see How can I tag text based on its indentation?
– G-Man
Jan 4 at 6:57










3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










create the new file newcontent.txt. putting the content which you want to replace with.




Cat new content.txt
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>



Use below command to get the content replaced as per your requirement




sed '5r newcontent.txt' example2.txt | sed ‘5d'. 



example2.txt==> original input file



newcontent.txt==> Consists of content which need to replaced



Output



<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>





share|improve this answer



























    up vote
    1
    down vote













    awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml





    share|improve this answer




















    • Can we do this generic, irrespective of line number as 5. .
      – Shravankumar Daddolu
      Jan 3 at 22:25










    • Based on what set of criteria?
      – DopeGhoti
      Jan 3 at 22:42










    • let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
      – Shravankumar Daddolu
      Jan 4 at 1:45










    • In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
      – DopeGhoti
      Jan 4 at 15:26

















    up vote
    1
    down vote













    Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.



    Instead, use either:



    • a programming language such as perl or python (or many others) that have XML parsing libraries.

    • a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.

    If you really must use sed or awk, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml to convert it back to properly formatted XML afterwards. e.g.



    xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile





    share|improve this answer



























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      create the new file newcontent.txt. putting the content which you want to replace with.




      Cat new content.txt
      <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
      <parameters name="_key" value="password" isit="true" />
      </function>



      Use below command to get the content replaced as per your requirement




      sed '5r newcontent.txt' example2.txt | sed ‘5d'. 



      example2.txt==> original input file



      newcontent.txt==> Consists of content which need to replaced



      Output



      <?xml version="1.0" encoding="UTF-8"?>
      <TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Text>
      <Checking state="BOOT" />
      <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
      <parameters name="_key" value="password" isit="true" />
      </function>
      <function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
      <function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
      <function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
      <parameters name="_key" value="password" isit="true" />
      </function>
      <function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
      <parameters name="_key" value="password" isit="true" />
      </function>
      <function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
      <parameters name="_key" value="password" isit="true" />
      </function>
      </Text>
      </TestConfig>





      share|improve this answer
























        up vote
        0
        down vote



        accepted










        create the new file newcontent.txt. putting the content which you want to replace with.




        Cat new content.txt
        <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
        <parameters name="_key" value="password" isit="true" />
        </function>



        Use below command to get the content replaced as per your requirement




        sed '5r newcontent.txt' example2.txt | sed ‘5d'. 



        example2.txt==> original input file



        newcontent.txt==> Consists of content which need to replaced



        Output



        <?xml version="1.0" encoding="UTF-8"?>
        <TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Text>
        <Checking state="BOOT" />
        <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
        <parameters name="_key" value="password" isit="true" />
        </function>
        <function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
        <function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
        <function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
        <parameters name="_key" value="password" isit="true" />
        </function>
        <function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
        <parameters name="_key" value="password" isit="true" />
        </function>
        <function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
        <parameters name="_key" value="password" isit="true" />
        </function>
        </Text>
        </TestConfig>





        share|improve this answer






















          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          create the new file newcontent.txt. putting the content which you want to replace with.




          Cat new content.txt
          <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>



          Use below command to get the content replaced as per your requirement




          sed '5r newcontent.txt' example2.txt | sed ‘5d'. 



          example2.txt==> original input file



          newcontent.txt==> Consists of content which need to replaced



          Output



          <?xml version="1.0" encoding="UTF-8"?>
          <TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <Text>
          <Checking state="BOOT" />
          <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>
          <function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
          <function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
          <function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>
          <function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>
          <function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>
          </Text>
          </TestConfig>





          share|improve this answer












          create the new file newcontent.txt. putting the content which you want to replace with.




          Cat new content.txt
          <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>



          Use below command to get the content replaced as per your requirement




          sed '5r newcontent.txt' example2.txt | sed ‘5d'. 



          example2.txt==> original input file



          newcontent.txt==> Consists of content which need to replaced



          Output



          <?xml version="1.0" encoding="UTF-8"?>
          <TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <Text>
          <Checking state="BOOT" />
          <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>
          <function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
          <function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
          <function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>
          <function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>
          <function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
          <parameters name="_key" value="password" isit="true" />
          </function>
          </Text>
          </TestConfig>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 3:05









          Praveen Kumar BS

          1,010128




          1,010128






















              up vote
              1
              down vote













              awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml





              share|improve this answer




















              • Can we do this generic, irrespective of line number as 5. .
                – Shravankumar Daddolu
                Jan 3 at 22:25










              • Based on what set of criteria?
                – DopeGhoti
                Jan 3 at 22:42










              • let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
                – Shravankumar Daddolu
                Jan 4 at 1:45










              • In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
                – DopeGhoti
                Jan 4 at 15:26














              up vote
              1
              down vote













              awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml





              share|improve this answer




















              • Can we do this generic, irrespective of line number as 5. .
                – Shravankumar Daddolu
                Jan 3 at 22:25










              • Based on what set of criteria?
                – DopeGhoti
                Jan 3 at 22:42










              • let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
                – Shravankumar Daddolu
                Jan 4 at 1:45










              • In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
                – DopeGhoti
                Jan 4 at 15:26












              up vote
              1
              down vote










              up vote
              1
              down vote









              awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml





              share|improve this answer












              awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 3 at 22:08









              DopeGhoti

              40.5k54979




              40.5k54979











              • Can we do this generic, irrespective of line number as 5. .
                – Shravankumar Daddolu
                Jan 3 at 22:25










              • Based on what set of criteria?
                – DopeGhoti
                Jan 3 at 22:42










              • let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
                – Shravankumar Daddolu
                Jan 4 at 1:45










              • In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
                – DopeGhoti
                Jan 4 at 15:26
















              • Can we do this generic, irrespective of line number as 5. .
                – Shravankumar Daddolu
                Jan 3 at 22:25










              • Based on what set of criteria?
                – DopeGhoti
                Jan 3 at 22:42










              • let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
                – Shravankumar Daddolu
                Jan 4 at 1:45










              • In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
                – DopeGhoti
                Jan 4 at 15:26















              Can we do this generic, irrespective of line number as 5. .
              – Shravankumar Daddolu
              Jan 3 at 22:25




              Can we do this generic, irrespective of line number as 5. .
              – Shravankumar Daddolu
              Jan 3 at 22:25












              Based on what set of criteria?
              – DopeGhoti
              Jan 3 at 22:42




              Based on what set of criteria?
              – DopeGhoti
              Jan 3 at 22:42












              let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
              – Shravankumar Daddolu
              Jan 4 at 1:45




              let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
              – Shravankumar Daddolu
              Jan 4 at 1:45












              In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
              – DopeGhoti
              Jan 4 at 15:26




              In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
              – DopeGhoti
              Jan 4 at 15:26










              up vote
              1
              down vote













              Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.



              Instead, use either:



              • a programming language such as perl or python (or many others) that have XML parsing libraries.

              • a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.

              If you really must use sed or awk, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml to convert it back to properly formatted XML afterwards. e.g.



              xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile





              share|improve this answer
























                up vote
                1
                down vote













                Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.



                Instead, use either:



                • a programming language such as perl or python (or many others) that have XML parsing libraries.

                • a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.

                If you really must use sed or awk, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml to convert it back to properly formatted XML afterwards. e.g.



                xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.



                  Instead, use either:



                  • a programming language such as perl or python (or many others) that have XML parsing libraries.

                  • a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.

                  If you really must use sed or awk, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml to convert it back to properly formatted XML afterwards. e.g.



                  xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile





                  share|improve this answer












                  Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.



                  Instead, use either:



                  • a programming language such as perl or python (or many others) that have XML parsing libraries.

                  • a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.

                  If you really must use sed or awk, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml to convert it back to properly formatted XML afterwards. e.g.



                  xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 4 at 2:33









                  cas

                  37.7k44394




                  37.7k44394












                      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