Pattern to swap backslashes in particular quoted stringsHow do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string?Regexp for replacing quotes in database insert statementsHow to replace captured groups only?Replace all double quotes within StringDelete lines in a text file that contain a specific stringReplace forward slash with double backslash enclosed in double quotesHow to use regex to specify a series of patterns that are logically this or this or this or this?Regex replace slash with backslash inside #include double quotesHow to find and replace a particular character but only if it is in quotes?

How are steel imports supposed to threaten US national security?

Can a Creature at 0 HP Take Damage?

Is It normal to keep log file larger than data file?

Is there such thing as plasma (from reentry) creating lift?

Why did a young George Washington sign a document admitting to assassinating a French military officer?

Comparison of values in dictionary

Is the text of all UK treaties and laws public?

"Es gefällt ihm." How to identify similar exceptions?

Should a grammatical article be a part of a web link anchor

How to ride a fish?

What can I do to avoid potential charges for bribery?

Solving a Certainty Equivalent (Decision Analysis) problem

Why not build an "approximate" Analytical Engine before the complete one?

How to write Hanief (my name) in Japanese?

Finger Picking Chords - Beats per bar

Are there any privately owned large commercial airports?

How could "aggressor" pilots fly foreign aircraft without speaking the language?

How to find an internship in OR/Optimization?

Transiting through Switzerland by coach with lots of cash

Find the percentage

How can I curtail abuse of the Illusion wizard's Illusory Reality feature?

Creating chess engine, machine learning vs. traditional engine?

Reduction of carbamate with LAH

Can I perform Umrah while on a Saudi Arabian visit e-visa



Pattern to swap backslashes in particular quoted strings


How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string?Regexp for replacing quotes in database insert statementsHow to replace captured groups only?Replace all double quotes within StringDelete lines in a text file that contain a specific stringReplace forward slash with double backslash enclosed in double quotesHow to use regex to specify a series of patterns that are logically this or this or this or this?Regex replace slash with backslash inside #include double quotesHow to find and replace a particular character but only if it is in quotes?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









-1















I'm using sed (actually ssed with extended "-r", so I have access to most any regex functionality I might need) to process files, one change being to convert doubled backslash characters to a single forward slashes inside quoted strings. The problem is that some quoted strings that contain the doubled backslashes should not be converted. All quoted strings I want to target have a certain word "myPhrase" inside the quotes.



So for a file with these two lines:



"\\server\dir\myPhrase\subdir"
"Don't change \something me!"


the output would be:



"//server/dir/myPhrase/subdir"
"Don't change \something me!"


I've tried various combinations of lookahead like (?=myPhrase) within a search pattern that finds the desired quoted chunks and replaces a capture group (\) with / as the replacement, but all my attempts either replace just the first occurance of the doubled backslashes, or those to the left of myPhrase, etc.



I'm sure there is some combination of lookahead/noncapture/recursion that should do this, but I'm blanking out completely right now.










share|improve this question
























  • Did you try sed '/myPhrase/ s/\\///g'?

    – oguz ismail
    Mar 28 at 21:07












  • To use PCRE in ssed, you must use -R option, not -r. That is why lookarounds could not work.

    – Wiktor Stribiżew
    Mar 28 at 21:10












  • Actually, sed '/myPhrase/s,\,/,g' file > newfile should work for you. It will replace all on a line that contains myPhrase

    – Wiktor Stribiżew
    Mar 28 at 21:24












  • ssed -R -e '/myPhrase/ s:\\:/:g;' seems to do the trick for me. It would fail if a line contained both a targeted quoted-string and one to be skipped, but hopefully I don't have that in my processing (I think I'm okay). If I do, I'll take another crack at the lookarounds - I must have thought "ssed -r" got me PCRE features. Thanks.

    – YKdvd
    Mar 28 at 21:34

















-1















I'm using sed (actually ssed with extended "-r", so I have access to most any regex functionality I might need) to process files, one change being to convert doubled backslash characters to a single forward slashes inside quoted strings. The problem is that some quoted strings that contain the doubled backslashes should not be converted. All quoted strings I want to target have a certain word "myPhrase" inside the quotes.



