Unix [SOLVED]: How do I split a file into several files by a multi-character delimiter?

Unix [SOLVED]: How do I split a file into several files by a multi-character delimiter?

Home Forums Unix Unix [SOLVED]: How do I split a file into several files by a multi-character delimiter?

Tagged: , , , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36658

    Anonymous

    QuestionQuestion

    On Unix, without adding anything to the OS (i.e. only using grep, awk, sed, cut, etc.), how can I split the following input into several files (e.g. “_temp1.txt”, “_temp2.txt”, etc.) starting with each “codeView” line?
    Note, it is likely that the line begins with several spaces.

    What if the input is coming from an API instead of an existing file?

    . . .
    "events" : [ {
    "id" : "123456",
    "important" : true,
    "codeView" : {
      "lines" : [ {
        "fragments" : [ {
          "type" : "NORMAL_CODE",
          "value" : "str = wrapper.getParameter("
        }, {
          "type" : "NORMAL_CODE",
          "value" : ")"
        } ],
        "text" : "str = wrapper.getParameter("motif")"
      } ],
      "nested" : false
    },
    "probableStartLocationView" : {
      "lines" : [ {
        "fragments" : [ {
          "type" : "STACKTRACE_LINE",
          "value" : "<init>() @ JSONInputData.java:12"
        } ],
        "text" : "<init>() @ JSONInputData.java:92"
      } ],
      "nested" : false
    },
    "dataView" : {
      "lines" : [ {
        "fragments" : [ {
          "type" : "TAINT_VALUE",
          "value" : "CP"
        } ],
        "text" : "{{#taint}}CP{{/taint}}"
      } ],
      "nested" : false
    },
    "collapsedEvents" : [ ],
    "dupes" : 0
    }, {
    "id" : "28861,28862",
    "important" : false,
    "type" : "P2O",
    "description" : "String Operations Occurred",
    "extraDetails" : null,
              "codeView" : {
      "lines" : [ {
        "fragments" : [ {
          "type" : "TEXT",
          "value" : "Over the following lines of code, blah blah."
        } ],
        "text" : "Over the following lines of code, blah blah."
      } ],
      "nested" : false
    },
    "probableStartLocationView" : {
      "lines" : [ {
        "fragments" : [ {
          "type" : "STACKTRACE_LINE",
          "value" : "remplaceString() @ O_UtilCaractere.java:234"
        } ],
        "text" : "remplaceString() @ O_UtilCaractere.java:234"
      }, {
        "fragments" : [ {
          "type" : "STACKTRACE_LINE",
          "value" : "replaceString() @ O_UtilCaractere.java:333"
        } ],
        "text" : "replaceString() @ O_UtilCaractere.java:333"
      }, {
        "fragments" : [ {
          "type" : "STACKTRACE_LINE",
          "value" : "creerIncidentPaie() @ Incidents.java:444"
        } ],
        "text" : "creerIncidentPaie() @ Incidents.java:219"
      }, {
        "fragments" : [ {
          "type" : "STACKTRACE_LINE",
          "value" : "repliquerAbsenceIncident() @ Incidents.java:876"
        } ],
        "text" : "repliquerAbsenceIncident() @ IncidentsPaieMgr.java:882"
      } ],
      "nested" : false
    },
    "dataView" : {
      "lines" : [ {
        "fragments" : [ {
          "type" : "TEXT",
          "value" : "insert into TGE_INCIDENT...4', 'YYYYMMDD'), 'A', '"
        }, {
          "type" : "TAINT_VALUE",
          "value" : "CP"
        }, {
          "type" : "TEXT",
          "value" : "', '', null, 'T', 'ADPTVT', to_date('2013012214..."
        } ],
        "text" : "insert into TGE_INCIDENT...4', 'YYYYMMDD'), 'A', '{{#taint}}CP{{/taint}}', '', null, 'T', 'ADPTVT', to_date('2017062214..."
      } ],
      "nested" : false
    }
    . . .
    

    #36659

    Anonymous

    Accepted AnswerAnswer

    This will work robustly in any awk:

    awk '/"codeView"/{close(out); out="_temp" ++c ".txt"} out!=""{print > out}' file
    

    Source: https://stackoverflow.com/questions/47850466/how-do-i-split-a-file-into-several-files-by-a-multi-character-delimiter
    Author: Ed Morton
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.