Delete Substring from Json File (malformated)

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











up vote
2
down vote

favorite












i am trying to delete differnt values from a json file. Its not pretty and jq return 1 compile error". So i searching for a solution within bash (awk/sed).



Here is my sample data from my json-file (similiar to docker-compose):



aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaa 
bbbb"PROTO 9080/tcp 9090/tcp"bbbbb
cccc"TEST PROTO 80 8080"ccccc


all entries can be found mutiple times or also within one line.



After using the replace it should look like:



aaaaaaaa 
bbbbbbbbb
cccc"TEST"ccccc


I tried sed and (awk -F["]) but could not get some useful results.
How can this be done ?



Update: i made the sample clearer..



There are 3 different types:...:



1: Delete from "MyPorts" to }},
2. Delete "Proto to "
3. Delete from " PROTO" till one character before ", only if left from Proto is a space


Update 2: How can this be done if the sample data is also in one line, like this:



aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaabbbb"PROTO 9080/tcp 9090/tcp"bbbbbcccc"TEST PROTO 80 8080"ccccc


After using the replace it should look like:



aaaaaaaabbbbbbbbbcccc"TEST"ccccc






share|improve this question

















  • 2




    this line ccccPROTO 80 8080"ccccc looks strange. How diverse could be your lines? How this line AaaA"aaa:1"cc"aaa"bbb""aaA should be treated?
    – RomanPerekhrest
    Jun 14 at 18:02











  • I updated the sample to make it clearer.. There are 3 different types:...: 1: Delete from "MyPorts" to }}, 2. Delete "Proto to " . 3 Delete from " PROTO" till ", only if left from Proto is a space
    – biohell
    Jun 14 at 19:59















up vote
2
down vote

favorite












i am trying to delete differnt values from a json file. Its not pretty and jq return 1 compile error". So i searching for a solution within bash (awk/sed).



Here is my sample data from my json-file (similiar to docker-compose):



aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaa 
bbbb"PROTO 9080/tcp 9090/tcp"bbbbb
cccc"TEST PROTO 80 8080"ccccc


all entries can be found mutiple times or also within one line.



After using the replace it should look like:



aaaaaaaa 
bbbbbbbbb
cccc"TEST"ccccc


I tried sed and (awk -F["]) but could not get some useful results.
How can this be done ?



Update: i made the sample clearer..



There are 3 different types:...:



1: Delete from "MyPorts" to }},
2. Delete "Proto to "
3. Delete from " PROTO" till one character before ", only if left from Proto is a space


Update 2: How can this be done if the sample data is also in one line, like this:



aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaabbbb"PROTO 9080/tcp 9090/tcp"bbbbbcccc"TEST PROTO 80 8080"ccccc


After using the replace it should look like:



aaaaaaaabbbbbbbbbcccc"TEST"ccccc






share|improve this question

















  • 2




    this line ccccPROTO 80 8080"ccccc looks strange. How diverse could be your lines? How this line AaaA"aaa:1"cc"aaa"bbb""aaA should be treated?
    – RomanPerekhrest
    Jun 14 at 18:02











  • I updated the sample to make it clearer.. There are 3 different types:...: 1: Delete from "MyPorts" to }}, 2. Delete "Proto to " . 3 Delete from " PROTO" till ", only if left from Proto is a space
    – biohell
    Jun 14 at 19:59













up vote
2
down vote

favorite









up vote
2
down vote

favorite











i am trying to delete differnt values from a json file. Its not pretty and jq return 1 compile error". So i searching for a solution within bash (awk/sed).



Here is my sample data from my json-file (similiar to docker-compose):



aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaa 
bbbb"PROTO 9080/tcp 9090/tcp"bbbbb
cccc"TEST PROTO 80 8080"ccccc


all entries can be found mutiple times or also within one line.



After using the replace it should look like:



aaaaaaaa 
bbbbbbbbb
cccc"TEST"ccccc


I tried sed and (awk -F["]) but could not get some useful results.
How can this be done ?



Update: i made the sample clearer..



There are 3 different types:...:



1: Delete from "MyPorts" to }},
2. Delete "Proto to "
3. Delete from " PROTO" till one character before ", only if left from Proto is a space


