lowercase everything except content between single quotes - perl












1















Is there a way in perl to replace all text in input line except ones within single quotes(There could be more than one) using regex, I have achieved this using the code below but would like to see if it can be done with regex and map.



while (<>) {
my $m=0;
for (split(//)) {
if (/'/ and ! $m) {
$m=1;
print;
}
elsif (/'/ and $m) {
$m=0;
print;
}
elsif ($m) {
print;
}
else {
print lc;
}
}
}




**Sample input:**

and (t.TARGET_TYPE='RAC_DATABASE' or (t.TARGET_TYPE='ORACLE_DATABASE' and t.TYPE_QUALIFIER3 != 'racinst'))

**Sample output:**

and (t.target_type='RAC_DATABASE' or (t.target_type='ORACLE_DATABASE' and t.type_qualifier3 != 'racinst'))









share|improve this question




















  • 3





    Welcome to Stack Overflow. Please edit your question and include sample input and output. It's hard to understand what you mean exactly.

    – simbabque
    Nov 13 '18 at 16:00











  • Can quotes be nested, can they be escaped, does an apostrophe count as a quote, and what should happen in those cases?

    – DavidO
    Nov 13 '18 at 16:17











  • $str=~s/(.*?)(".*?")/sprintf("%s%s",lc $1, $2)/ge; #this will do it

    – hoffmeister
    Nov 13 '18 at 16:37













  • @simbabque Thanks, I have added sample input and output now.

    – Prabhu David
    Nov 13 '18 at 17:02






  • 1





    @SilvioMayolo usually though the answer to the question "Should this be done in Perl with a regex" is "no" though. ;)

    – simbabque
    Nov 13 '18 at 19:04
















1















Is there a way in perl to replace all text in input line except ones within single quotes(There could be more than one) using regex, I have achieved this using the code below but would like to see if it can be done with regex and map.



while (<>) {
my $m=0;
for (split(//)) {
if (/'/ and ! $m) {
$m=1;
print;
}
elsif (/'/ and $m) {
$m=0;
print;
}
elsif ($m) {
print;
}
else {
print lc;
}
}
}




**Sample input:**

and (t.TARGET_TYPE='RAC_DATABASE' or (t.TARGET_TYPE='ORACLE_DATABASE' and t.TYPE_QUALIFIER3 != 'racinst'))

**Sample output:**

and (t.target_type='RAC_DATABASE' or (t.target_type='ORACLE_DATABASE' and t.type_qualifier3 != 'racinst'))









share|improve this question




















  • 3





    Welcome to Stack Overflow. Please edit your question and include sample input and output. It's hard to understand what you mean exactly.

    – simbabque
    Nov 13 '18 at 16:00











  • Can quotes be nested, can they be escaped, does an apostrophe count as a quote, and what should happen in those cases?

    – DavidO
    Nov 13 '18 at 16:17











  • $str=~s/(.*?)(".*?")/sprintf("%s%s",lc $1, $2)/ge; #this will do it

    – hoffmeister
    Nov 13 '18 at 16:37













  • @simbabque Thanks, I have added sample input and output now.

    – Prabhu David
    Nov 13 '18 at 17:02






  • 1





    @SilvioMayolo usually though the answer to the question "Should this be done in Perl with a regex" is "no" though. ;)

    – simbabque
    Nov 13 '18 at 19:04














1












1








1








Is there a way in perl to replace all text in input line except ones within single quotes(There could be more than one) using regex, I have achieved this using the code below but would like to see if it can be done with regex and map.



