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;
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
add a comment
|
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
Did you trysed '/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 allon 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
add a comment
|
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
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
regex sed
asked Mar 28 at 21:05
YKdvdYKdvd
42 bronze badges
42 bronze badges
Did you trysed '/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 allon 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
add a comment
|
Did you trysed '/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 allon 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
add a comment
|
2 Answers
2
active
oldest
votes
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!"
add a comment
|
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
$
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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!"
add a comment
|
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!"
add a comment
|
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!"
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!"
answered Mar 28 at 21:27
Ed MortonEd Morton
126k13 gold badges49 silver badges111 bronze badges
126k13 gold badges49 silver badges111 bronze badges
add a comment
|
add a comment
|
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
$
add a comment
|
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
$
add a comment
|
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
$
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
$
answered Mar 28 at 21:59
stack0114106stack0114106
5,4462 gold badges5 silver badges25 bronze badges
5,4462 gold badges5 silver badges25 bronze badges
add a comment
|
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 allon 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