Removing multiple lines

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












1















I am trying to write up a command line interfaces that will removes a particular section / lines of codes within a list of json files. By the way, the json file are located within the sub-folders of the main directory



I am pretty new to this but this is the code that I can come up with so far - find -name "*.json" | xargs sed -i "map" but some of the json files I had, its format is slightly different



So far I am seeing the following 2 formats within my list:




"tags": ,
"map":
"KPA":
"State": True,
"namespace": "KPA01"





or




"tags":
"type": [
"char"
],
"dynamic": true
,
"map":
"KPA01":
"State": True,
"namespace": "KPA01"





and basically, I am trying to omit out the map section that it has, so that it will only display the tags section but the presence of commas and / are making it hard for me.
So my output results should be like this:




"tags":



or




"tags":
"type": [
"char"
],
"dynamic": true




Will this be possible to do so in a command line interface? I heard that jq may be able to do it, however, as I tried executing jq '.map' test.json I am getting parse error: ':' not as part of an object at line 2, column 11 in my terminal. Likewise it also seems to be giving off error if I am using the jq play online..



Any ideas?










share|improve this question
























  • It's not a very good idea to parse hierarchical markup languages (html/xml/json) with string processors that are not aware of the syntax. It always goes wrong somewhere (you can quickly see that you'd have to actually count braces to make this right in general). It would be much better to actually load json as an object, remove the property and output it again. I'd go for python. I'll post an answer shortly.

    – orion
    Jan 27 '15 at 9:05















1















I am trying to write up a command line interfaces that will removes a particular section / lines of codes within a list of json files. By the way, the json file are located within the sub-folders of the main directory



I am pretty new to this but this is the code that I can come up with so far - find -name "*.json" | xargs sed -i "map" but some of the json files I had, its format is slightly different



So far I am seeing the following 2 formats within my list:




"tags": ,
"map":
"KPA":
"State": True,
"namespace": "KPA01"





or




"tags":
"type": [
"char"
],
"dynamic": true
,
"map":
"KPA01":
"State": True,
"namespace": "KPA01"





and basically, I am trying to omit out the map section that it has, so that it will only display the tags section but the presence of commas and / are making it hard for me.
So my output results should be like this:




"tags":



or




"tags":
"type": [
"char"
],
"dynamic": true




Will this be possible to do so in a command line interface? I heard that jq may be able to do it, however, as I tried executing jq '.map' test.json I am getting parse error: ':' not as part of an object at line 2, column 11 in my terminal. Likewise it also seems to be giving off error if I am using the jq play online..



Any ideas?










share|improve this question
























  • It's not a very good idea to parse hierarchical markup languages (html/xml/json) with string processors that are not aware of the syntax. It always goes wrong somewhere (you can quickly see that you'd have to actually count braces to make this right in general). It would be much better to actually load json as an object, remove the property and output it again. I'd go for python. I'll post an answer shortly.

    – orion
    Jan 27 '15 at 9:05













1












1








1








I am trying to write up a command line interfaces that will removes a particular section / lines of codes within a list of json files. By the way, the json file are located within the sub-folders of the main directory



I am pretty new to this but this is the code that I can come up with so far - find -name "*.json" | xargs sed -i "map" but some of the json files I had, its format is slightly different



So far I am seeing the following 2 formats within my list:




"tags": ,
"map":
"KPA":
"State": True,
"namespace": "KPA01"





or




"tags":
"type": [
"char"
],
"dynamic": true
,
"map":
"KPA01":
"State": True,
"namespace": "KPA01"





and basically, I am trying to omit out the map section that it has, so that it will only display the tags section but the presence of commas and / are making it hard for me.
So my output results should be like this:




"tags":



or




"tags":
"type": [
"char"
],
"dynamic": true




Will this be possible to do so in a command line interface? I heard that jq may be able to do it, however, as I tried executing jq '.map' test.json I am getting parse error: ':' not as part of an object at line 2, column 11 in my terminal. Likewise it also seems to be giving off error if I am using the jq play online..



Any ideas?










share|improve this question
















I am trying to write up a command line interfaces that will removes a particular section / lines of codes within a list of json files. By the way, the json file are located within the sub-folders of the main directory



I am pretty new to this but this is the code that I can come up with so far - find -name "*.json" | xargs sed -i "map" but some of the json files I had, its format is slightly different



So far I am seeing the following 2 formats within my list:




