Rename a HTML file based on the content of a tag

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 a lot of html files that I want to rename according to the content of
the tag h1.



Any suggestion on how to do it on bash?



Example of a file:



<!DOCTYPE html><html lang="pt-BR"><head><meta charset="utf-8"><title>Repositório - MAIS</title>
<script src="lib/tudo.js"></script>
<link rel="stylesheet" href="lib/style.css">
</head>
<body>
<div id="cabecalho"></div>
<div id="corpo">
<h1>teste</h1>

<div class="Experimento"></div>
<div class="gallery">
<img class="image-gallery" src="img/2dados.png">
</div>

<br><br><strong>Mídia:</strong> experimento (uma aula dupla)

<br><br><strong>Descrição:</strong> este experimento propõe 4 jogos diferentes, todos baseados no lançamento de 2 dados comuns. Discutindo as chances de cada jogador vencer cada um dos jogos, os estudantes terão a chance de discutir vários conteúdos relacionados à probabilidade

<br><br><strong>Conteúdo:</strong> experimento aleatório, espaço amostral, eventos equiprováveis, probabilidade

<br><br><strong>Recomendação de uso:</strong> este experimento pode ser usado como introdução ou aplicação dos conceitos iniciais de probabilidade.

<br><br><strong>Autoria:</strong> este experimento foi desenvolvido pela <a class="externo" href="http://www.mais.mat.br" target="_blank">Mais</a> e pode ser utuilziado e distribído livremente, contanto que citada a autoria original.

<a class="download" href="http://www.mais.mat.br/recursos/images/5/5b/2dados.pdf">Baixar</a>

</div>
<div id="rodape"></div>
</body>
</html>


I want the file renamed to "teste.html"



If it is helpful, this tag is always on the 8th line of each file and alone (nothing else on the same line). Also, there is always only one occurrence of h1 in each file.







share|improve this question


















  • 3




    I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...).
    – Cyrus
    Feb 17 at 17:23










  • Please add sample input.
    – Gilles Quenot
    Feb 17 at 17:28










  • you should never rely on a line number when dealing with XML/HTML data. Post the crucial tag name and its content
    – RomanPerekhrest
    Feb 17 at 17:30











  • Are you suggesting an edit your own post? Don't worry, the system is smart and the mods can merge your accounts. (Btw, using multiple accounts is allowed here, only misusing this is not (for example, upvoting your own post on your other account)).
    – peterh
    Feb 17 at 19:51











  • @peterh I'm afraid mods can't merge accounts. Leo, please request a merge of your two accounts by following the instructions here: unix.stackexchange.com/help/merging-accounts
    – terdon♦
    Feb 18 at 12:31














up vote
0
down vote

favorite












I have a lot of html files that I want to rename according to the content of
the tag h1.



Any suggestion on how to do it on bash?



Example of a file:



<!DOCTYPE html><html lang="pt-BR"><head><meta charset="utf-8"><title>Repositório - MAIS</title>
<script src="lib/tudo.js"></script>
<link rel="stylesheet" href="lib/style.css">
</head>
<body>
<div id="cabecalho"></div>
<div id="corpo">
<h1>teste</h1>

<div class="Experimento"></div>
<div class="gallery">
<img class="image-gallery" src="img/2dados.png">
</div>

<br><br><strong>Mídia:</strong> experimento (uma aula dupla)

<br><br><strong>Descrição:</strong> este experimento propõe 4 jogos diferentes, todos baseados no lançamento de 2 dados comuns. Discutindo as chances de cada jogador vencer cada um dos jogos, os estudantes terão a chance de discutir vários conteúdos relacionados à probabilidade

<br><br><strong>Conteúdo:</strong> experimento aleatório, espaço amostral, eventos equiprováveis, probabilidade

<br><br><strong>Recomendação de uso:</strong> este experimento pode ser usado como introdução ou aplicação dos conceitos iniciais de probabilidade.

<br><br><strong>Autoria:</strong> este experimento foi desenvolvido pela <a class="externo" href="http://www.mais.mat.br" target="_blank">Mais</a> e pode ser utuilziado e distribído livremente, contanto que citada a autoria original.

<a class="download" href="http://www.mais.mat.br/recursos/images/5/5b/2dados.pdf">Baixar</a>

</div>
<div id="rodape"></div>
</body>
</html>


