Regex for sed/awk command

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











up vote
0
down vote

favorite












I would like to change the file content which i specified like below.I want to comment out all the lines which contains ".class" except the class name i provide.



@MyClass1.MyClass2(value = 
Class1.class,
Class2.class,
Class3.class,
Class4.class
)
public class Foo


For example if i provide Class2 as parameter to



sed -i -E 's/some_regex/Class2/g'


command,the result should be like this,



@MyClass1.MyClass2(value = 
//Class1.class,
Class2.class,
//Class3.class,
//Class4.class
)
public class Foo


What regex should i use for this?










share|improve this question









New contributor




aekber 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

    favorite












    I would like to change the file content which i specified like below.I want to comment out all the lines which contains ".class" except the class name i provide.



    @MyClass1.MyClass2(value = 
    Class1.class,
    Class2.class,
    Class3.class,
    Class4.class
    )
    public class Foo


    For example if i provide Class2 as parameter to



    sed -i -E 's/some_regex/Class2/g'


    command,the result should be like this,



    @MyClass1.MyClass2(value = 
    //Class1.class,
    Class2.class,
    //Class3.class,
    //Class4.class
    )
    public class Foo


    What regex should i use for this?










    share|improve this question









    New contributor




    aekber 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

      favorite









      up vote
      0
      down vote

      favorite











      I would like to change the file content which i specified like below.I want to comment out all the lines which contains ".class" except the class name i provide.



      @MyClass1.MyClass2(value = 
      Class1.class,
      Class2.class,
      Class3.class,
      Class4.class
      )
      public class Foo


      For example if i provide Class2 as parameter to



      sed -i -E 's/some_regex/Class2/g'


      command,the result should be like this,



      @MyClass1.MyClass2(value = 
      //Class1.class,
      Class2.class,
      //Class3.class,
      //Class4.class
      )
      public class Foo


      What regex should i use for this?










      share|improve this question









      New contributor




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











      I would like to change the file content which i specified like below.I want to comment out all the lines which contains ".class" except the class name i provide.



      @MyClass1.MyClass2(value = 
      Class1.class,
      Class2.class,
      Class3.class,
      Class4.class
      )
      public class Foo


      For example if i provide Class2 as parameter to



      sed -i -E 's/some_regex/Class2/g'


      command,the result should be like this,



      @MyClass1.MyClass2(value = 
      //Class1.class,
      Class2.class,
      //Class3.class,
      //Class4.class
      )
      public class Foo


      What regex should i use for this?







      text-processing awk sed regular-expression






      share|improve this question









      New contributor




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











      share|improve this question









      New contributor




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









      share|improve this question




      share|improve this question








      edited 2 days ago









      Jeff Schaller

      35.9k952119




      35.9k952119






      New contributor




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









      asked 2 days ago









      aekber

      31




      31




      New contributor




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





      New contributor





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






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




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          For a file like this:



          $ cat file1
          @MyClass1.MyClass2(value =
          Class1.class,
          Class2.class,
          Class3.class,
          Class4.class
          )
          public class Foo
          @MyClass11.MyClass2(value =
          Class1.class,
          Class2.class,
          Class3.class,
          Class4.class
          )
          public class Foo2


          You can have this result:



          $ sed '/@MyClass1.MyClass2(*/,/})/s' file1
          @MyClass1.MyClass2(value =
          // Class1.class,
          Class2.class,
          // Class3.class,
          // Class4.class
          )
          public class Foo
          @MyClass11.MyClass2(value =
          Class1.class,
          Class2.class,
          Class3.class,
          Class4.class
          )
          public class Foo2


          Explanation:



          '/@MyClass1.MyClass2(*/,/})/ .... --> range to operate in form /from/,/to/actions



          to avoid escaping //






          share)/ .... --> range to operate in form /from/,/to/actions

          s)/ .... --> range to operate in form /from/,/to/actions



          sClass.*)/ .... --> range to operate in form /from/,/to/actions



          to avoid escaping //






          share)/s' file1
          @MyClass1.MyClass2(value =
          // Class1.class,
          Class2.class,
          // Class3.class,
          // Class4.class
          )
          public class Foo
          @MyClass11.MyClass2(value =
          Class1.class,
          Class2.class,
          Class3.class,
          Class4.class
          )
          public class Foo2


          Explanation:



          '/@MyClass1.MyClass2(*/,/})/ .... --> range to operate in form /from/,/to/actions



          {s|sClass.*|//&|g; --> apply // in all lines starting with whitespace s and then Class



          /Class2/ --> remove // for Class2



          Tip: In sed we can use any char as a separator for s/replacethis/withthat/ synthax.

          I choose | to avoid escaping //







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          George Vasiliou

          5,44531028




          5,44531028







          • 1




            I like how you removed the comment from Class2.
            – unxnut
            2 days ago










          • Great answer,but i am just wondering, what if i want to apply // in all lines ending with .class? What regex should i use instead of {s|sClass.*|//&|g; ?
            – aekber
            2 days ago










          • @aekber Maybe something like s|.class$|//&|g. The use of $ is a regex anchor that means "end of line".
            – George Vasiliou
            yesterday












          • 1




            I like how you removed the comment from Class2.
            – unxnut
            2 days ago










          • Great answer,but i am just wondering, what if i want to apply // in all lines ending with .class? What regex should i use instead of {s|sClass.*|//&|g; ?
            – aekber
            2 days ago










          • @aekber Maybe something like s|.class$|//&|g. The use of $ is a regex anchor that means "end of line".
            – George Vasiliou
            yesterday







          1




          1




          I like how you removed the comment from Class2.
          – unxnut
          2 days ago




          I like how you removed the comment from Class2.
          – unxnut
          2 days ago












          Great answer,but i am just wondering, what if i want to apply // in all lines ending with .class? What regex should i use instead of {s|sClass.*|//&|g; ?
          – aekber
          2 days ago




          Great answer,but i am just wondering, what if i want to apply // in all lines ending with .class? What regex should i use instead of {s|sClass.*|//&|g; ?
          – aekber
          2 days ago












          @aekber Maybe something like s|.class$|//&|g. The use of $ is a regex anchor that means "end of line".
          – George Vasiliou
          yesterday




          @aekber Maybe something like s|.class$|//&|g. The use of $ is a regex anchor that means "end of line".
          – George Vasiliou
          yesterday












          up vote
          1
          down vote













          awk '/.class/ && !/Class2/ print "//", $0
          !(/.class/ && !/Class2/) print $0' filename





          share|improve this answer




















          • I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
            – aekber
            2 days ago














          up vote
          1
          down vote













          awk '/.class/ && !/Class2/ print "//", $0
          !(/.class/ && !/Class2/) print $0' filename





          share|improve this answer




















          • I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
            – aekber
            2 days ago












          up vote
          1
          down vote










          up vote
          1
          down vote









          awk '/.class/ && !/Class2/ print "//", $0
          !(/.class/ && !/Class2/) print $0' filename





          share|improve this answer












          awk '/.class/ && !/Class2/ print "//", $0
          !(/.class/ && !/Class2/) print $0' filename






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          unxnut

          3,5272918




          3,5272918











          • I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
            – aekber
            2 days ago
















          • I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
            – aekber
            2 days ago















          I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
          – aekber
          2 days ago




          I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
          – aekber
          2 days ago










          aekber is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          aekber is a new contributor. Be nice, and check out our Code of Conduct.












          aekber is a new contributor. Be nice, and check out our Code of Conduct.











          aekber is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482024%2fregex-for-sed-awk-command%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 check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?