"tags": ,
"map":
"KPA":
"State": True,
"namespace": "KPA01"





or




"tags":
"type": [
"char"
],
"dynamic": true
,
"map":
"KPA01":
"State": True,
"namespace": "KPA01"





and basically, I am trying to omit out the map section that it has, so that it will only display the tags section but the presence of commas and / are making it hard for me.
So my output results should be like this:




"tags":



or




"tags":
"type": [
"char"
],
"dynamic": true




Will this be possible to do so in a command line interface? I heard that jq may be able to do it, however, as I tried executing jq '.map' test.json I am getting parse error: ':' not as part of an object at line 2, column 11 in my terminal. Likewise it also seems to be giving off error if I am using the jq play online..



Any ideas?







command-line sed json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 30 '16 at 17:55









Thomas Dickey

52.5k596167




52.5k596167










asked Jan 27 '15 at 7:45









dissidiadissidia

12616




12616












  • It's not a very good idea to parse hierarchical markup languages (html/xml/json) with string processors that are not aware of the syntax. It always goes wrong somewhere (you can quickly see that you'd have to actually count braces to make this right in general). It would be much better to actually load json as an object, remove the property and output it again. I'd go for python. I'll post an answer shortly.

    – orion
    Jan 27 '15 at 9:05

















  • It's not a very good idea to parse hierarchical markup languages (html/xml/json) with string processors that are not aware of the syntax. It always goes wrong somewhere (you can quickly see that you'd have to actually count braces to make this right in general). It would be much better to actually load json as an object, remove the property and output it again. I'd go for python. I'll post an answer shortly.

    – orion
    Jan 27 '15 at 9:05
















It's not a very good idea to parse hierarchical markup languages (html/xml/json) with string processors that are not aware of the syntax. It always goes wrong somewhere (you can quickly see that you'd have to actually count braces to make this right in general). It would be much better to actually load json as an object, remove the property and output it again. I'd go for python. I'll post an answer shortly.

– orion
Jan 27 '15 at 9:05





It's not a very good idea to parse hierarchical markup languages (html/xml/json) with string processors that are not aware of the syntax. It always goes wrong somewhere (you can quickly see that you'd have to actually count braces to make this right in general). It would be much better to actually load json as an object, remove the property and output it again. I'd go for python. I'll post an answer shortly.

– orion
Jan 27 '15 at 9:05










4 Answers
4






active

oldest

votes


















2














First of all, change True to true. As a whole, this works very well:



#!/usr/bin/python
import sys
import json

inputfile = sys.argv[1]
with open(inputfile,'r') as myfile:
obj = json.loads(myfile.read().replace('True','true'))
if "map" in obj:
del obj["map"]
json.dump(obj,sys.stdout,indent=4,separators=(',',': '))


This writes to standard output.



EDIT: the previous in-place version seemed to be somewhat dangerous. Better do it this way:



#!/usr/bin/python
import sys
import json

inputfile = sys.argv[1]
with open(inputfile,'r') as myfile:
obj = json.loads(myfile.read().replace('True','true'))
if "map" in obj:
del obj["map"]
with open(inputfile,'w') as myfile:
json.dump(obj,myfile,indent=4,separators=(',',': '))


Because the script is actually aware of what a valid JSON is, it will throw an exception if it encounters invalid code, instead of producing unpredictable output.



This works on python 3, just so you know.



EDIT2:



You can modify the objects in any way you like, the purpose of Json is exactly serialization of objects. Treat them as associative arrays and give them any value you want. For instance, you can do this:



#add a new string on the "ground" level
obj["new_key"]="lol"
#add a new subarray, with properties of different types
obj["this_is_array"]="a": 3, "b": 16, "c": "string", "d": False
#modify the value of existing field
obj["new_key"]="new value"
#insert into subarray (test if it exists first)
if "this_is_array" in obj:
obj["this_is_array"]["e"]=42