I want the file renamed to "teste.html"



If it is helpful, this tag is always on the 8th line of each file and alone (nothing else on the same line). Also, there is always only one occurrence of h1 in each file.







share|improve this question


















  • 3




    I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...).
    – Cyrus
    Feb 17 at 17:23










  • Please add sample input.
    – Gilles Quenot
    Feb 17 at 17:28










  • you should never rely on a line number when dealing with XML/HTML data. Post the crucial tag name and its content
    – RomanPerekhrest
    Feb 17 at 17:30











  • Are you suggesting an edit your own post? Don't worry, the system is smart and the mods can merge your accounts. (Btw, using multiple accounts is allowed here, only misusing this is not (for example, upvoting your own post on your other account)).
    – peterh
    Feb 17 at 19:51











  • @peterh I'm afraid mods can't merge accounts. Leo, please request a merge of your two accounts by following the instructions here: unix.stackexchange.com/help/merging-accounts
    – terdon♦
    Feb 18 at 12:31












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a lot of html files that I want to rename according to the content of
the tag h1.



Any suggestion on how to do it on bash?



Example of a file:



<!DOCTYPE html><html lang="pt-BR"><head><meta charset="utf-8"><title>Repositório - MAIS</title>
<script src="lib/tudo.js"></script>
<link rel="stylesheet" href="lib/style.css">
</head>
<body>
<div id="cabecalho"></div>
<div id="corpo">
<h1>teste</h1>

<div class="Experimento"></div>
<div class="gallery">
<img class="image-gallery" src="img/2dados.png">
</div>

<br><br><strong>Mídia:</strong> experimento (uma aula dupla)

<br><br><strong>Descrição:</strong> este experimento propõe 4 jogos diferentes, todos baseados no lançamento de 2 dados comuns. Discutindo as chances de cada jogador vencer cada um dos jogos, os estudantes terão a chance de discutir vários conteúdos relacionados à probabilidade

<br><br><strong>Conteúdo:</strong> experimento aleatório, espaço amostral, eventos equiprováveis, probabilidade

<br><br><strong>Recomendação de uso:</strong> este experimento pode ser usado como introdução ou aplicação dos conceitos iniciais de probabilidade.

<br><br><strong>Autoria:</strong> este experimento foi desenvolvido pela <a class="externo" href="http://www.mais.mat.br" target="_blank">Mais</a> e pode ser utuilziado e distribído livremente, contanto que citada a autoria original.

<a class="download" href="http://www.mais.mat.br/recursos/images/5/5b/2dados.pdf">Baixar</a>

</div>
<div id="rodape"></div>
</body>
</html>


I want the file renamed to "teste.html"



If it is helpful, this tag is always on the 8th line of each file and alone (nothing else on the same line). Also, there is always only one occurrence of h1 in each file.







share|improve this question














I have a lot of html files that I want to rename according to the content of
the tag h1.



Any suggestion on how to do it on bash?



Example of a file:



<!DOCTYPE html><html lang="pt-BR"><head><meta charset="utf-8"><title>Repositório - MAIS</title>
<script src="lib/tudo.js"></script>
<link rel="stylesheet" href="lib/style.css">
</head>
<body>
<div id="cabecalho"></div>
<div id="corpo">
<h1>teste</h1>

<div class="Experimento"></div>
<div class="gallery">
<img class="image-gallery" src="img/2dados.png">
</div>

<br><br><strong>Mídia:</strong> experimento (uma aula dupla)

<br><br><strong>Descrição:</strong> este experimento propõe 4 jogos diferentes, todos baseados no lançamento de 2 dados comuns. Discutindo as chances de cada jogador vencer cada um dos jogos, os estudantes terão a chance de discutir vários conteúdos relacionados à probabilidade

<br><br><strong>Conteúdo:</strong> experimento aleatório, espaço amostral, eventos equiprováveis, probabilidade

<br><br><strong>Recomendação de uso:</strong> este experimento pode ser usado como introdução ou aplicação dos conceitos iniciais de probabilidade.

<br><br><strong>Autoria:</strong> este experimento foi desenvolvido pela <a class="externo" href="http://www.mais.mat.br" target="_blank">Mais</a> e pode ser utuilziado e distribído livremente, contanto que citada a autoria original.

<a class="download" href="http://www.mais.mat.br/recursos/images/5/5b/2dados.pdf">Baixar</a>