while (<>) {
my $m=0;
for (split(//)) {
if (/'/ and ! $m) {
$m=1;
print;
}
elsif (/'/ and $m) {
$m=0;
print;
}
elsif ($m) {
print;
}
else {
print lc;
}
}
}




**Sample input:**

and (t.TARGET_TYPE='RAC_DATABASE' or (t.TARGET_TYPE='ORACLE_DATABASE' and t.TYPE_QUALIFIER3 != 'racinst'))

**Sample output:**

and (t.target_type='RAC_DATABASE' or (t.target_type='ORACLE_DATABASE' and t.type_qualifier3 != 'racinst'))









share|improve this question
















Is there a way in perl to replace all text in input line except ones within single quotes(There could be more than one) using regex, I have achieved this using the code below but would like to see if it can be done with regex and map.



while (<>) {
my $m=0;
for (split(//)) {
if (/'/ and ! $m) {
$m=1;
print;
}
elsif (/'/ and $m) {
$m=0;
print;
}
elsif ($m) {
print;
}
else {
print lc;
}
}
}




**Sample input:**

and (t.TARGET_TYPE='RAC_DATABASE' or (t.TARGET_TYPE='ORACLE_DATABASE' and t.TYPE_QUALIFIER3 != 'racinst'))

**Sample output:**

and (t.target_type='RAC_DATABASE' or (t.target_type='ORACLE_DATABASE' and t.type_qualifier3 != 'racinst'))






perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 16:56







Prabhu David

















asked Nov 13 '18 at 15:59









Prabhu DavidPrabhu David

133




133








  • 3





    Welcome to Stack Overflow. Please edit your question and include sample input and output. It's hard to understand what you mean exactly.

    – simbabque
    Nov 13 '18 at 16:00











  • Can quotes be nested, can they be escaped, does an apostrophe count as a quote, and what should happen in those cases?

    – DavidO
    Nov 13 '18 at 16:17











  • $str=~s/(.*?)(".*?")/sprintf("%s%s",lc $1, $2)/ge; #this will do it

    – hoffmeister
    Nov 13 '18 at 16:37













  • @simbabque Thanks, I have added sample input and output now.

    – Prabhu David
    Nov 13 '18 at 17:02






  • 1





    @SilvioMayolo usually though the answer to the question "Should this be done in Perl with a regex" is "no" though. ;)

    – simbabque
    Nov 13 '18 at 19:04














  • 3





    Welcome to Stack Overflow. Please edit your question and include sample input and output. It's hard to understand what you mean exactly.

    – simbabque
    Nov 13 '18 at 16:00











  • Can quotes be nested, can they be escaped, does an apostrophe count as a quote, and what should happen in those cases?

    – DavidO
    Nov 13 '18 at 16:17











  • $str=~s/(.*?)(".*?")/sprintf("%s%s",lc $1, $2)/ge; #this will do it

    – hoffmeister
    Nov 13 '18 at 16:37













  • @simbabque Thanks, I have added sample input and output now.

    – Prabhu David
    Nov 13 '18 at 17:02






  • 1





    @SilvioMayolo usually though the answer to the question "Should this be done in Perl with a regex" is "no" though. ;)

    – simbabque
    Nov 13 '18 at 19:04








3




3





Welcome to Stack Overflow. Please edit your question and include sample input and output. It's hard to understand what you mean exactly.

– simbabque
Nov 13 '18 at 16:00





Welcome to Stack Overflow. Please edit your question and include sample input and output. It's hard to understand what you mean exactly.

– simbabque
Nov 13 '18 at 16:00













Can quotes be nested, can they be escaped, does an apostrophe count as a quote, and what should happen in those cases?

– DavidO
Nov 13 '18 at 16:17





Can quotes be nested, can they be escaped, does an apostrophe count as a quote, and what should happen in those cases?

– DavidO
Nov 13 '18 at 16:17













$str=~s/(.*?)(".*?")/sprintf("%s%s",lc $1, $2)/ge; #this will do it

– hoffmeister
Nov 13 '18 at 16:37







$str=~s/(.*?)(".*?")/sprintf("%s%s",lc $1, $2)/ge; #this will do it

– hoffmeister
Nov 13 '18 at 16:37















@simbabque Thanks, I have added sample input and output now.

– Prabhu David
Nov 13 '18 at 17:02





@simbabque Thanks, I have added sample input and output now.

– Prabhu David
Nov 13 '18 at 17:02




1




1





@SilvioMayolo usually though the answer to the question "Should this be done in Perl with a regex" is "no" though. ;)