Update 2: How can this be done if the sample data is also in one line, like this:



aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaabbbb"PROTO 9080/tcp 9090/tcp"bbbbbcccc"TEST PROTO 80 8080"ccccc


After using the replace it should look like:



aaaaaaaabbbbbbbbbcccc"TEST"ccccc






share|improve this question













i am trying to delete differnt values from a json file. Its not pretty and jq return 1 compile error". So i searching for a solution within bash (awk/sed).



Here is my sample data from my json-file (similiar to docker-compose):



aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaa 
bbbb"PROTO 9080/tcp 9090/tcp"bbbbb
cccc"TEST PROTO 80 8080"ccccc


all entries can be found mutiple times or also within one line.



After using the replace it should look like:



aaaaaaaa 
bbbbbbbbb
cccc"TEST"ccccc


I tried sed and (awk -F["]) but could not get some useful results.
How can this be done ?



Update: i made the sample clearer..



There are 3 different types:...:



1: Delete from "MyPorts" to }},
2. Delete "Proto to "
3. Delete from " PROTO" till one character before ", only if left from Proto is a space


Update 2: How can this be done if the sample data is also in one line, like this:



aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaabbbb"PROTO 9080/tcp 9090/tcp"bbbbbcccc"TEST PROTO 80 8080"ccccc


After using the replace it should look like:



aaaaaaaabbbbbbbbbcccc"TEST"ccccc








share|improve this question












share|improve this question




share|improve this question








edited Jun 15 at 10:03
























asked Jun 14 at 17:56









biohell

133




133







  • 2




    this line ccccPROTO 80 8080"ccccc looks strange. How diverse could be your lines? How this line AaaA"aaa:1"cc"aaa"bbb""aaA should be treated?
    – RomanPerekhrest
    Jun 14 at 18:02











  • I updated the sample to make it clearer.. There are 3 different types:...: 1: Delete from "MyPorts" to }}, 2. Delete "Proto to " . 3 Delete from " PROTO" till ", only if left from Proto is a space
    – biohell
    Jun 14 at 19:59













  • 2




    this line ccccPROTO 80 8080"ccccc looks strange. How diverse could be your lines? How this line AaaA"aaa:1"cc"aaa"bbb""aaA should be treated?
    – RomanPerekhrest
    Jun 14 at 18:02











  • I updated the sample to make it clearer.. There are 3 different types:...: 1: Delete from "MyPorts" to }}, 2. Delete "Proto to " . 3 Delete from " PROTO" till ", only if left from Proto is a space
    – biohell
    Jun 14 at 19:59








2




2




this line ccccPROTO 80 8080"ccccc looks strange. How diverse could be your lines? How this line AaaA"aaa:1"cc"aaa"bbb""aaA should be treated?
– RomanPerekhrest
Jun 14 at 18:02





this line ccccPROTO 80 8080"ccccc looks strange. How diverse could be your lines? How this line AaaA"aaa:1"cc"aaa"bbb""aaA should be treated?
– RomanPerekhrest
Jun 14 at 18:02













I updated the sample to make it clearer.. There are 3 different types:...: 1: Delete from "MyPorts" to }}, 2. Delete "Proto to " . 3 Delete from " PROTO" till ", only if left from Proto is a space
– biohell
Jun 14 at 19:59





I updated the sample to make it clearer.. There are 3 different types:...: 1: Delete from "MyPorts" to }}, 2. Delete "Proto to " . 3 Delete from " PROTO" till ", only if left from Proto is a space
– biohell
Jun 14 at 19:59











1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










sed approach:



sed -E 's~("MyPorts.*},|"PROTO[^"]+"| +PROTO[^"]+)~~' file


The output:



aaaaaaaa 
bbbbbbbbb
cccc"TEST"ccccc



Bonus approach for one-line string input:



$ s='aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaabbbb"PROTO 9080/tcp 9090/tcp"bbbbbcccc"TES^CPROTO 80 8080"ccccc'

$ sed -En 's~("MyPorts.+},|"PROTO[^"]+"| +PROTO[^"]+)~~gp' <<<"$s"
aaaaaaaabbbbbbbbbcccc"TEST"ccccc