</div>
<div id="rodape"></div>
</body>
</html>


I want the file renamed to "teste.html"



If it is helpful, this tag is always on the 8th line of each file and alone (nothing else on the same line). Also, there is always only one occurrence of h1 in each file.









share|improve this question













share|improve this question




share|improve this question








edited Feb 18 at 1:20









Jeff Schaller

31.2k846105




31.2k846105










asked Feb 17 at 17:19









Leo

1




1







  • 3




    I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...).
    – Cyrus
    Feb 17 at 17:23










  • Please add sample input.
    – Gilles Quenot
    Feb 17 at 17:28










  • you should never rely on a line number when dealing with XML/HTML data. Post the crucial tag name and its content
    – RomanPerekhrest
    Feb 17 at 17:30











  • Are you suggesting an edit your own post? Don't worry, the system is smart and the mods can merge your accounts. (Btw, using multiple accounts is allowed here, only misusing this is not (for example, upvoting your own post on your other account)).
    – peterh
    Feb 17 at 19:51











  • @peterh I'm afraid mods can't merge accounts. Leo, please request a merge of your two accounts by following the instructions here: unix.stackexchange.com/help/merging-accounts
    – terdon♦
    Feb 18 at 12:31












  • 3




    I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...).
    – Cyrus
    Feb 17 at 17:23










  • Please add sample input.
    – Gilles Quenot
    Feb 17 at 17:28










  • you should never rely on a line number when dealing with XML/HTML data. Post the crucial tag name and its content
    – RomanPerekhrest
    Feb 17 at 17:30











  • Are you suggesting an edit your own post? Don't worry, the system is smart and the mods can merge your accounts. (Btw, using multiple accounts is allowed here, only misusing this is not (for example, upvoting your own post on your other account)).
    – peterh
    Feb 17 at 19:51











  • @peterh I'm afraid mods can't merge accounts. Leo, please request a merge of your two accounts by following the instructions here: unix.stackexchange.com/help/merging-accounts
    – terdon♦
    Feb 18 at 12:31







3




3




I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...).
– Cyrus
Feb 17 at 17:23




I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...).
– Cyrus
Feb 17 at 17:23












Please add sample input.
– Gilles Quenot
Feb 17 at 17:28




Please add sample input.
– Gilles Quenot
Feb 17 at 17:28












you should never rely on a line number when dealing with XML/HTML data. Post the crucial tag name and its content
– RomanPerekhrest
Feb 17 at 17:30





you should never rely on a line number when dealing with XML/HTML data. Post the crucial tag name and its content
– RomanPerekhrest
Feb 17 at 17:30













Are you suggesting an edit your own post? Don't worry, the system is smart and the mods can merge your accounts. (Btw, using multiple accounts is allowed here, only misusing this is not (for example, upvoting your own post on your other account)).
– peterh
Feb 17 at 19:51





Are you suggesting an edit your own post? Don't worry, the system is smart and the mods can merge your accounts. (Btw, using multiple accounts is allowed here, only misusing this is not (for example, upvoting your own post on your other account)).
– peterh
Feb 17 at 19:51













@peterh I'm afraid mods can't merge accounts. Leo, please request a merge of your two accounts by following the instructions here: unix.stackexchange.com/help/merging-accounts
– terdon♦
Feb 18 at 12:31




@peterh I'm afraid mods can't merge accounts. Leo, please request a merge of your two accounts by following the instructions here: unix.stackexchange.com/help/merging-accounts
– terdon♦
Feb 18 at 12:31










5 Answers
5






active

oldest

votes

















up vote
3
down vote













With xmllint :



File:



<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<a>foo</a>
<b>bar</b>
<c>base</c>
</body>
</html>


Command:



for file in *.html; do
tag=$(xmllint --xpath '//b/text()' $file)
echo mv "$file" "$tag_$file"
done


Comment:



Put off the echo command when your tests become ok to run the command for real






share|improve this answer


















  • 1




    maybe echo mv "$file" "$tag_$file" - that way the quotes are still correct if echo is deleted from the line. Also, why not put in a <h1>...</h1> example since that's what the OP wanted? finally, a pipe to head -n 1 would be useful in case there's more than one h1 tag.
    – cas
    Feb 18 at 10:39

















up vote
1
down vote













The right way with find + xmlstarlet tools:



