Adding tags to a specific phrase on each line












0















So, basically I have lines like these:



ILU1910ilu0001 “ My hand is broken , ” said the sailor , “ and smoked the pipe . ” 
ILU1910ilu0001 “ It is going make life harder for us , ” he said .


And I want them to look like this:



<ignore>ILU1910ilu0001</ignore> “ My hand is broken , ” said the sailor , “ and smoked the pipe . ” 
<ignore>ILU1910ilu0001</ignore> “ It is going make life harder for us , ” he said .


Basically at the start of each line there is ILU1910/ilu0001 and I want to add <ignore> at the start and </ignore> at the end of said phrase.



I tried to use this to make it work by using this command:



cat file.txt | sed 's/^([^A-Za-z0-9]+ )/<ignore>1</ignore>/g'


But it does not seem to work. I am using the terminal on my MacBook.










share|improve this question





























    0















    So, basically I have lines like these:



    ILU1910ilu0001 “ My hand is broken , ” said the sailor , “ and smoked the pipe . ” 
    ILU1910ilu0001 “ It is going make life harder for us , ” he said .


    And I want them to look like this:



    <ignore>ILU1910ilu0001</ignore> “ My hand is broken , ” said the sailor , “ and smoked the pipe . ” 
    <ignore>ILU1910ilu0001</ignore> “ It is going make life harder for us , ” he said .


    Basically at the start of each line there is ILU1910/ilu0001 and I want to add <ignore> at the start and </ignore> at the end of said phrase.



    I tried to use this to make it work by using this command:



    cat file.txt | sed 's/^([^A-Za-z0-9]+ )/<ignore>1</ignore>/g'


    But it does not seem to work. I am using the terminal on my MacBook.










    share|improve this question



























      0












      0








      0


      0






      So, basically I have lines like these:



      ILU1910ilu0001 “ My hand is broken , ” said the sailor , “ and smoked the pipe . ” 
      ILU1910ilu0001 “ It is going make life harder for us , ” he said .


      And I want them to look like this:



      <ignore>ILU1910ilu0001</ignore> “ My hand is broken , ” said the sailor , “ and smoked the pipe . ” 
      <ignore>ILU1910ilu0001</ignore> “ It is going make life harder for us , ” he said .


      Basically at the start of each line there is ILU1910/ilu0001 and I want to add <ignore> at the start and </ignore> at the end of said phrase.



      I tried to use this to make it work by using this command:



      cat file.txt | sed 's/^([^A-Za-z0-9]+ )/<ignore>1</ignore>/g'


      But it does not seem to work. I am using the terminal on my MacBook.










      share|improve this question
















      So, basically I have lines like these:



      ILU1910ilu0001 “ My hand is broken , ” said the sailor , “ and smoked the pipe . ” 
      ILU1910ilu0001 “ It is going make life harder for us , ” he said .


      And I want them to look like this:



      <ignore>ILU1910ilu0001</ignore> “ My hand is broken , ” said the sailor , “ and smoked the pipe . ” 
      <ignore>ILU1910ilu0001</ignore> “ It is going make life harder for us , ” he said .


      Basically at the start of each line there is ILU1910/ilu0001 and I want to add <ignore> at the start and </ignore> at the end of said phrase.



      I tried to use this to make it work by using this command:



      cat file.txt | sed 's/^([^A-Za-z0-9]+ )/<ignore>1</ignore>/g'


      But it does not seem to work. I am using the terminal on my MacBook.







      sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 10 at 3:24









      Rui F Ribeiro

      40.4k1479137




      40.4k1479137










      asked Feb 9 at 21:13









      Laura Laura

      313




      313






















          2 Answers
          2






          active

          oldest

          votes


















          1














          It is easily done using sed.



          sed 's/ILU1910\ilu0001/<ignore>&</ignore>/' file.txt


          Since there is only one pattern to match, you can do it more easily using &. If you want to use cat, it'll be



          cat file.txt | sed 's/ILU1910\ilu0001/<ignore>&</ignore>/'


          In your attempt, you are working with anything non-alphanumeric [^A-Za-z0-9]. You could modify your pattern to anything non-blank using [^ ]* as follows:



          sed 's/^[^ ]*/<ignore>&</ignore>/' file.txt





          share|improve this answer

































            0














            If you're trying to edit a file in-place, you could script the changes with ed:



            ed -s file.txt <<< $'1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>,nwnq'


            This calls ed in -s silent mode and sends it a list of instructions in a quoted here-string:





            • 1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>, - on every line (1,$), search and replace (s, ... , ... ,) the first part with the second part. Because there's a forward slash in the replacement text, I changed the typical / delimiter to a comma (,), since there were no commas in the search or replacement text. Because backslashes are interpreted by both the shell's quoting and by ed, they must be doubled-up twice, which is how one turned into four \\.


            • w - write the file back to disk


            • q - quit ed






            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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              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%2f499696%2fadding-tags-to-a-specific-phrase-on-each-line%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              It is easily done using sed.



              sed 's/ILU1910\ilu0001/<ignore>&</ignore>/' file.txt


              Since there is only one pattern to match, you can do it more easily using &. If you want to use cat, it'll be



              cat file.txt | sed 's/ILU1910\ilu0001/<ignore>&</ignore>/'


              In your attempt, you are working with anything non-alphanumeric [^A-Za-z0-9]. You could modify your pattern to anything non-blank using [^ ]* as follows:



              sed 's/^[^ ]*/<ignore>&</ignore>/' file.txt





              share|improve this answer






























                1














                It is easily done using sed.



                sed 's/ILU1910\ilu0001/<ignore>&</ignore>/' file.txt


                Since there is only one pattern to match, you can do it more easily using &. If you want to use cat, it'll be



                cat file.txt | sed 's/ILU1910\ilu0001/<ignore>&</ignore>/'


                In your attempt, you are working with anything non-alphanumeric [^A-Za-z0-9]. You could modify your pattern to anything non-blank using [^ ]* as follows:



                sed 's/^[^ ]*/<ignore>&</ignore>/' file.txt





                share|improve this answer




























                  1












                  1








                  1







                  It is easily done using sed.



                  sed 's/ILU1910\ilu0001/<ignore>&</ignore>/' file.txt


                  Since there is only one pattern to match, you can do it more easily using &. If you want to use cat, it'll be



                  cat file.txt | sed 's/ILU1910\ilu0001/<ignore>&</ignore>/'


                  In your attempt, you are working with anything non-alphanumeric [^A-Za-z0-9]. You could modify your pattern to anything non-blank using [^ ]* as follows:



                  sed 's/^[^ ]*/<ignore>&</ignore>/' file.txt





                  share|improve this answer















                  It is easily done using sed.



                  sed 's/ILU1910\ilu0001/<ignore>&</ignore>/' file.txt


                  Since there is only one pattern to match, you can do it more easily using &. If you want to use cat, it'll be



                  cat file.txt | sed 's/ILU1910\ilu0001/<ignore>&</ignore>/'


                  In your attempt, you are working with anything non-alphanumeric [^A-Za-z0-9]. You could modify your pattern to anything non-blank using [^ ]* as follows:



                  sed 's/^[^ ]*/<ignore>&</ignore>/' file.txt






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 9 at 21:46

























                  answered Feb 9 at 21:40









                  unxnutunxnut

                  3,7272919




                  3,7272919

























                      0














                      If you're trying to edit a file in-place, you could script the changes with ed:



                      ed -s file.txt <<< $'1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>,nwnq'


                      This calls ed in -s silent mode and sends it a list of instructions in a quoted here-string:





                      • 1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>, - on every line (1,$), search and replace (s, ... , ... ,) the first part with the second part. Because there's a forward slash in the replacement text, I changed the typical / delimiter to a comma (,), since there were no commas in the search or replacement text. Because backslashes are interpreted by both the shell's quoting and by ed, they must be doubled-up twice, which is how one turned into four \\.


                      • w - write the file back to disk


                      • q - quit ed






                      share|improve this answer




























                        0














                        If you're trying to edit a file in-place, you could script the changes with ed:



                        ed -s file.txt <<< $'1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>,nwnq'


                        This calls ed in -s silent mode and sends it a list of instructions in a quoted here-string:





                        • 1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>, - on every line (1,$), search and replace (s, ... , ... ,) the first part with the second part. Because there's a forward slash in the replacement text, I changed the typical / delimiter to a comma (,), since there were no commas in the search or replacement text. Because backslashes are interpreted by both the shell's quoting and by ed, they must be doubled-up twice, which is how one turned into four \\.


                        • w - write the file back to disk


                        • q - quit ed






                        share|improve this answer


























                          0












                          0








                          0







                          If you're trying to edit a file in-place, you could script the changes with ed:



                          ed -s file.txt <<< $'1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>,nwnq'


                          This calls ed in -s silent mode and sends it a list of instructions in a quoted here-string:





                          • 1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>, - on every line (1,$), search and replace (s, ... , ... ,) the first part with the second part. Because there's a forward slash in the replacement text, I changed the typical / delimiter to a comma (,), since there were no commas in the search or replacement text. Because backslashes are interpreted by both the shell's quoting and by ed, they must be doubled-up twice, which is how one turned into four \\.


                          • w - write the file back to disk


                          • q - quit ed






                          share|improve this answer













                          If you're trying to edit a file in-place, you could script the changes with ed:



                          ed -s file.txt <<< $'1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>,nwnq'


                          This calls ed in -s silent mode and sends it a list of instructions in a quoted here-string:





                          • 1,$s,ILU1910\\ilu0001,<ignore>ILU1910\\ilu0001</ignore>, - on every line (1,$), search and replace (s, ... , ... ,) the first part with the second part. Because there's a forward slash in the replacement text, I changed the typical / delimiter to a comma (,), since there were no commas in the search or replacement text. Because backslashes are interpreted by both the shell's quoting and by ed, they must be doubled-up twice, which is how one turned into four \\.


                          • w - write the file back to disk


                          • q - quit ed







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 10 at 14:23









                          Jeff SchallerJeff Schaller

                          42.1k1156133




                          42.1k1156133






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499696%2fadding-tags-to-a-specific-phrase-on-each-line%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                              is 'sed' thread safe

                              How to make a Squid Proxy server?