So for a file with these two lines:



"\\server\dir\myPhrase\subdir"
"Don't change \something me!"


the output would be:



"//server/dir/myPhrase/subdir"
"Don't change \something me!"


I've tried various combinations of lookahead like (?=myPhrase) within a search pattern that finds the desired quoted chunks and replaces a capture group (\) with / as the replacement, but all my attempts either replace just the first occurance of the doubled backslashes, or those to the left of myPhrase, etc.



I'm sure there is some combination of lookahead/noncapture/recursion that should do this, but I'm blanking out completely right now.










share|improve this question
























  • Did you try sed '/myPhrase/ s/\\///g'?

    – oguz ismail
    Mar 28 at 21:07












  • To use PCRE in ssed, you must use -R option, not -r. That is why lookarounds could not work.

    – Wiktor Stribiżew
    Mar 28 at 21:10












  • Actually, sed '/myPhrase/s,\,/,g' file > newfile should work for you. It will replace all on a line that contains myPhrase

    – Wiktor Stribiżew
    Mar 28 at 21:24












  • ssed -R -e '/myPhrase/ s:\\:/:g;' seems to do the trick for me. It would fail if a line contained both a targeted quoted-string and one to be skipped, but hopefully I don't have that in my processing (I think I'm okay). If I do, I'll take another crack at the lookarounds - I must have thought "ssed -r" got me PCRE features. Thanks.

    – YKdvd
    Mar 28 at 21:34













-1












-1








-1








I'm using sed (actually ssed with extended "-r", so I have access to most any regex functionality I might need) to process files, one change being to convert doubled backslash characters to a single forward slashes inside quoted strings. The problem is that some quoted strings that contain the doubled backslashes should not be converted. All quoted strings I want to target have a certain word "myPhrase" inside the quotes.



So for a file with these two lines:



"\\server\dir\myPhrase\subdir"
"Don't change \something me!"


the output would be:



"//server/dir/myPhrase/subdir"
"Don't change \something me!"


I've tried various combinations of lookahead like (?=myPhrase) within a search pattern that finds the desired quoted chunks and replaces a capture group (\) with / as the replacement, but all my attempts either replace just the first occurance of the doubled backslashes, or those to the left of myPhrase, etc.



I'm sure there is some combination of lookahead/noncapture/recursion that should do this, but I'm blanking out completely right now.










share|improve this question














I'm using sed (actually ssed with extended "-r", so I have access to most any regex functionality I might need) to process files, one change being to convert doubled backslash characters to a single forward slashes inside quoted strings. The problem is that some quoted strings that contain the doubled backslashes should not be converted. All quoted strings I want to target have a certain word "myPhrase" inside the quotes.



So for a file with these two lines:



"\\server\dir\myPhrase\subdir"
"Don't change \something me!"


the output would be:



"//server/dir/myPhrase/subdir"
"Don't change \something me!"


I've tried various combinations of lookahead like (?=myPhrase) within a search pattern that finds the desired quoted chunks and replaces a capture group (\) with / as the replacement, but all my attempts either replace just the first occurance of the doubled backslashes, or those to the left of myPhrase, etc.



I'm sure there is some combination of lookahead/noncapture/recursion that should do this, but I'm blanking out completely right now.







regex sed






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 21:05









YKdvdYKdvd

42 bronze badges