find . -type f -name "*.html" -exec sh -c 
'name=$(xmlstarlet sel -t -v "//tagname" $1 2>/dev/null);
[ ! -z "$name" ] && echo mv "$1" "$1%%/*/$name.html"' _ ;



  • name - variable which is assigned with a value (the contents of the tag tagname) for the new filename


  • [ ! -z "$name" ] - check if a new filename is not empty (i.e. <tagname> was found and it had a value)





share|improve this answer





























    up vote
    1
    down vote













    With xmlstarlet:



    xmlstarlet format --html teste.html | xmlstarlet select --html --template --value-of '//html/body/div/h1'


    Output:




    teste


    I used xmlstarlet format --html teste.html to fix your non valid html code.






    share|improve this answer



























      up vote
      0
      down vote













      If tag is alone on the 8-th line, you have many way to pick it.



      tag=$( awk NR==8 file )

      tag=$( awk 'NR == 8 print ;' file )


      where




      • NR is number of record (line number)


      • print is implied action

      or



      tag=$(sed -n 8p file)


      where




      • -n will not echo line


      • 8p print 8th line.

      and many other.



      as notted in comment, relaying on line number is risky. If file is a valid xml file, extracting tag would be easier and safest.






      share|improve this answer



























        up vote
        0
        down vote













        My final solution was the code below, combining two suggestions. Thank you, guys!



        for file in *.html; do
        tag=$(xmlstarlet format --html $file | xmlstarlet select --html --template --value-of '//html/body/div/h1')
        mv "$file" "$tag.html"
        done


        It worked great for my files!






        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%2f424830%2frename-a-html-file-based-on-the-content-of-a-tag%23new-answer', 'question_page');

          );

          Post as a guest






























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          With xmllint :



          File:



          <!DOCTYPE html>
          <html>
          <head>
          <title>test</title>
          </head>
          <body>
          <a>foo</a>
          <b>bar</b>
          <c>base</c>
          </body>
          </html>


          Command:



          for file in *.html; do
          tag=$(xmllint --xpath '//b/text()' $file)
          echo mv "$file" "$tag_$file"
          done


          Comment:



          Put off the echo command when your tests become ok to run the command for real






          share|improve this answer


















          • 1




            maybe echo mv "$file" "$tag_$file" - that way the quotes are still correct if echo is deleted from the line. Also, why not put in a <h1>...</h1> example since that's what the OP wanted? finally, a pipe to head -n 1 would be useful in case there's more than one h1 tag.
            – cas
            Feb 18 at 10:39














          up vote
          3
          down vote













          With xmllint :



          File:



          <!DOCTYPE html>
          <html>
          <head>
          <title>test</title>
          </head>
          <body>
          <a>foo</a>
          <b>bar</b>
          <c>base</c>
          </body>
          </html>


          Command:



          for file in *.html; do
          tag=$(xmllint --xpath '//b/text()' $file)
          echo mv "$file" "$tag_$file"
          done


          Comment:



          Put off the echo command when your tests become ok to run the command for real






          share|improve this answer


















          • 1




            maybe echo mv "$file" "$tag_$file" - that way the quotes are still correct if echo is deleted from the line. Also, why not put in a <h1>...</h1> example since that's what the OP wanted? finally, a pipe to head -n 1 would be useful in case there's more than one h1 tag.
            – cas
            Feb 18 at 10:39












          up vote
          3
          down vote










          up vote
          3
          down vote









          With xmllint :



          File:



          <!DOCTYPE html>
          <html>
          <head>
          <title>test</title>
          </head>
          <body>
          <a>foo</a>
          <b>bar</b>
          <c>base</c>
          </body>
          </html>


          Command:



          for file in *.html; do
          tag=$(xmllint --xpath '//b/text()' $file)
          echo mv "$file" "$tag_$file"
          done


          Comment:



          Put off the echo command when your tests become ok to run the command for real






          share|improve this answer














          With xmllint :



          File:



          <!DOCTYPE html>
          <html>
          <head>
          <title>test</title>
          </head>
          <body>
          <a>foo</a>
          <b>bar</b>
          <c>base</c>
          </body>
          </html>


          Command:



          for file in *.html; do
          tag=$(xmllint --xpath '//b/text()' $file)
          echo mv "$file" "$tag_$file"
          done


          Comment:



          Put off the echo command when your tests become ok to run the command for real







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 19 at 14:17

























          answered Feb 17 at 17:35









          Gilles Quenot

          15.3k13448




          15.3k13448







          • 1




            maybe echo mv "$file" "$tag_$file" - that way the quotes are still correct if echo is deleted from the line. Also, why not put in a <h1>...</h1> example since that's what the OP wanted? finally, a pipe to head -n 1 would be useful in case there's more than one h1 tag.
            – cas
            Feb 18 at 10:39












          • 1




            maybe echo mv "$file" "$tag_$file" - that way the quotes are still correct if echo is deleted from the line. Also, why not put in a <h1>...</h1> example since that's what the OP wanted? finally, a pipe to head -n 1 would be useful in case there's more than one h1 tag.
            – cas
            Feb 18 at 10:39







          1




          1




          maybe echo mv "$file" "$tag_$file" - that way the quotes are still correct if echo is deleted from the line. Also, why not put in a <h1>...</h1> example since that's what the OP wanted? finally, a pipe to head -n 1 would be useful in case there's more than one h1 tag.
          – cas
          Feb 18 at 10:39




          maybe echo mv "$file" "$tag_$file" - that way the quotes are still correct if echo is deleted from the line. Also, why not put in a <h1>...</h1> example since that's what the OP wanted? finally, a pipe to head -n 1 would be useful in case there's more than one h1 tag.
          – cas
          Feb 18 at 10:39












          up vote
          1
          down vote













          The right way with find + xmlstarlet tools:



          find . -type f -name "*.html" -exec sh -c 
          'name=$(xmlstarlet sel -t -v "//tagname" $1 2>/dev/null);
          [ ! -z "$name" ] && echo mv "$1" "$1%%/*/$name.html"' _ ;



          • name - variable which is assigned with a value (the contents of the tag tagname) for the new filename


          • [ ! -z "$name" ] - check if a new filename is not empty (i.e. <tagname> was found and it had a value)





          share|improve this answer


























            up vote
            1
            down vote













            The right way with find + xmlstarlet tools:



            find . -type f -name "*.html" -exec sh -c 
            'name=$(xmlstarlet sel -t -v "//tagname" $1 2>/dev/null);
            [ ! -z "$name" ] && echo mv "$1" "$1%%/*/$name.html"' _ ;



            • name - variable which is assigned with a value (the contents of the tag tagname) for the new filename


            • [ ! -z "$name" ] - check if a new filename is not empty (i.e. <tagname> was found and it had a value)





            share|improve this answer
























              up vote
              1
              down vote










              up vote
              1
              down vote









              The right way with find + xmlstarlet tools:



              find . -type f -name "*.html" -exec sh -c 
              'name=$(xmlstarlet sel -t -v "//tagname" $1 2>/dev/null);
              [ ! -z "$name" ] && echo mv "$1" "$1%%/*/$name.html"' _ ;



              • name - variable which is assigned with a value (the contents of the tag tagname) for the new filename


              • [ ! -z "$name" ] - check if a new filename is not empty (i.e. <tagname> was found and it had a value)





              share|improve this answer














              The right way with find + xmlstarlet tools:



              find . -type f -name "*.html" -exec sh -c 
              'name=$(xmlstarlet sel -t -v "//tagname" $1 2>/dev/null);
              [ ! -z "$name" ] && echo mv "$1" "$1%%/*/$name.html"' _ ;



              • name - variable which is assigned with a value (the contents of the tag tagname) for the new filename


              • [ ! -z "$name" ] - check if a new filename is not empty (i.e. <tagname> was found and it had a value)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 17 at 17:51

























              answered Feb 17 at 17:46









              RomanPerekhrest

              22.4k12144




              22.4k12144




















                  up vote
                  1
                  down vote













                  With xmlstarlet:



                  xmlstarlet format --html teste.html | xmlstarlet select --html --template --value-of '//html/body/div/h1'


                  Output:




                  teste


                  I used xmlstarlet format --html teste.html to fix your non valid html code.






                  share|improve this answer
























                    up vote
                    1
                    down vote













                    With xmlstarlet:



                    xmlstarlet format --html teste.html | xmlstarlet select --html --template --value-of '//html/body/div/h1'


                    Output:




                    teste


                    I used xmlstarlet format --html teste.html to fix your non valid html code.






                    share|improve this answer






















                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      With xmlstarlet:



                      xmlstarlet format --html teste.html | xmlstarlet select --html --template --value-of '//html/body/div/h1'


                      Output:




                      teste


                      I used xmlstarlet format --html teste.html to fix your non valid html code.






                      share|improve this answer












                      With xmlstarlet:



                      xmlstarlet format --html teste.html | xmlstarlet select --html --template --value-of '//html/body/div/h1'


                      Output:




                      teste


                      I used xmlstarlet format --html teste.html to fix your non valid html code.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 17 at 17:57









                      Cyrus

                      7,0362835




                      7,0362835




















                          up vote
                          0
                          down vote













                          If tag is alone on the 8-th line, you have many way to pick it.



                          tag=$( awk NR==8 file )

                          tag=$( awk 'NR == 8 print ;' file )


                          where




                          • NR is number of record (line number)


                          • print is implied action

                          or



                          tag=$(sed -n 8p file)


                          where




                          • -n will not echo line


                          • 8p print 8th line.

                          and many other.



                          as notted in comment, relaying on line number is risky. If file is a valid xml file, extracting tag would be easier and safest.






                          share|improve this answer
























                            up vote
                            0
                            down vote













                            If tag is alone on the 8-th line, you have many way to pick it.



                            tag=$( awk NR==8 file )

                            tag=$( awk 'NR == 8 print ;' file )


                            where




                            • NR is number of record (line number)


                            • print is implied action

                            or



                            tag=$(sed -n 8p file)


                            where




                            • -n will not echo line


                            • 8p print 8th line.

                            and many other.



                            as notted in comment, relaying on line number is risky. If file is a valid xml file, extracting tag would be easier and safest.






                            share|improve this answer






















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              If tag is alone on the 8-th line, you have many way to pick it.



                              tag=$( awk NR==8 file )

                              tag=$( awk 'NR == 8 print ;' file )


                              where




                              • NR is number of record (line number)


                              • print is implied action

                              or



                              tag=$(sed -n 8p file)


                              where




                              • -n will not echo line


                              • 8p print 8th line.

                              and many other.



                              as notted in comment, relaying on line number is risky. If file is a valid xml file, extracting tag would be easier and safest.






                              share|improve this answer












                              If tag is alone on the 8-th line, you have many way to pick it.



                              tag=$( awk NR==8 file )

                              tag=$( awk 'NR == 8 print ;' file )


                              where




                              • NR is number of record (line number)


                              • print is implied action

                              or



                              tag=$(sed -n 8p file)


                              where




                              • -n will not echo line


                              • 8p print 8th line.

                              and many other.



                              as notted in comment, relaying on line number is risky. If file is a valid xml file, extracting tag would be easier and safest.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 17 at 17:30









                              Archemar

                              18.9k93366




                              18.9k93366




















                                  up vote
                                  0
                                  down vote













                                  My final solution was the code below, combining two suggestions. Thank you, guys!



                                  for file in *.html; do
                                  tag=$(xmlstarlet format --html $file | xmlstarlet select --html --template --value-of '//html/body/div/h1')
                                  mv "$file" "$tag.html"
                                  done


                                  It worked great for my files!






                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    My final solution was the code below, combining two suggestions. Thank you, guys!



                                    for file in *.html; do
                                    tag=$(xmlstarlet format --html $file | xmlstarlet select --html --template --value-of '//html/body/div/h1')
                                    mv "$file" "$tag.html"
                                    done


                                    It worked great for my files!






                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      My final solution was the code below, combining two suggestions. Thank you, guys!



                                      for file in *.html; do
                                      tag=$(xmlstarlet format --html $file | xmlstarlet select --html --template --value-of '//html/body/div/h1')
                                      mv "$file" "$tag.html"
                                      done


                                      It worked great for my files!






                                      share|improve this answer












                                      My final solution was the code below, combining two suggestions. Thank you, guys!



                                      for file in *.html; do
                                      tag=$(xmlstarlet format --html $file | xmlstarlet select --html --template --value-of '//html/body/div/h1')
                                      mv "$file" "$tag.html"
                                      done


                                      It worked great for my files!







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 19 at 10:37









                                      Leonardo Barichello

                                      31




                                      31






















                                           

                                          draft saved


                                          draft discarded


























                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424830%2frename-a-html-file-based-on-the-content-of-a-tag%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