share|improve this answer

























  • Pardon me. How am I suppose to run this? Run it within the directory python <Name of this script>?

    – dissidia
    Jan 27 '15 at 9:32











  • Either python <name of this script> <name of your json file> or just make it executable (chmod +x <script file>) and call it as ./script.py <json file>. The second variant modifies each file in place, so make a backup of json files before using it just in case.

    – orion
    Jan 27 '15 at 9:37











  • Is this not possible for it to run thru the directory? I have a massive list of json files to process :x

    – dissidia
    Jan 27 '15 at 9:41











  • This processes one file, it's much more flexible this way; you can loop through any set of chosen files in commandline. For instance, for file in *.json; do ./script.py "$file"; done, or find -name '*.json' -exec ./script.py '' ';' if you have them in many subdirectories.

    – orion
    Jan 27 '15 at 9:45












  • So when I tried the first code, whether it is r or r+, it doesn't seems to appends the changes to the file despite terminal is showing the correct output. However as I tried your second code, it either wipes out all the contents or throw this error - IOError: [Errno 22] Invalid argument in myfile.truncate(0)

    – dissidia
    Jan 27 '15 at 9:54


















1














If you handle the True > true as mentioned elsewhere and get the jq tool, you can just do:



jq 'tags' <infile


For example, after copying one of your examples to my clipboard:



xsel -bo | sed 's/True/true/g' | jq 'tags'


OUTPUT:




"tags":
"type": [
"char"
],
"dynamic": true