– simbabque
Nov 13 '18 at 19:04





@SilvioMayolo usually though the answer to the question "Should this be done in Perl with a regex" is "no" though. ;)

– simbabque
Nov 13 '18 at 19:04












3 Answers
3






active

oldest

votes


















2














You can give this a shot. All one regexp.



$str =~ s/(?:^|'[^']*')K[^']*/lc($&)/ge;


Or, cleaner and more documented (this is semantically equivalent to the above)



$str =~ s/
(?:
^ | # Match either the start of the string, or
'[^']*' # some text in quotes.
)K # Then ignore that part,
# because we want to leave it be.
[^']* # Take the text after it, and
# lowercase it.
/lc($&)/gex;


The g flag tells the regexp to run as many times as necessary. e tells it that the substitution portion (lc($&), in our case) is Perl code, not just text. x lets us put those comments in there so that the regexp isn't total gibberish.






share|improve this answer
























  • Thanks a lot for that, exactly what I was looking for.

    – Prabhu David
    Nov 13 '18 at 17:46



















1














Don't you play too hard with regexp for such a simple job?



Why not get the kid 'split' for it today?



#!/usr/bin/perl
while (<>)
{
@F = split "'";
@F = map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);
print join "'", @F;
}


The above is for understanding. We often join the latter two lines reasonably into:



print join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);


Or enjoy more, making it a one-liner? (in bash shell) In concept, it looks like:



perl -pF/'/ -e 'join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


In reality, however, we need to respect the shell and do some escape (hard) job:



perl -pF/'/ -e 'join "'"'"'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


