How can I improve or wrap my sed-script?

0

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
    

Bastl

Posted 2012-02-07T09:51:55.587

Reputation: 173

1Consider providing a sample input file. – Daniel Beck – 2012-02-07T09:57:08.747

http://pastebin.com/LEFyX5Py – Bastl – 2012-02-07T10:12:20.227

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 – 2012-02-07T10:14:51.533

especially I'd like to know if sed has a "p" command that does not terminate with a newline. – Bastl – 2012-02-07T10:16:27.957

No answers