share|improve this answer























  • Thank you very much. Works like a charm !!! Great and Short Solution.. Cannot mark this a solution due less reputation, Thanks RomanPerekhrest found it.
    – biohell
    Jun 14 at 20:28











  • Great job at interpreting the OP's request! @biohell can accept the answer, no minimum reputation is required.
    – simlev
    Jun 14 at 20:58










  • @RomanPerekhrest: if the sample data is in one line,your call doesn't work.. Can you chnage your call to reflect this ?
    – biohell
    Jun 15 at 9:40










  • @biohell, all additional cases should be described in the question (with respective input fragment)
    – RomanPerekhrest
    Jun 15 at 9:44










  • @@RomanPerekhrest: i updated the question.
    – biohell
    Jun 15 at 10:05










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449870%2fdelete-substring-from-json-file-malformated%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










sed approach:



sed -E 's~("MyPorts.*},|"PROTO[^"]+"| +PROTO[^"]+)~~' file


The output:



aaaaaaaa 
bbbbbbbbb
cccc"TEST"ccccc



Bonus approach for one-line string input:



$ s='aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaabbbb"PROTO 9080/tcp 9090/tcp"bbbbbcccc"TES^CPROTO 80 8080"ccccc'

$ sed -En 's~("MyPorts.+},|"PROTO[^"]+"| +PROTO[^"]+)~~gp' <<<"$s"
aaaaaaaabbbbbbbbbcccc"TEST"ccccc





share|improve this answer























  • Thank you very much. Works like a charm !!! Great and Short Solution.. Cannot mark this a solution due less reputation, Thanks RomanPerekhrest found it.
    – biohell
    Jun 14 at 20:28











  • Great job at interpreting the OP's request! @biohell can accept the answer, no minimum reputation is required.
    – simlev
    Jun 14 at 20:58










  • @RomanPerekhrest: if the sample data is in one line,your call doesn't work.. Can you chnage your call to reflect this ?
    – biohell
    Jun 15 at 9:40










  • @biohell, all additional cases should be described in the question (with respective input fragment)
    – RomanPerekhrest
    Jun 15 at 9:44










  • @@RomanPerekhrest: i updated the question.
    – biohell
    Jun 15 at 10:05














up vote
1
down vote



accepted










sed approach:



sed -E 's~("MyPorts.*},|"PROTO[^"]+"| +PROTO[^"]+)~~' file


The output:



aaaaaaaa 
bbbbbbbbb
cccc"TEST"ccccc



Bonus approach for one-line string input:



$ s='aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaabbbb"PROTO 9080/tcp 9090/tcp"bbbbbcccc"TES^CPROTO 80 8080"ccccc'

$ sed -En 's~("MyPorts.+},|"PROTO[^"]+"| +PROTO[^"]+)~~gp' <<<"$s"
aaaaaaaabbbbbbbbbcccc"TEST"ccccc





share|improve this answer























  • Thank you very much. Works like a charm !!! Great and Short Solution.. Cannot mark this a solution due less reputation, Thanks RomanPerekhrest found it.
    – biohell
    Jun 14 at 20:28











  • Great job at interpreting the OP's request! @biohell can accept the answer, no minimum reputation is required.
    – simlev
    Jun 14 at 20:58










  • @RomanPerekhrest: if the sample data is in one line,your call doesn't work.. Can you chnage your call to reflect this ?
    – biohell
    Jun 15 at 9:40










  • @biohell, all additional cases should be described in the question (with respective input fragment)
    – RomanPerekhrest
    Jun 15 at 9:44










  • @@RomanPerekhrest: i updated the question.
    – biohell
    Jun 15 at 10:05












up vote
1
down vote



accepted







up vote
1
down vote



accepted






sed approach:



sed -E 's~("MyPorts.*},|"PROTO[^"]+"| +PROTO[^"]+)~~' file


The output:



aaaaaaaa 
bbbbbbbbb
cccc"TEST"ccccc



Bonus approach for one-line string input:



$ s='aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaabbbb"PROTO 9080/tcp 9090/tcp"bbbbbcccc"TES^CPROTO 80 8080"ccccc'

