sed pattern replace “ to ” and to \ except json string
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I've got problem to replace "
to "
and to
\
and except "
of json
test.txt
input file
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
uff08u524du5bfeu5fdc
I want to output like
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc
text-processing sed
New contributor
add a comment |
I've got problem to replace "
to "
and to
\
and except "
of json
test.txt
input file
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
uff08u524du5bfeu5fdc
I want to output like
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc
text-processing sed
New contributor
1
How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain{
or}
? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a{
?
– terdon♦
1 hour ago
add a comment |
I've got problem to replace "
to "
and to
\
and except "
of json
test.txt
input file
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
uff08u524du5bfeu5fdc
I want to output like
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc
text-processing sed
New contributor
I've got problem to replace "
to "
and to
\
and except "
of json
test.txt
input file
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
uff08u524du5bfeu5fdc
I want to output like
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc
text-processing sed
text-processing sed
New contributor
New contributor
edited 3 hours ago
fra-san
2,0271721
2,0271721
New contributor
asked 3 hours ago
JOIN MAXJOIN MAX
61
61
New contributor
New contributor
1
How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain{
or}
? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a{
?
– terdon♦
1 hour ago
add a comment |
1
How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain{
or}
? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a{
?
– terdon♦
1 hour ago
1
1
How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain
{
or }
? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a {
?– terdon♦
1 hour ago
How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain
{
or }
? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a {
?– terdon♦
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
To be more robust, you could do a full json parsing:
perl -0777 -pe '
s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
$1 ? $1 =~ s/["\]/\$&/gr : $&@gse'
Which on an input like
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
uff08u524du5bfeu5fdc
gives
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
\uff08\u524d\u5bfe\u5fdc
You may want to clarify what you want to do if the input contains "foo"bar"
or "foonbar"
outside of json objects.
add a comment |
In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {
:
$ sed -E '/^[^{]/s|(["])|\1|g' file
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc
However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening {
and closing }
and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:
perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file
However, this will also break if the JSON string itself can contain {
or }
which don't signify a JSON section (e.g. {"1":"b-{c}"}
or whatever). For such cases, use Stéphane's approach instead.
add a comment |
$ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'
- Replace
or
"
at start of line or end of line or before space/tab character:'s/(\|^"|"$|"[ t])/\1/g'
- or
"
after space/tab character:sed 's/([ t])"/1\"/g'
So this only works if your json string doesn't contain any space/tab characters and is on one line.
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.
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%2funix.stackexchange.com%2fquestions%2f511415%2fsed-pattern-replace-to-and-to-except-json-string%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
To be more robust, you could do a full json parsing:
perl -0777 -pe '
s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
$1 ? $1 =~ s/["\]/\$&/gr : $&@gse'
Which on an input like
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
uff08u524du5bfeu5fdc
gives
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
\uff08\u524d\u5bfe\u5fdc
You may want to clarify what you want to do if the input contains "foo"bar"
or "foonbar"
outside of json objects.
add a comment |
To be more robust, you could do a full json parsing:
perl -0777 -pe '
s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
$1 ? $1 =~ s/["\]/\$&/gr : $&@gse'
Which on an input like
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
uff08u524du5bfeu5fdc
gives
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
\uff08\u524d\u5bfe\u5fdc
You may want to clarify what you want to do if the input contains "foo"bar"
or "foonbar"
outside of json objects.
add a comment |
To be more robust, you could do a full json parsing:
perl -0777 -pe '
s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
$1 ? $1 =~ s/["\]/\$&/gr : $&@gse'
Which on an input like
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
uff08u524du5bfeu5fdc
gives
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
\uff08\u524d\u5bfe\u5fdc
You may want to clarify what you want to do if the input contains "foo"bar"
or "foonbar"
outside of json objects.
To be more robust, you could do a full json parsing:
perl -0777 -pe '
s@(".*?"|\)|({(?:"(?:\.|[^"])*+"|(?2)|[^"{}]++)*+})|[^{}\"]+@
$1 ? $1 =~ s/["\]/\$&/gr : $&@gse'
Which on an input like
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
uff08u524du5bfeu5fdc
gives
"a" "b" "c{d"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
{
"1": {"x": "y"}
"2": "}}}"
"3": ["{"x", "}"]
}
\uff08\u524d\u5bfe\u5fdc
You may want to clarify what you want to do if the input contains "foo"bar"
or "foonbar"
outside of json objects.
answered 1 hour ago
Stéphane ChazelasStéphane Chazelas
313k57593950
313k57593950
add a comment |
add a comment |
In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {
:
$ sed -E '/^[^{]/s|(["])|\1|g' file
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc
However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening {
and closing }
and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:
perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file
However, this will also break if the JSON string itself can contain {
or }
which don't signify a JSON section (e.g. {"1":"b-{c}"}
or whatever). For such cases, use Stéphane's approach instead.
add a comment |
In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {
:
$ sed -E '/^[^{]/s|(["])|\1|g' file
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc
However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening {
and closing }
and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:
perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file
However, this will also break if the JSON string itself can contain {
or }
which don't signify a JSON section (e.g. {"1":"b-{c}"}
or whatever). For such cases, use Stéphane's approach instead.
add a comment |
In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {
:
$ sed -E '/^[^{]/s|(["])|\1|g' file
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc
However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening {
and closing }
and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:
perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file
However, this will also break if the JSON string itself can contain {
or }
which don't signify a JSON section (e.g. {"1":"b-{c}"}
or whatever). For such cases, use Stéphane's approach instead.
In the simple example you show, this is easy. Just escape the characters only on lines that don't start with a {
:
$ sed -E '/^[^{]/s|(["])|\1|g' file
"a" "b"
{"1":"female","2":"197312","3":"359","4":"201109","5":"mail"}
\uff08\u524d\u5bfe\u5fdc
However, things get considerably more complicated if your JSON can span several lines. For such cases, you can write a little script that counts the number of opening {
and closing }
and only applies the replacement while those numbers are equal (so when we aren't in a JSON string). Something like:
perl -F'' -ne 'for (@F){$op++ if /{/; $cl++ if /}/; if($cl==$op){s|["\]|\$&|g;}print}' file
However, this will also break if the JSON string itself can contain {
or }
which don't signify a JSON section (e.g. {"1":"b-{c}"}
or whatever). For such cases, use Stéphane's approach instead.
edited 41 mins ago
answered 1 hour ago
terdon♦terdon
134k33269449
134k33269449
add a comment |
add a comment |
$ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'
- Replace
or
"
at start of line or end of line or before space/tab character:'s/(\|^"|"$|"[ t])/\1/g'
- or
"
after space/tab character:sed 's/([ t])"/1\"/g'
So this only works if your json string doesn't contain any space/tab characters and is on one line.
add a comment |
$ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'
- Replace
or
"
at start of line or end of line or before space/tab character:'s/(\|^"|"$|"[ t])/\1/g'
- or
"
after space/tab character:sed 's/([ t])"/1\"/g'
So this only works if your json string doesn't contain any space/tab characters and is on one line.
add a comment |
$ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'
- Replace
or
"
at start of line or end of line or before space/tab character:'s/(\|^"|"$|"[ t])/\1/g'
- or
"
after space/tab character:sed 's/([ t])"/1\"/g'
So this only works if your json string doesn't contain any space/tab characters and is on one line.
$ sed 's/(\|^"|"$|"[ t])/\1/g' test.txt | sed 's/([ t])"/1\"/g'
- Replace
or
"
at start of line or end of line or before space/tab character:'s/(\|^"|"$|"[ t])/\1/g'
- or
"
after space/tab character:sed 's/([ t])"/1\"/g'
So this only works if your json string doesn't contain any space/tab characters and is on one line.
edited 2 hours ago
answered 2 hours ago
FreddyFreddy
1,559210
1,559210
add a comment |
add a comment |
JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.
JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.
JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.
JOIN MAX is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f511415%2fsed-pattern-replace-to-and-to-except-json-string%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
1
How complex is your file? Will the JSON string always be in a single line? Will the JSON string never contain
{
or}
? Will there be multiple lines of data? Will the JSON data always be the only line(s) in the file that start with a{
?– terdon♦
1 hour ago