42 bronze badges















  • Did you try sed '/myPhrase/ s/\\///g'?

    – oguz ismail
    Mar 28 at 21:07












  • To use PCRE in ssed, you must use -R option, not -r. That is why lookarounds could not work.

    – Wiktor Stribiżew
    Mar 28 at 21:10












  • Actually, sed '/myPhrase/s,\,/,g' file > newfile should work for you. It will replace all on a line that contains myPhrase

    – Wiktor Stribiżew
    Mar 28 at 21:24












  • ssed -R -e '/myPhrase/ s:\\:/:g;' seems to do the trick for me. It would fail if a line contained both a targeted quoted-string and one to be skipped, but hopefully I don't have that in my processing (I think I'm okay). If I do, I'll take another crack at the lookarounds - I must have thought "ssed -r" got me PCRE features. Thanks.

    – YKdvd
    Mar 28 at 21:34

















  • Did you try sed '/myPhrase/ s/\\///g'?

    – oguz ismail
    Mar 28 at 21:07












  • To use PCRE in ssed, you must use -R option, not -r. That is why lookarounds could not work.

    – Wiktor Stribiżew
    Mar 28 at 21:10












  • Actually, sed '/myPhrase/s,\,/,g' file > newfile should work for you. It will replace all on a line that contains myPhrase

    – Wiktor Stribiżew
    Mar 28 at 21:24












  • ssed -R -e '/myPhrase/ s:\\:/:g;' seems to do the trick for me. It would fail if a line contained both a targeted quoted-string and one to be skipped, but hopefully I don't have that in my processing (I think I'm okay). If I do, I'll take another crack at the lookarounds - I must have thought "ssed -r" got me PCRE features. Thanks.

    – YKdvd
    Mar 28 at 21:34
















Did you try sed '/myPhrase/ s/\\///g'?

– oguz ismail
Mar 28 at 21:07






Did you try sed '/myPhrase/ s/\\///g'?

– oguz ismail
Mar 28 at 21:07














To use PCRE in ssed, you must use -R option, not -r. That is why lookarounds could not work.

– Wiktor Stribiżew
Mar 28 at 21:10






To use PCRE in ssed, you must use -R option, not -r. That is why lookarounds could not work.

– Wiktor Stribiżew
Mar 28 at 21:10














Actually, sed '/myPhrase/s,\,/,g' file > newfile should work for you. It will replace all on a line that contains myPhrase

– Wiktor Stribiżew
Mar 28 at 21:24






Actually, sed '/myPhrase/s,\,/,g' file > newfile should work for you. It will replace all on a line that contains myPhrase

– Wiktor Stribiżew
Mar 28 at 21:24














ssed -R -e '/myPhrase/ s:\\:/:g;' seems to do the trick for me. It would fail if a line contained both a targeted quoted-string and one to be skipped, but hopefully I don't have that in my processing (I think I'm okay). If I do, I'll take another crack at the lookarounds - I must have thought "ssed -r" got me PCRE features. Thanks.

– YKdvd
Mar 28 at 21:34





ssed -R -e '/myPhrase/ s:\\:/:g;' seems to do the trick for me. It would fail if a line contained both a targeted quoted-string and one to be skipped, but hopefully I don't have that in my processing (I think I'm okay). If I do, I'll take another crack at the lookarounds - I must have thought "ssed -r" got me PCRE features. Thanks.

– YKdvd
Mar 28 at 21:34












2 Answers
2






active

oldest

votes


















0
















With GNU awk for the 3rd arg to match():



$ cat file
"dont change \this" "\\server\dir\myPhrase\subdir" "nor\this"
"Don't change \something me!"

$ awk 'match($0,/(.*)("[^"]*myPhrase[^"]*")(.*)/,a)gsub(/[\][\]/,"/",a[2]); $0=a[1] a[2] a[3] 1' file
"dont change \this" "//server/dir/myPhrase/subdir" "nor\this"
"Don't change \something me!"