$ sed -En 's~("MyPorts.+},|"PROTO[^"]+"| +PROTO[^"]+)~~gp' <<<"$s"
aaaaaaaabbbbbbbbbcccc"TEST"ccccc





share|improve this answer















sed approach:



sed -E 's~("MyPorts.*},|"PROTO[^"]+"| +PROTO[^"]+)~~' file


The output:



aaaaaaaa 
bbbbbbbbb
cccc"TEST"ccccc



Bonus approach for one-line string input:



$ s='aaaa"MyPorts":"80/tcp":,"8080/tcp":,"9080/tcp":,"9090/tcp":,aaaabbbb"PROTO 9080/tcp 9090/tcp"bbbbbcccc"TES^CPROTO 80 8080"ccccc'

$ sed -En 's~("MyPorts.+},|"PROTO[^"]+"| +PROTO[^"]+)~~gp' <<<"$s"
aaaaaaaabbbbbbbbbcccc"TEST"ccccc






share|improve this answer















share|improve this answer



share|improve this answer








edited Jun 15 at 10:11


























answered Jun 14 at 20:17









RomanPerekhrest

22.4k12144




22.4k12144











  • Thank you very much. Works like a charm !!! Great and Short Solution.. Cannot mark this a solution due less reputation, Thanks RomanPerekhrest found it.
    – biohell
    Jun 14 at 20:28











  • Great job at interpreting the OP's request! @biohell can accept the answer, no minimum reputation is required.
    – simlev
    Jun 14 at 20:58










  • @RomanPerekhrest: if the sample data is in one line,your call doesn't work.. Can you chnage your call to reflect this ?
    – biohell
    Jun 15 at 9:40










  • @biohell, all additional cases should be described in the question (with respective input fragment)
    – RomanPerekhrest
    Jun 15 at 9:44










  • @@RomanPerekhrest: i updated the question.
    – biohell
    Jun 15 at 10:05
















  • Thank you very much. Works like a charm !!! Great and Short Solution.. Cannot mark this a solution due less reputation, Thanks RomanPerekhrest found it.
    – biohell
    Jun 14 at 20:28











  • Great job at interpreting the OP's request! @biohell can accept the answer, no minimum reputation is required.
    – simlev
    Jun 14 at 20:58










  • @RomanPerekhrest: if the sample data is in one line,your call doesn't work.. Can you chnage your call to reflect this ?
    – biohell
    Jun 15 at 9:40










  • @biohell, all additional cases should be described in the question (with respective input fragment)
    – RomanPerekhrest
    Jun 15 at 9:44










  • @@RomanPerekhrest: i updated the question.
    – biohell
    Jun 15 at 10:05















Thank you very much. Works like a charm !!! Great and Short Solution.. Cannot mark this a solution due less reputation, Thanks RomanPerekhrest found it.
– biohell
Jun 14 at 20:28





Thank you very much. Works like a charm !!! Great and Short Solution.. Cannot mark this a solution due less reputation, Thanks RomanPerekhrest found it.
– biohell
Jun 14 at 20:28













Great job at interpreting the OP's request! @biohell can accept the answer, no minimum reputation is required.
– simlev
Jun 14 at 20:58




Great job at interpreting the OP's request! @biohell can accept the answer, no minimum reputation is required.
– simlev
Jun 14 at 20:58












@RomanPerekhrest: if the sample data is in one line,your call doesn't work.. Can you chnage your call to reflect this ?
– biohell
Jun 15 at 9:40




@RomanPerekhrest: if the sample data is in one line,your call doesn't work.. Can you chnage your call to reflect this ?
– biohell
Jun 15 at 9:40












@biohell, all additional cases should be described in the question (with respective input fragment)
– RomanPerekhrest
Jun 15 at 9:44




@biohell, all additional cases should be described in the question (with respective input fragment)
– RomanPerekhrest
Jun 15 at 9:44












@@RomanPerekhrest: i updated the question.
– biohell
Jun 15 at 10:05




@@RomanPerekhrest: i updated the question.
– biohell
Jun 15 at 10:05












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449870%2fdelete-substring-from-json-file-malformated%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Christian Cage

How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?