Tuesday, 15 February 2011

regex - String replace on a very large file -


I have a large text file that is JSON, you can see it here: I saved this JSON in a file named cards.json .

I have to change ' with \' .

Usually it is trivial in any editor, though the file is too large. How can I avoid all single quotes in this string?

Whatever I have tried:

  1. I use sed Tried my order sed s / \ / / \\\ '/ cards.json & gt; Cards_cleaned.json . However, there is no escape in the card_cleaned.json file ', it was just an exact copy of cards.json . When I sed s / \ / foobar / cards.json & gt;

  2. I have tried to use vim . I opened in cards.json vim $ vi cards.json . Then I tried a global string using :% s / '/ \' / g and it did not change anything in the file.

While responding to @Anvahwan or @Gboffi works , they produce and INVALID JSON

Backslash :

\ "\\ \ / \ b \ f \ n \ r \ t \ u char-hex As part of the following original (right) JSON "rare", "name": "[[" "description": "whenever a country enters the battleground, Mishra's eye has 2 losses to the controller of that land. "" Rare "," Name ":" Mishra's Eye "}]

You want to receive

< "Whenever a country enters the battleground, Mishra's eye 2" Code>
P> but it is an INVALID JSON.

Therefore, if you want (for some strange RE) son that backslash is , you will have to use the \\ twice, Like:

  [["description": "Whenever a land enters the battlefield, Mishra's eyes deal with the loss of 2 lands to the controller of that land.", "Rare ":" Rare "," name ":" Mishra's eye "}]  

solution (for both)

with perl

  perl -pE "s / '/ \\\' / g" & lt; Mtg_cards.json & gt; " 

and

  perl -pE" / / / \\ "\\ '/ g' for the wrong" land "of" land " Lt; Mtg_cards.json & gt; Card_with_double_BS.json #changes for "land \\ s"  

Ps: Because your file is only a long (30MB) line, There are several tools here, from

Code> command. You can safely use vim after "beautifying".


No comments:

Post a Comment