share|improve this answer
































    0














    I found same error:



    parse error: Invalid numeric literal at line 5, column 26


    and I'm not used to json but I think True should be in smallcase, as true, so you can run a perl one-liner to fix it and then use jq to filter out the map key, like:



    perl -pe 's/(W)T(rue)/$1t$2/g' file1.json | ./jq 'del(.map)'


    for first example, that yields:




    "tags":



    and:



    perl -pe 's/(W)T(rue)/$1t$2/g' file2.json | ./jq 'del(.map)'


    for the second one, that yields:




    "tags":
    "type": [
    "char"
    ],
    "dynamic": true







    share|improve this answer























    • I am actually trying to run the command directly on a directory that contains a massive list of such json files. Not to mention that the said files are all located in a folder of its own. As such, it will be pretty tedious if I were to run code by code depending on the format

      – dissidia
      Jan 27 '15 at 9:10











    • @dissidia: This is Unix, so I assume you can run a find command to get all your json files and pipe them to the perl command.

      – Birei
      Jan 27 '15 at 9:12











    • Sorry for this noob question, while the output results displayed in the Terminal is correct, however it is not reflecting the changes as it should be in the actual file. Am I missing something?

      – dissidia
      Jan 27 '15 at 9:17











    • By the way, using find -name "*.json" does not seems to work... I keep getting parse error: Invalid numeric literal at line 2, column 0

      – dissidia
      Jan 27 '15 at 9:24


















    0














    question is old, however for completeness of options, here's jtc based solution:



    bash $ jtc -pw'<map>l+0' input.json

    "tags":
    "dynamic": true,
    "type": [
    "char"
    ]


    bash $


    • it will find all labels "map" and purge'em all (while preserving the rest of json)





    share|improve this answer






















      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
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f181273%2fremoving-multiple-lines%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      First of all, change True to true. As a whole, this works very well:



      #!/usr/bin/python
      import sys
      import json

      inputfile = sys.argv[1]
      with open(inputfile,'r') as myfile:
      obj = json.loads(myfile.read().replace('True','true'))
      if "map" in obj:
      del obj["map"]
      json.dump(obj,sys.stdout,indent=4,separators=(',',': '))


      This writes to standard output.



      EDIT: the previous in-place version seemed to be somewhat dangerous. Better do it this way:



      #!/usr/bin/python
      import sys
      import json

      inputfile = sys.argv[1]
      with open(inputfile,'r') as myfile:
      obj = json.loads(myfile.read().replace('True','true'))
      if "map" in obj:
      del obj["map"]
      with open(inputfile,'w') as myfile:
      json.dump(obj,myfile,indent=4,separators=(',',': '))


      Because the script is actually aware of what a valid JSON is, it will throw an exception if it encounters invalid code, instead of producing unpredictable output.



      This works on python 3, just so you know.



      EDIT2:



      You can modify the objects in any way you like, the purpose of Json is exactly serialization of objects. Treat them as associative arrays and give them any value you want. For instance, you can do this:



      #add a new string on the "ground" level
      obj["new_key"]="lol"
      #add a new subarray, with properties of different types
      obj["this_is_array"]="a": 3, "b": 16, "c": "string", "d": False
      #modify the value of existing field
      obj["new_key"]="new value"
      #insert into subarray (test if it exists first)
      if "this_is_array" in obj:
      obj["this_is_array"]["e"]=42





      share|improve this answer

























      • Pardon me. How am I suppose to run this? Run it within the directory python <Name of this script>?

        – dissidia
        Jan 27 '15 at 9:32











      • Either python <name of this script> <name of your json file> or just make it executable (chmod +x <script file>) and call it as ./script.py <json file>. The second variant modifies each file in place, so make a backup of json files before using it just in case.

        – orion
        Jan 27 '15 at 9:37











      • Is this not possible for it to run thru the directory? I have a massive list of json files to process :x

        – dissidia
        Jan 27 '15 at 9:41











      • This processes one file, it's much more flexible this way; you can loop through any set of chosen files in commandline. For instance, for file in *.json; do ./script.py "$file"; done, or find -name '*.json' -exec ./script.py '' ';' if you have them in many subdirectories.

        – orion
        Jan 27 '15 at 9:45












      • So when I tried the first code, whether it is r or r+, it doesn't seems to appends the changes to the file despite terminal is showing the correct output. However as I tried your second code, it either wipes out all the contents or throw this error - IOError: [Errno 22] Invalid argument in myfile.truncate(0)

        – dissidia
        Jan 27 '15 at 9:54















      2














      First of all, change True to true. As a whole, this works very well:



      #!/usr/bin/python
      import sys
      import json

      inputfile = sys.argv[1]
      with open(inputfile,'r') as myfile:
      obj = json.loads(myfile.read().replace('True','true'))
      if "map" in obj:
      del obj["map"]
      json.dump(obj,sys.stdout,indent=4,separators=(',',': '))


      This writes to standard output.



      EDIT: the previous in-place version seemed to be somewhat dangerous. Better do it this way:



      #!/usr/bin/python
      import sys
      import json

      inputfile = sys.argv[1]
      with open(inputfile,'r') as myfile:
      obj = json.loads(myfile.read().replace('True','true'))
      if "map" in obj:
      del obj["map"]
      with open(inputfile,'w') as myfile:
      json.dump(obj,myfile,indent=4,separators=(',',': '))


      Because the script is actually aware of what a valid JSON is, it will throw an exception if it encounters invalid code, instead of producing unpredictable output.



      This works on python 3, just so you know.



      EDIT2:



      You can modify the objects in any way you like, the purpose of Json is exactly serialization of objects. Treat them as associative arrays and give them any value you want. For instance, you can do this:



      #add a new string on the "ground" level
      obj["new_key"]="lol"
      #add a new subarray, with properties of different types
      obj["this_is_array"]="a": 3, "b": 16, "c": "string", "d": False
      #modify the value of existing field
      obj["new_key"]="new value"
      #insert into subarray (test if it exists first)
      if "this_is_array" in obj:
      obj["this_is_array"]["e"]=42





      share|improve this answer

























      • Pardon me. How am I suppose to run this? Run it within the directory python <Name of this script>?

        – dissidia
        Jan 27 '15 at 9:32











      • Either python <name of this script> <name of your json file> or just make it executable (chmod +x <script file>) and call it as ./script.py <json file>. The second variant modifies each file in place, so make a backup of json files before using it just in case.

        – orion
        Jan 27 '15 at 9:37











      • Is this not possible for it to run thru the directory? I have a massive list of json files to process :x

        – dissidia
        Jan 27 '15 at 9:41











      • This processes one file, it's much more flexible this way; you can loop through any set of chosen files in commandline. For instance, for file in *.json; do ./script.py "$file"; done, or find -name '*.json' -exec ./script.py '' ';' if you have them in many subdirectories.

        – orion
        Jan 27 '15 at 9:45












      • So when I tried the first code, whether it is r or r+, it doesn't seems to appends the changes to the file despite terminal is showing the correct output. However as I tried your second code, it either wipes out all the contents or throw this error - IOError: [Errno 22] Invalid argument in myfile.truncate(0)

        – dissidia
        Jan 27 '15 at 9:54













      2












      2








      2







      First of all, change True to true. As a whole, this works very well:



      #!/usr/bin/python
      import sys
      import json

      inputfile = sys.argv[1]
      with open(inputfile,'r') as myfile:
      obj = json.loads(myfile.read().replace('True','true'))
      if "map" in obj:
      del obj["map"]
      json.dump(obj,sys.stdout,indent=4,separators=(',',': '))


      This writes to standard output.



      EDIT: the previous in-place version seemed to be somewhat dangerous. Better do it this way:



      #!/usr/bin/python
      import sys
      import json

      inputfile = sys.argv[1]
      with open(inputfile,'r') as myfile:
      obj = json.loads(myfile.read().replace('True','true'))
      if "map" in obj:
      del obj["map"]
      with open(inputfile,'w') as myfile:
      json.dump(obj,myfile,indent=4,separators=(',',': '))


      Because the script is actually aware of what a valid JSON is, it will throw an exception if it encounters invalid code, instead of producing unpredictable output.



      This works on python 3, just so you know.



      EDIT2:



      You can modify the objects in any way you like, the purpose of Json is exactly serialization of objects. Treat them as associative arrays and give them any value you want. For instance, you can do this:



      #add a new string on the "ground" level
      obj["new_key"]="lol"
      #add a new subarray, with properties of different types
      obj["this_is_array"]="a": 3, "b": 16, "c": "string", "d": False
      #modify the value of existing field
      obj["new_key"]="new value"
      #insert into subarray (test if it exists first)
      if "this_is_array" in obj:
      obj["this_is_array"]["e"]=42





      share|improve this answer















      First of all, change True to true. As a whole, this works very well:



      #!/usr/bin/python
      import sys
      import json

      inputfile = sys.argv[1]
      with open(inputfile,'r') as myfile:
      obj = json.loads(myfile.read().replace('True','true'))
      if "map" in obj:
      del obj["map"]
      json.dump(obj,sys.stdout,indent=4,separators=(',',': '))


      This writes to standard output.



      EDIT: the previous in-place version seemed to be somewhat dangerous. Better do it this way:



      #!/usr/bin/python
      import sys
      import json

      inputfile = sys.argv[1]
      with open(inputfile,'r') as myfile:
      obj = json.loads(myfile.read().replace('True','true'))
      if "map" in obj:
      del obj["map"]
      with open(inputfile,'w') as myfile:
      json.dump(obj,myfile,indent=4,separators=(',',': '))


      Because the script is actually aware of what a valid JSON is, it will throw an exception if it encounters invalid code, instead of producing unpredictable output.



      This works on python 3, just so you know.



      EDIT2:



      You can modify the objects in any way you like, the purpose of Json is exactly serialization of objects. Treat them as associative arrays and give them any value you want. For instance, you can do this:



      #add a new string on the "ground" level
      obj["new_key"]="lol"
      #add a new subarray, with properties of different types
      obj["this_is_array"]="a": 3, "b": 16, "c": "string", "d": False
      #modify the value of existing field
      obj["new_key"]="new value"
      #insert into subarray (test if it exists first)
      if "this_is_array" in obj:
      obj["this_is_array"]["e"]=42






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 28 '15 at 12:04

























      answered Jan 27 '15 at 9:24









      orionorion

      9,1331833




      9,1331833












      • Pardon me. How am I suppose to run this? Run it within the directory python <Name of this script>?

        – dissidia
        Jan 27 '15 at 9:32











      • Either python <name of this script> <name of your json file> or just make it executable (chmod +x <script file>) and call it as ./script.py <json file>. The second variant modifies each file in place, so make a backup of json files before using it just in case.

        – orion
        Jan 27 '15 at 9:37











      • Is this not possible for it to run thru the directory? I have a massive list of json files to process :x

        – dissidia
        Jan 27 '15 at 9:41











      • This processes one file, it's much more flexible this way; you can loop through any set of chosen files in commandline. For instance, for file in *.json; do ./script.py "$file"; done, or find -name '*.json' -exec ./script.py '' ';' if you have them in many subdirectories.

        – orion
        Jan 27 '15 at 9:45












      • So when I tried the first code, whether it is r or r+, it doesn't seems to appends the changes to the file despite terminal is showing the correct output. However as I tried your second code, it either wipes out all the contents or throw this error - IOError: [Errno 22] Invalid argument in myfile.truncate(0)

        – dissidia
        Jan 27 '15 at 9:54

















      • Pardon me. How am I suppose to run this? Run it within the directory python <Name of this script>?

        – dissidia
        Jan 27 '15 at 9:32











      • Either python <name of this script> <name of your json file> or just make it executable (chmod +x <script file>) and call it as ./script.py <json file>. The second variant modifies each file in place, so make a backup of json files before using it just in case.

        – orion
        Jan 27 '15 at 9:37











      • Is this not possible for it to run thru the directory? I have a massive list of json files to process :x

        – dissidia
        Jan 27 '15 at 9:41











      • This processes one file, it's much more flexible this way; you can loop through any set of chosen files in commandline. For instance, for file in *.json; do ./script.py "$file"; done, or find -name '*.json' -exec ./script.py '' ';' if you have them in many subdirectories.

        – orion
        Jan 27 '15 at 9:45












      • So when I tried the first code, whether it is r or r+, it doesn't seems to appends the changes to the file despite terminal is showing the correct output. However as I tried your second code, it either wipes out all the contents or throw this error - IOError: [Errno 22] Invalid argument in myfile.truncate(0)

        – dissidia
        Jan 27 '15 at 9:54
















      Pardon me. How am I suppose to run this? Run it within the directory python <Name of this script>?

      – dissidia
      Jan 27 '15 at 9:32





      Pardon me. How am I suppose to run this? Run it within the directory python <Name of this script>?

      – dissidia
      Jan 27 '15 at 9:32













      Either python <name of this script> <name of your json file> or just make it executable (chmod +x <script file>) and call it as ./script.py <json file>. The second variant modifies each file in place, so make a backup of json files before using it just in case.

      – orion
      Jan 27 '15 at 9:37





      Either python <name of this script> <name of your json file> or just make it executable (chmod +x <script file>) and call it as ./script.py <json file>. The second variant modifies each file in place, so make a backup of json files before using it just in case.

      – orion
      Jan 27 '15 at 9:37













      Is this not possible for it to run thru the directory? I have a massive list of json files to process :x

      – dissidia
      Jan 27 '15 at 9:41





      Is this not possible for it to run thru the directory? I have a massive list of json files to process :x

      – dissidia
      Jan 27 '15 at 9:41













      This processes one file, it's much more flexible this way; you can loop through any set of chosen files in commandline. For instance, for file in *.json; do ./script.py "$file"; done, or find -name '*.json' -exec ./script.py '' ';' if you have them in many subdirectories.

      – orion
      Jan 27 '15 at 9:45






      This processes one file, it's much more flexible this way; you can loop through any set of chosen files in commandline. For instance, for file in *.json; do ./script.py "$file"; done, or find -name '*.json' -exec ./script.py '' ';' if you have them in many subdirectories.

      – orion
      Jan 27 '15 at 9:45














      So when I tried the first code, whether it is r or r+, it doesn't seems to appends the changes to the file despite terminal is showing the correct output. However as I tried your second code, it either wipes out all the contents or throw this error - IOError: [Errno 22] Invalid argument in myfile.truncate(0)

      – dissidia
      Jan 27 '15 at 9:54





      So when I tried the first code, whether it is r or r+, it doesn't seems to appends the changes to the file despite terminal is showing the correct output. However as I tried your second code, it either wipes out all the contents or throw this error - IOError: [Errno 22] Invalid argument in myfile.truncate(0)

      – dissidia
      Jan 27 '15 at 9:54













      1














      If you handle the True > true as mentioned elsewhere and get the jq tool, you can just do:



      jq 'tags' <infile


      For example, after copying one of your examples to my clipboard:



      xsel -bo | sed 's/True/true/g' | jq 'tags'


      OUTPUT:




      "tags":
      "type": [
      "char"
      ],
      "dynamic": true







      share|improve this answer





























        1














        If you handle the True > true as mentioned elsewhere and get the jq tool, you can just do:



        jq 'tags' <infile


        For example, after copying one of your examples to my clipboard:



        xsel -bo | sed 's/True/true/g' | jq 'tags'


        OUTPUT:




        "tags":
        "type": [
        "char"
        ],
        "dynamic": true







        share|improve this answer



























          1












          1








          1







          If you handle the True > true as mentioned elsewhere and get the jq tool, you can just do:



          jq 'tags' <infile


          For example, after copying one of your examples to my clipboard:



          xsel -bo | sed 's/True/true/g' | jq 'tags'


          OUTPUT:




          "tags":
          "type": [
          "char"
          ],
          "dynamic": true







          share|improve this answer















          If you handle the True > true as mentioned elsewhere and get the jq tool, you can just do:



          jq 'tags' <infile


          For example, after copying one of your examples to my clipboard:



          xsel -bo | sed 's/True/true/g' | jq 'tags'


          OUTPUT:




          "tags":
          "type": [
          "char"
          ],
          "dynamic": true








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 27 '15 at 9:54

























          answered Jan 27 '15 at 9:40









          mikeservmikeserv

          45.5k668155




          45.5k668155





















              0














              I found same error:



              parse error: Invalid numeric literal at line 5, column 26


              and I'm not used to json but I think True should be in smallcase, as true, so you can run a perl one-liner to fix it and then use jq to filter out the map key, like:



              perl -pe 's/(W)T(rue)/$1t$2/g' file1.json | ./jq 'del(.map)'


              for first example, that yields:




              "tags":



              and:



              perl -pe 's/(W)T(rue)/$1t$2/g' file2.json | ./jq 'del(.map)'


              for the second one, that yields:




              "tags":
              "type": [
              "char"
              ],
              "dynamic": true







              share|improve this answer























              • I am actually trying to run the command directly on a directory that contains a massive list of such json files. Not to mention that the said files are all located in a folder of its own. As such, it will be pretty tedious if I were to run code by code depending on the format

                – dissidia
                Jan 27 '15 at 9:10











              • @dissidia: This is Unix, so I assume you can run a find command to get all your json files and pipe them to the perl command.

                – Birei
                Jan 27 '15 at 9:12











              • Sorry for this noob question, while the output results displayed in the Terminal is correct, however it is not reflecting the changes as it should be in the actual file. Am I missing something?

                – dissidia
                Jan 27 '15 at 9:17











              • By the way, using find -name "*.json" does not seems to work... I keep getting parse error: Invalid numeric literal at line 2, column 0

                – dissidia
                Jan 27 '15 at 9:24















              0














              I found same error:



              parse error: Invalid numeric literal at line 5, column 26


              and I'm not used to json but I think True should be in smallcase, as true, so you can run a perl one-liner to fix it and then use jq to filter out the map key, like:



              perl -pe 's/(W)T(rue)/$1t$2/g' file1.json | ./jq 'del(.map)'


              for first example, that yields:




              "tags":



              and:



              perl -pe 's/(W)T(rue)/$1t$2/g' file2.json | ./jq 'del(.map)'


              for the second one, that yields:




              "tags":
              "type": [
              "char"
              ],
              "dynamic": true







              share|improve this answer























              • I am actually trying to run the command directly on a directory that contains a massive list of such json files. Not to mention that the said files are all located in a folder of its own. As such, it will be pretty tedious if I were to run code by code depending on the format

                – dissidia
                Jan 27 '15 at 9:10











              • @dissidia: This is Unix, so I assume you can run a find command to get all your json files and pipe them to the perl command.

                – Birei
                Jan 27 '15 at 9:12











              • Sorry for this noob question, while the output results displayed in the Terminal is correct, however it is not reflecting the changes as it should be in the actual file. Am I missing something?

                – dissidia
                Jan 27 '15 at 9:17











              • By the way, using find -name "*.json" does not seems to work... I keep getting parse error: Invalid numeric literal at line 2, column 0

                – dissidia
                Jan 27 '15 at 9:24













              0












              0








              0







              I found same error:



              parse error: Invalid numeric literal at line 5, column 26


              and I'm not used to json but I think True should be in smallcase, as true, so you can run a perl one-liner to fix it and then use jq to filter out the map key, like:



              perl -pe 's/(W)T(rue)/$1t$2/g' file1.json | ./jq 'del(.map)'


              for first example, that yields:




              "tags":



              and:



              perl -pe 's/(W)T(rue)/$1t$2/g' file2.json | ./jq 'del(.map)'


              for the second one, that yields:




              "tags":
              "type": [
              "char"
              ],
              "dynamic": true







              share|improve this answer













              I found same error:



              parse error: Invalid numeric literal at line 5, column 26


              and I'm not used to json but I think True should be in smallcase, as true, so you can run a perl one-liner to fix it and then use jq to filter out the map key, like:



              perl -pe 's/(W)T(rue)/$1t$2/g' file1.json | ./jq 'del(.map)'


              for first example, that yields:




              "tags":



              and:



              perl -pe 's/(W)T(rue)/$1t$2/g' file2.json | ./jq 'del(.map)'


              for the second one, that yields:




              "tags":
              "type": [
              "char"
              ],
              "dynamic": true








              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 27 '15 at 9:07









              BireiBirei

              4,95221514




              4,95221514












              • I am actually trying to run the command directly on a directory that contains a massive list of such json files. Not to mention that the said files are all located in a folder of its own. As such, it will be pretty tedious if I were to run code by code depending on the format

                – dissidia
                Jan 27 '15 at 9:10











              • @dissidia: This is Unix, so I assume you can run a find command to get all your json files and pipe them to the perl command.

                – Birei
                Jan 27 '15 at 9:12











              • Sorry for this noob question, while the output results displayed in the Terminal is correct, however it is not reflecting the changes as it should be in the actual file. Am I missing something?

                – dissidia
                Jan 27 '15 at 9:17











              • By the way, using find -name "*.json" does not seems to work... I keep getting parse error: Invalid numeric literal at line 2, column 0

                – dissidia
                Jan 27 '15 at 9:24

















              • I am actually trying to run the command directly on a directory that contains a massive list of such json files. Not to mention that the said files are all located in a folder of its own. As such, it will be pretty tedious if I were to run code by code depending on the format

                – dissidia
                Jan 27 '15 at 9:10











              • @dissidia: This is Unix, so I assume you can run a find command to get all your json files and pipe them to the perl command.

                – Birei
                Jan 27 '15 at 9:12











              • Sorry for this noob question, while the output results displayed in the Terminal is correct, however it is not reflecting the changes as it should be in the actual file. Am I missing something?

                – dissidia
                Jan 27 '15 at 9:17











              • By the way, using find -name "*.json" does not seems to work... I keep getting parse error: Invalid numeric literal at line 2, column 0

                – dissidia
                Jan 27 '15 at 9:24
















              I am actually trying to run the command directly on a directory that contains a massive list of such json files. Not to mention that the said files are all located in a folder of its own. As such, it will be pretty tedious if I were to run code by code depending on the format

              – dissidia
              Jan 27 '15 at 9:10





              I am actually trying to run the command directly on a directory that contains a massive list of such json files. Not to mention that the said files are all located in a folder of its own. As such, it will be pretty tedious if I were to run code by code depending on the format

              – dissidia
              Jan 27 '15 at 9:10













              @dissidia: This is Unix, so I assume you can run a find command to get all your json files and pipe them to the perl command.

              – Birei
              Jan 27 '15 at 9:12





              @dissidia: This is Unix, so I assume you can run a find command to get all your json files and pipe them to the perl command.

              – Birei
              Jan 27 '15 at 9:12













              Sorry for this noob question, while the output results displayed in the Terminal is correct, however it is not reflecting the changes as it should be in the actual file. Am I missing something?

              – dissidia
              Jan 27 '15 at 9:17





              Sorry for this noob question, while the output results displayed in the Terminal is correct, however it is not reflecting the changes as it should be in the actual file. Am I missing something?

              – dissidia
              Jan 27 '15 at 9:17













              By the way, using find -name "*.json" does not seems to work... I keep getting parse error: Invalid numeric literal at line 2, column 0

              – dissidia
              Jan 27 '15 at 9:24





              By the way, using find -name "*.json" does not seems to work... I keep getting parse error: Invalid numeric literal at line 2, column 0

              – dissidia
              Jan 27 '15 at 9:24











              0














              question is old, however for completeness of options, here's jtc based solution:



              bash $ jtc -pw'<map>l+0' input.json

              "tags":
              "dynamic": true,
              "type": [
              "char"
              ]


              bash $


              • it will find all labels "map" and purge'em all (while preserving the rest of json)





              share|improve this answer



























                0














                question is old, however for completeness of options, here's jtc based solution:



                bash $ jtc -pw'<map>l+0' input.json

                "tags":
                "dynamic": true,
                "type": [
                "char"
                ]


                bash $


                • it will find all labels "map" and purge'em all (while preserving the rest of json)





                share|improve this answer

























                  0












                  0








                  0







                  question is old, however for completeness of options, here's jtc based solution:



                  bash $ jtc -pw'<map>l+0' input.json

                  "tags":
                  "dynamic": true,
                  "type": [
                  "char"
                  ]


                  bash $


                  • it will find all labels "map" and purge'em all (while preserving the rest of json)





                  share|improve this answer













                  question is old, however for completeness of options, here's jtc based solution:



                  bash $ jtc -pw'<map>l+0' input.json

                  "tags":
                  "dynamic": true,
                  "type": [
                  "char"
                  ]


                  bash $


                  • it will find all labels "map" and purge'em all (while preserving the rest of json)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 9 at 16:37









                  Dmitry L.Dmitry L.

                  112




                  112



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f181273%2fremoving-multiple-lines%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

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?