share|improve this answer
































    0
















    Try this Perl solution



    perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' file


    with the below inputs



    $ cat ykdvd.txt
    "\\server\dir\myPhrase\subdir"
    "Don't change \something me!"
    Another line

    $ perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' ykdvd.txt
    "//server/dir/myPhrase/subdir"
    "Don't change \something me!"
    Another line

    $





    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/4.0/"u003ecc by-sa 4.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%2f55406828%2fpattern-to-swap-backslashes-in-particular-quoted-strings%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









      0
















      With GNU awk for the 3rd arg to match():



      $ cat file
      "dont change \this" "\\server\dir\myPhrase\subdir" "nor\this"
      "Don't change \something me!"

      $ awk 'match($0,/(.*)("[^"]*myPhrase[^"]*")(.*)/,a)gsub(/[\][\]/,"/",a[2]); $0=a[1] a[2] a[3] 1' file
      "dont change \this" "//server/dir/myPhrase/subdir" "nor\this"
      "Don't change \something me!"





      share|improve this answer





























        0
















        With GNU awk for the 3rd arg to match():



        $ cat file
        "dont change \this" "\\server\dir\myPhrase\subdir" "nor\this"
        "Don't change \something me!"

        $ awk 'match($0,/(.*)("[^"]*myPhrase[^"]*")(.*)/,a)gsub(/[\][\]/,"/",a[2]); $0=a[1] a[2] a[3] 1' file
        "dont change \this" "//server/dir/myPhrase/subdir" "nor\this"
        "Don't change \something me!"





        share|improve this answer



























          0














          0










          0









          With GNU awk for the 3rd arg to match():



          $ cat file
          "dont change \this" "\\server\dir\myPhrase\subdir" "nor\this"
          "Don't change \something me!"

          $ awk 'match($0,/(.*)("[^"]*myPhrase[^"]*")(.*)/,a)gsub(/[\][\]/,"/",a[2]); $0=a[1] a[2] a[3] 1' file
          "dont change \this" "//server/dir/myPhrase/subdir" "nor\this"
          "Don't change \something me!"





          share|improve this answer













          With GNU awk for the 3rd arg to match():



          $ cat file
          "dont change \this" "\\server\dir\myPhrase\subdir" "nor\this"
          "Don't change \something me!"

          $ awk 'match($0,/(.*)("[^"]*myPhrase[^"]*")(.*)/,a)gsub(/[\][\]/,"/",a[2]); $0=a[1] a[2] a[3] 1' file
          "dont change \this" "//server/dir/myPhrase/subdir" "nor\this"
          "Don't change \something me!"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 28 at 21:27









          Ed MortonEd Morton

          126k13 gold badges49 silver badges111 bronze badges




          126k13 gold badges49 silver badges111 bronze badges


























              0
















              Try this Perl solution



              perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' file


              with the below inputs



              $ cat ykdvd.txt
              "\\server\dir\myPhrase\subdir"
              "Don't change \something me!"
              Another line

              $ perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' ykdvd.txt
              "//server/dir/myPhrase/subdir"
              "Don't change \something me!"
              Another line

              $





              share|improve this answer





























                0
















                Try this Perl solution



                perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' file


                with the below inputs



                $ cat ykdvd.txt
                "\\server\dir\myPhrase\subdir"
                "Don't change \something me!"
                Another line

                $ perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' ykdvd.txt
                "//server/dir/myPhrase/subdir"
                "Don't change \something me!"
                Another line

                $





                share|improve this answer



























                  0














                  0










                  0









                  Try this Perl solution



                  perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' file


                  with the below inputs



                  $ cat ykdvd.txt
                  "\\server\dir\myPhrase\subdir"
                  "Don't change \something me!"
                  Another line

                  $ perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' ykdvd.txt
                  "//server/dir/myPhrase/subdir"
                  "Don't change \something me!"
                  Another line

                  $





                  share|improve this answer













                  Try this Perl solution



                  perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' file


                  with the below inputs



                  $ cat ykdvd.txt
                  "\\server\dir\myPhrase\subdir"
                  "Don't change \something me!"
                  Another line

                  $ perl -pe ' s"(.+)?"$x=$1; if($x=~m/myPhrase/ ) $x=~s!\\!/!g;sprintf("x22%sx22",$x)ge ' ykdvd.txt
                  "//server/dir/myPhrase/subdir"
                  "Don't change \something me!"
                  Another line

                  $






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 28 at 21:59









                  stack0114106stack0114106

                  5,4462 gold badges5 silver badges25 bronze badges




                  5,4462 gold badges5 silver badges25 bronze badges































                      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%2f55406828%2fpattern-to-swap-backslashes-in-particular-quoted-strings%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

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript