Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I instrumented a mvn build to emit profiling data, which I want to analyse later on. Therefore, I made the sed script below, to generate json-data from the profilers output. I call the script with

cat some.log | sed -nf thescript.sed > some.json

It produces a nested list of json-objects ("projects"), but each list is terminated with a ",".

To make a valid json file from this I perform the following steps in my favorite editor.

  • Joining everything to one line
  • insert a wrapping object by prepending "{\"list\":[" and appending "]}" to the whole content.
  • replace all occurences of ", ]" (wrongly terminated lists) by "]"
  • insert a line-break after each project, to avoid crashes in the JSON-parse due to the overly long line.

I perform these steps manually currently, and want to have this scripted too.

  • How can I either get the sed script to produce valid json (i.e. make linebreaks only when I want them, or dont put that terminating ",")
  • or how can I script the steps above in another filter ?

    /[PROFILER]/ s/\[PROFILER\] \(.*\)ms : Task (project-execute new-mvifp:\(.*\):\(.*\) (  task-segment: \[\(.*\)\] )) started./{"\2":{"timestamp":\1,"project":"\2","tasks":"\4","branch":"\3","segments":[/p
    /[PROFILER]/ s/\[PROFILER\] \(.*\)ms : Task (Segment:\(.*\)) started./{"\2":{"timestamp":\1,"segment":"\2","mojos":[/p
    /[PROFILER]/ s/\[PROFILER\] \(.*\)ms : Task (mojo-execute \(.*\) {execution: \(.*\)}) started./{"\2":{"timestamp":\1,"mojo":"\2","execution":"\3",/p
    /[PROFILER]/ s/\[PROFILER\] \(.*\)ms : Task (mojo-execute \(.*\) {execution: \(.*\)}) finished.*duration \(.*\)ms./"duration":\4}},/p
    /[PROFILER]/ s/\[PROFILER\] \(.*\)ms : Task (Segment:\(.*\)) finished.*duration \(.*\)ms./],"duration":\3}},/p
    /[PROFILER]/ s/\[PROFILER\] \(.*\)ms : Task (project-execute new-mvifp:\(.*\):\(.*\) (  task-segment: \[\(.*\)\] )) finished.*duration \(.*\)ms./],"duration":\5}},/p
share|improve this question
Consider providing a sample input file. – Daniel Beck Feb 7 '12 at 9:57 – Bastl Feb 7 '12 at 10:12
perhaps sed isnt the best choice for this. but it is easy to understand for me: linewise regexes are quickly written. But this is more like opening and closing tags, spanning over multiple lines. – Bastl Feb 7 '12 at 10:14
especially I'd like to know if sed has a "p" command that does not terminate with a newline. – Bastl Feb 7 '12 at 10:16

You must log in to answer this question.

Browse other questions tagged .