I’ve been looking all over the internet but had no result.
I’m trying to build a package generator (in shell/bash) and that respective package contains (one or more) json files. When browsing through a json file, if the user wants to delete one of the steps I must first take into consideration what if the step he wants to delete is the last. If so, the previous of the last step will contain a comma, but the json format does not permit that.
{ "operation_machinetype": "Ford", "operation_steps": [ "steps/Step_1/01_paint_the_car.json", "steps/Step_2/01_drive_the_car.json", "steps/Step_2/02_park_the_car.json" ] }
Example, If I delete ‘”steps/Step2/02_park_the_car.json”‘, then ‘”steps/Step2/01_drive_the_car.json”,’ will be the last step, but the comma will then cause an error.
Thank you in advance everyone.
Advertisement
Answer
$ cat tst.awk index($0,tgt) { if (!/,[[:space:]]*$/) { sub(/,[[:space:]]*$/,"",prev) } next } NR>1 { print prev } { prev = $0 } END { print prev } $ awk -v tgt='"steps/Step_2/02_park_the_car.json"' -f tst.awk file { "operation_machinetype": "Ford", "operation_steps": [ "steps/Step_1/01_paint_the_car.json", "steps/Step_2/01_drive_the_car.json" ] } $ awk -v tgt='"steps/Step_2/01_drive_the_car.json"' -f tst.awk file { "operation_machinetype": "Ford", "operation_steps": [ "steps/Step_1/01_paint_the_car.json", "steps/Step_2/02_park_the_car.json" ]