How to add text to end of line when pattern is matched?

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











up vote
8
down vote

favorite
2












inputs:



line1 with the PATTERN that contains ( ) 
line2 with the PATTERN that contains ( )
lineN with the PATTERN that contains ( )


outputs:



line1 with the PATTERN that contains ( ) ;
line2 with the PATTERN that contains ( ) ;
...
lineN with the PATTERN that contains ( ) ;


I tried this:



find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"


but it didn't work.










share|improve this question



























    up vote
    8
    down vote

    favorite
    2












    inputs:



    line1 with the PATTERN that contains ( ) 
    line2 with the PATTERN that contains ( )
    lineN with the PATTERN that contains ( )


    outputs:



    line1 with the PATTERN that contains ( ) ;
    line2 with the PATTERN that contains ( ) ;
    ...
    lineN with the PATTERN that contains ( ) ;


    I tried this:



    find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"


    but it didn't work.










    share|improve this question

























      up vote
      8
      down vote

      favorite
      2









      up vote
      8
      down vote

      favorite
      2






      2





      inputs:



      line1 with the PATTERN that contains ( ) 
      line2 with the PATTERN that contains ( )
      lineN with the PATTERN that contains ( )


      outputs:



      line1 with the PATTERN that contains ( ) ;
      line2 with the PATTERN that contains ( ) ;
      ...
      lineN with the PATTERN that contains ( ) ;


      I tried this:



      find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"


      but it didn't work.










      share|improve this question















      inputs:



      line1 with the PATTERN that contains ( ) 
      line2 with the PATTERN that contains ( )
      lineN with the PATTERN that contains ( )


      outputs:



      line1 with the PATTERN that contains ( ) ;
      line2 with the PATTERN that contains ( ) ;
      ...
      lineN with the PATTERN that contains ( ) ;


      I tried this:



      find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"


      but it didn't work.







      shell sed line-editor






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 25 '15 at 13:14









      kenorb

      7,756365103




      7,756365103










      asked Feb 28 '14 at 22:03









      user3342338

      90125




      90125




















          6 Answers
          6






          active

          oldest

          votes

















          up vote
          3
          down vote













          perl -ipe 's/$/;/ if /PATTERN/'


          This will add a ; at the end if the line contains PATTERN.






          share|improve this answer



























            up vote
            3
            down vote













            The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.



            Also, you don't need xargs here, it's safer to use the -exec flag of fine:



            find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +


            If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):



            find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;


            Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.






            share|improve this answer


















            • 1




              You'll gain some efficiency with -exec ... + instead of -exec ... ;, if your find allows it.
              – glenn jackman
              Feb 28 '14 at 22:36

















            up vote
            2
            down vote













            Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:



            sed -i '/(.*)/ s/$/ ;/' test.txt





            share|improve this answer





























              up vote
              1
              down vote













              Using ex (which is equivalent to vi -e/vim -e).



              One file:



              ex +"g/local/s/$/;/g" -cwq foo.txt


              All test.txt files recursively:



              ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt


              Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.



              Note: The :bufdo command is not POSIX.






              share|improve this answer






















              • note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
                – Steven Penny
                Apr 17 '16 at 0:43

















              up vote
              0
              down vote













              Try:



              sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt





              share|improve this answer



























                up vote
                0
                down vote













                como seria en las lineas que no coicidan con la palabra





                share








                New contributor




                jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.

















                  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%2f117570%2fhow-to-add-text-to-end-of-line-when-pattern-is-matched%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  3
                  down vote













                  perl -ipe 's/$/;/ if /PATTERN/'


                  This will add a ; at the end if the line contains PATTERN.






                  share|improve this answer
























                    up vote
                    3
                    down vote













                    perl -ipe 's/$/;/ if /PATTERN/'


                    This will add a ; at the end if the line contains PATTERN.






                    share|improve this answer






















                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      perl -ipe 's/$/;/ if /PATTERN/'


                      This will add a ; at the end if the line contains PATTERN.






                      share|improve this answer












                      perl -ipe 's/$/;/ if /PATTERN/'


                      This will add a ; at the end if the line contains PATTERN.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 28 '14 at 22:11









                      michas

                      14.7k33569




                      14.7k33569






















                          up vote
                          3
                          down vote













                          The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.



                          Also, you don't need xargs here, it's safer to use the -exec flag of fine:



                          find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +


                          If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):



                          find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;


                          Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.






                          share|improve this answer


















                          • 1




                            You'll gain some efficiency with -exec ... + instead of -exec ... ;, if your find allows it.
                            – glenn jackman
                            Feb 28 '14 at 22:36














                          up vote
                          3
                          down vote













                          The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.



                          Also, you don't need xargs here, it's safer to use the -exec flag of fine:



                          find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +


                          If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):



                          find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;


                          Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.






                          share|improve this answer


















                          • 1




                            You'll gain some efficiency with -exec ... + instead of -exec ... ;, if your find allows it.
                            – glenn jackman
                            Feb 28 '14 at 22:36












                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote









                          The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.



                          Also, you don't need xargs here, it's safer to use the -exec flag of fine:



                          find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +


                          If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):



                          find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;


                          Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.






                          share|improve this answer














                          The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.



                          Also, you don't need xargs here, it's safer to use the -exec flag of fine:



                          find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +


                          If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):



                          find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;


                          Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Apr 13 '17 at 12:36









                          Community♦

                          1




                          1










                          answered Feb 28 '14 at 22:14









                          janos

                          6,92322247




                          6,92322247







                          • 1




                            You'll gain some efficiency with -exec ... + instead of -exec ... ;, if your find allows it.
                            – glenn jackman
                            Feb 28 '14 at 22:36












                          • 1




                            You'll gain some efficiency with -exec ... + instead of -exec ... ;, if your find allows it.
                            – glenn jackman
                            Feb 28 '14 at 22:36







                          1




                          1




                          You'll gain some efficiency with -exec ... + instead of -exec ... ;, if your find allows it.
                          – glenn jackman
                          Feb 28 '14 at 22:36




                          You'll gain some efficiency with -exec ... + instead of -exec ... ;, if your find allows it.
                          – glenn jackman
                          Feb 28 '14 at 22:36










                          up vote
                          2
                          down vote













                          Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:



                          sed -i '/(.*)/ s/$/ ;/' test.txt





                          share|improve this answer


























                            up vote
                            2
                            down vote













                            Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:



                            sed -i '/(.*)/ s/$/ ;/' test.txt





                            share|improve this answer
























                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:



                              sed -i '/(.*)/ s/$/ ;/' test.txt





                              share|improve this answer














                              Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:



                              sed -i '/(.*)/ s/$/ ;/' test.txt






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 1 '14 at 0:15

























                              answered Feb 28 '14 at 22:15









                              Graeme

                              24.4k46294




                              24.4k46294




















                                  up vote
                                  1
                                  down vote













                                  Using ex (which is equivalent to vi -e/vim -e).



                                  One file:



                                  ex +"g/local/s/$/;/g" -cwq foo.txt


                                  All test.txt files recursively:



                                  ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt


                                  Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.



                                  Note: The :bufdo command is not POSIX.






                                  share|improve this answer






















                                  • note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
                                    – Steven Penny
                                    Apr 17 '16 at 0:43














                                  up vote
                                  1
                                  down vote













                                  Using ex (which is equivalent to vi -e/vim -e).



                                  One file:



                                  ex +"g/local/s/$/;/g" -cwq foo.txt


                                  All test.txt files recursively:



                                  ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt


                                  Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.



                                  Note: The :bufdo command is not POSIX.






                                  share|improve this answer






















                                  • note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
                                    – Steven Penny
                                    Apr 17 '16 at 0:43












                                  up vote
                                  1
                                  down vote










                                  up vote
                                  1
                                  down vote









                                  Using ex (which is equivalent to vi -e/vim -e).



                                  One file:



                                  ex +"g/local/s/$/;/g" -cwq foo.txt


                                  All test.txt files recursively:



                                  ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt


                                  Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.



                                  Note: The :bufdo command is not POSIX.






                                  share|improve this answer














                                  Using ex (which is equivalent to vi -e/vim -e).



                                  One file:



                                  ex +"g/local/s/$/;/g" -cwq foo.txt


                                  All test.txt files recursively:



                                  ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt


                                  Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.



                                  Note: The :bufdo command is not POSIX.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 30 '17 at 13:12

























                                  answered May 25 '15 at 12:55









                                  kenorb

                                  7,756365103




                                  7,756365103











                                  • note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
                                    – Steven Penny
                                    Apr 17 '16 at 0:43
















                                  • note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
                                    – Steven Penny
                                    Apr 17 '16 at 0:43















                                  note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
                                  – Steven Penny
                                  Apr 17 '16 at 0:43




                                  note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
                                  – Steven Penny
                                  Apr 17 '16 at 0:43










                                  up vote
                                  0
                                  down vote













                                  Try:



                                  sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt





                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    Try:



                                    sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt





                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      Try:



                                      sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt





                                      share|improve this answer












                                      Try:



                                      sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 28 '14 at 22:05









                                      DopeGhoti

                                      41.6k55180




                                      41.6k55180




















                                          up vote
                                          0
                                          down vote













                                          como seria en las lineas que no coicidan con la palabra





                                          share








                                          New contributor




                                          jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.





















                                            up vote
                                            0
                                            down vote













                                            como seria en las lineas que no coicidan con la palabra





                                            share








                                            New contributor




                                            jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                            Check out our Code of Conduct.



















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              como seria en las lineas que no coicidan con la palabra





                                              share








                                              New contributor




                                              jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.









                                              como seria en las lineas que no coicidan con la palabra






                                              share








                                              New contributor




                                              jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.








                                              share


                                              share






                                              New contributor




                                              jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.









                                              answered 8 mins ago









                                              jean cesar quintanilla rivera

                                              1




                                              1




                                              New contributor




                                              jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.





                                              New contributor





                                              jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.






                                              jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f117570%2fhow-to-add-text-to-end-of-line-when-pattern-is-matched%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Popular posts from this blog

                                                  Peggy Mitchell

                                                  Palaiologos

                                                  The Forum (Inglewood, California)