Force Patch character differences to a file


Hello,
I have an englishStrings.json file and when I make new edits/ additions to it, I need to copy the new changes to all the other language string.json files.

Before making any changes to englishStrings, the englishStrings and otherLanguageStrings have the same keys and same number of lines but values differ.

I am trying to do something like this:

git diff -U0 —-word-diff-regex=. —-word-diff=porcelain englishStrings.json > changes.diff

patch frenchStrings.json < changes.diff

But it does not work for edits. The reason I want to apply character differences for when I insert a comma at the end of the key, value pair of an englishString and need to transfer it to another language strings file.

Examples:
EnglishString.json

{
  "a": "one",
  "b": "two",
  "test": "test",
  "c": "three",
  "edit": "edit"
}

FrenchString.json

{
“a”: “un”,
“b”: “deux”,
“c”: “trois”
}
git diff -U0 —-word-diff-regex=. —-word-diff=porcelain EnglishString.json > changes.diff
@@ -4 +4,3 @@
   "
+test": "test",
~
+  "
 c": "three"
+,
~
+  "edit": "edit"
~

Expecting after patch:

{
  "a": "un",
  "b": "deux",
  "test": "test",
  "c": "trois",
  "edit": "edit"
}

But patch is currently telling me that the patch file is malformed. Patch probably wont be able to help since line length can differ between English and French Strings but I would be open to recommendations to change up my solution

Asked By: Freezer Fridge

||

Rather than looking at the issue as a text patching problem, we may see it as a JSON structure mering task, overwriting the keys+values of one JSON data structure with those of another.

Assuming that the typographical (fancy) quotes in the French file are actually ordinary double quotes, you can use the JSON processor jq to merge the two data structures like this:

$ jq -s 'add' EnglishString.json FrenchString.json
{
  "a": "un",
  "b": "deux",
  "test": "test",
  "c": "trois",
  "edit": "edit"
}

This reads the "dictionary" of each file into an array element. The add instruction in the jq expression adds the two elements together, overwriting the keys+values read from the first file with the matching keys+values from the second.

In the example, the keys a, b, and c occur in both files, so the values from the last file are used.

Answered By: Kusalananda
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.