1

I am trying to get rid of a load of content in a JSON file to have it prepped for translation using BBEdit.

a Line looks like this:

"ribbonText1" : "The text that needs to be translated",

I would like to remove everything so it ends up like this:

The text that needs to be translated

Any help greatly appreciated!!!

2 Answers2

0

First regexp to select a sting, regexps in sub to remove everything but text.

$ cat r.sh
awk '
/"ribbonText1"[ \t]*:/ {
    sub(/^[^:]*:/, "")
    sub(/^[^"]*"/, "")
    sub(/"[^"]*$/, "")
    print
}
' "$@"

Usage

$ sh r.sh file
The text that needs to be translated 
slitvinov
  • 5,693
  • 20
  • 31
0

Assuming the example shown is part of a JSON object and the key names will vary, something like this...

{
  "ribbonText1" : "The text that needs to be translated",
  "ribbonText2" : "The text that needs to be translated2",
  "ribbonText3" : "The text that needs to be translated3",
  "ribbonText4" : "The text that needs to be translated4",
  "ribbonText5" : "The text that needs to be translated5",
  "ribbonText6" : "The text that needs to be translated6"
}

This regex in BBEdit should do the trick:

Find: ^\s?[^:]+:\s?"([^"]+)",?$ Replace: \1

As Twometer notes in a comment though, this is fragile. I'd be inclined to use jq for this.

cat foo.json | jq '.[]'

steveax
  • 17,527
  • 6
  • 44
  • 59