(The single-quoted single quote needs to become 5 letters: '"'"')



If it doesn't help your job, it helps sleep.






share|improve this answer































    0














    One more variant with Perl one-liner. I'm using hex x27 for single quotes



    $ cat sql_str.txt
    and (t.TARGET_TYPE='RAC_DATABASE' or (t.TARGET_TYPE='ORACLE_DATABASE' and t.TYPE_QUALIFIER3 != 'racinst'))

    $ perl -ne ' { @F=split(/x27/); for my $val (0..$#F) { $F[$val]=lc($F[$val]) if $val%2==0 } ; print join("x27",@F) } ' sql_str.txt
    and (t.target_type='RAC_DATABASE' or (t.target_type='ORACLE_DATABASE' and t.type_qualifier3 != 'racinst'))

    $





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53284859%2flowercase-everything-except-content-between-single-quotes-perl%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can give this a shot. All one regexp.



      $str =~ s/(?:^|'[^']*')K[^']*/lc($&)/ge;


      Or, cleaner and more documented (this is semantically equivalent to the above)



      $str =~ s/
      (?:
      ^ | # Match either the start of the string, or
      '[^']*' # some text in quotes.
      )K # Then ignore that part,
      # because we want to leave it be.
      [^']* # Take the text after it, and
      # lowercase it.
      /lc($&)/gex;


      The g flag tells the regexp to run as many times as necessary. e tells it that the substitution portion (lc($&), in our case) is Perl code, not just text. x lets us put those comments in there so that the regexp isn't total gibberish.






      share|improve this answer
























      • Thanks a lot for that, exactly what I was looking for.

        – Prabhu David
        Nov 13 '18 at 17:46
















      2














      You can give this a shot. All one regexp.



      $str =~ s/(?:^|'[^']*')K[^']*/lc($&)/ge;


      Or, cleaner and more documented (this is semantically equivalent to the above)



      $str =~ s/
      (?:
      ^ | # Match either the start of the string, or
      '[^']*' # some text in quotes.
      )K # Then ignore that part,
      # because we want to leave it be.
      [^']* # Take the text after it, and
      # lowercase it.
      /lc($&)/gex;


      The g flag tells the regexp to run as many times as necessary. e tells it that the substitution portion (lc($&), in our case) is Perl code, not just text. x lets us put those comments in there so that the regexp isn't total gibberish.






      share|improve this answer
























      • Thanks a lot for that, exactly what I was looking for.

        – Prabhu David
        Nov 13 '18 at 17:46














      2












      2








      2







      You can give this a shot. All one regexp.



      $str =~ s/(?:^|'[^']*')K[^']*/lc($&)/ge;


      Or, cleaner and more documented (this is semantically equivalent to the above)



      $str =~ s/
      (?:
      ^ | # Match either the start of the string, or
      '[^']*' # some text in quotes.
      )K # Then ignore that part,
      # because we want to leave it be.
      [^']* # Take the text after it, and
      # lowercase it.
      /lc($&)/gex;


      The g flag tells the regexp to run as many times as necessary. e tells it that the substitution portion (lc($&), in our case) is Perl code, not just text. x lets us put those comments in there so that the regexp isn't total gibberish.






      share|improve this answer













      You can give this a shot. All one regexp.



      $str =~ s/(?:^|'[^']*')K[^']*/lc($&)/ge;


      Or, cleaner and more documented (this is semantically equivalent to the above)



      $str =~ s/
      (?:
      ^ | # Match either the start of the string, or
      '[^']*' # some text in quotes.
      )K # Then ignore that part,
      # because we want to leave it be.
      [^']* # Take the text after it, and
      # lowercase it.
      /lc($&)/gex;


      The g flag tells the regexp to run as many times as necessary. e tells it that the substitution portion (lc($&), in our case) is Perl code, not just text. x lets us put those comments in there so that the regexp isn't total gibberish.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 13 '18 at 17:17









      Silvio MayoloSilvio Mayolo

      14.1k22453




      14.1k22453













      • Thanks a lot for that, exactly what I was looking for.

        – Prabhu David
        Nov 13 '18 at 17:46



















      • Thanks a lot for that, exactly what I was looking for.

        – Prabhu David
        Nov 13 '18 at 17:46

















      Thanks a lot for that, exactly what I was looking for.

      – Prabhu David
      Nov 13 '18 at 17:46





      Thanks a lot for that, exactly what I was looking for.

      – Prabhu David
      Nov 13 '18 at 17:46













      1














      Don't you play too hard with regexp for such a simple job?



      Why not get the kid 'split' for it today?



      #!/usr/bin/perl
      while (<>)
      {
      @F = split "'";
      @F = map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);
      print join "'", @F;
      }


      The above is for understanding. We often join the latter two lines reasonably into:



      print join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);


      Or enjoy more, making it a one-liner? (in bash shell) In concept, it looks like:



      perl -pF/'/ -e 'join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


      In reality, however, we need to respect the shell and do some escape (hard) job:



      perl -pF/'/ -e 'join "'"'"'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


      (The single-quoted single quote needs to become 5 letters: '"'"')



      If it doesn't help your job, it helps sleep.






      share|improve this answer




























        1














        Don't you play too hard with regexp for such a simple job?



        Why not get the kid 'split' for it today?



        #!/usr/bin/perl
        while (<>)
        {
        @F = split "'";
        @F = map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);
        print join "'", @F;
        }


        The above is for understanding. We often join the latter two lines reasonably into:



        print join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);


        Or enjoy more, making it a one-liner? (in bash shell) In concept, it looks like:



        perl -pF/'/ -e 'join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


        In reality, however, we need to respect the shell and do some escape (hard) job:



        perl -pF/'/ -e 'join "'"'"'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


        (The single-quoted single quote needs to become 5 letters: '"'"')



        If it doesn't help your job, it helps sleep.






        share|improve this answer


























          1












          1








          1







          Don't you play too hard with regexp for such a simple job?



          Why not get the kid 'split' for it today?



          #!/usr/bin/perl
          while (<>)
          {
          @F = split "'";
          @F = map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);
          print join "'", @F;
          }


          The above is for understanding. We often join the latter two lines reasonably into:



          print join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);


          Or enjoy more, making it a one-liner? (in bash shell) In concept, it looks like:



          perl -pF/'/ -e 'join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


          In reality, however, we need to respect the shell and do some escape (hard) job:



          perl -pF/'/ -e 'join "'"'"'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


          (The single-quoted single quote needs to become 5 letters: '"'"')



          If it doesn't help your job, it helps sleep.






          share|improve this answer













          Don't you play too hard with regexp for such a simple job?



          Why not get the kid 'split' for it today?



          #!/usr/bin/perl
          while (<>)
          {
          @F = split "'";
          @F = map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);
          print join "'", @F;
          }


          The above is for understanding. We often join the latter two lines reasonably into:



          print join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);


          Or enjoy more, making it a one-liner? (in bash shell) In concept, it looks like:



          perl -pF/'/ -e 'join "'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


          In reality, however, we need to respect the shell and do some escape (hard) job:



          perl -pF/'/ -e 'join "'"'"'", map { $_ % 2 ? $F[$_] : lc $F[$_] } (0..@F);' YOUR_FILE


          (The single-quoted single quote needs to become 5 letters: '"'"')



          If it doesn't help your job, it helps sleep.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 '18 at 16:16









          Charles JieCharles Jie

          707




          707























              0














              One more variant with Perl one-liner. I'm using hex x27 for single quotes



              $ cat sql_str.txt
              and (t.TARGET_TYPE='RAC_DATABASE' or (t.TARGET_TYPE='ORACLE_DATABASE' and t.TYPE_QUALIFIER3 != 'racinst'))

              $ perl -ne ' { @F=split(/x27/); for my $val (0..$#F) { $F[$val]=lc($F[$val]) if $val%2==0 } ; print join("x27",@F) } ' sql_str.txt
              and (t.target_type='RAC_DATABASE' or (t.target_type='ORACLE_DATABASE' and t.type_qualifier3 != 'racinst'))

              $





              share|improve this answer




























                0














                One more variant with Perl one-liner. I'm using hex x27 for single quotes



                $ cat sql_str.txt
                and (t.TARGET_TYPE='RAC_DATABASE' or (t.TARGET_TYPE='ORACLE_DATABASE' and t.TYPE_QUALIFIER3 != 'racinst'))

                $ perl -ne ' { @F=split(/x27/); for my $val (0..$#F) { $F[$val]=lc($F[$val]) if $val%2==0 } ; print join("x27",@F) } ' sql_str.txt
                and (t.target_type='RAC_DATABASE' or (t.target_type='ORACLE_DATABASE' and t.type_qualifier3 != 'racinst'))

                $





                share|improve this answer


























                  0












                  0








                  0







                  One more variant with Perl one-liner. I'm using hex x27 for single quotes



                  $ cat sql_str.txt
                  and (t.TARGET_TYPE='RAC_DATABASE' or (t.TARGET_TYPE='ORACLE_DATABASE' and t.TYPE_QUALIFIER3 != 'racinst'))

                  $ perl -ne ' { @F=split(/x27/); for my $val (0..$#F) { $F[$val]=lc($F[$val]) if $val%2==0 } ; print join("x27",@F) } ' sql_str.txt
                  and (t.target_type='RAC_DATABASE' or (t.target_type='ORACLE_DATABASE' and t.type_qualifier3 != 'racinst'))

                  $





                  share|improve this answer













                  One more variant with Perl one-liner. I'm using hex x27 for single quotes



                  $ cat sql_str.txt
                  and (t.TARGET_TYPE='RAC_DATABASE' or (t.TARGET_TYPE='ORACLE_DATABASE' and t.TYPE_QUALIFIER3 != 'racinst'))

                  $ perl -ne ' { @F=split(/x27/); for my $val (0..$#F) { $F[$val]=lc($F[$val]) if $val%2==0 } ; print join("x27",@F) } ' sql_str.txt
                  and (t.target_type='RAC_DATABASE' or (t.target_type='ORACLE_DATABASE' and t.type_qualifier3 != 'racinst'))

                  $






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 20:38









                  stack0114106stack0114106

                  3,2002417




                  3,2002417






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f53284859%2flowercase-everything-except-content-between-single-quotes-perl%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

                      Full-time equivalent

                      さくらももこ

                      13 indicted, 8 arrested in Calif. drug cartel investigation