0

I've looked around to find a solution to my problem in other posts listed bellow, but it looks my regex is quit different and need special care: How to output only captured groups with sed Replace one capture group with another with GNU sed (macOS) 4.4 sed replace line with capture groups

I'm trying to replace a regex match group in big JSON file, My file has mongoDB exported objects, and I'm trying to replace the objectId with the string:

{"_id":{"$oid":"56cad2ce0481320c111d2313"},"recordId":{"$oid":"56cad2ce0481320c111d2313"}}

So the output in the original file should look like this: {"_id":"56cad2ce0481320c111d2313","recordId":"56cad2ce0481320c111d2313"}

That's the command I run in the shell:

sed -i 's/(?:{"\$oid":)("\w+")}/\$1/g' data.json

I get no error, but the file remains the same. What exactly am I doing wrong?

yudarik
  • 113
  • 1
  • 12

1 Answers1

0

Finally I've managed to make it work, the way regex works in bash is different then in regexr.com tester tool.

echo '{"$oid":"56cad2ce0481320c111d2313"}' | sed 's/{"$oid":\("\w*"\)}/\1/g'

gives the correct output:

"56cad2ce0481320c111d2313"

I found it even better to read from stdin and output to file, instead of writing first to JSON file, then read, replace and write again.

Since I use mongoexport to export collection, replace the objectId and write the output to JSON file, my final solution looks like this:

mongoexport --host localhost --db myDB --collection my_collection | sed 's/{"$oid":\\("\\w*"\\)}/\\1/g' >> data.json
yudarik
  • 113
  • 1
  • 12
  • see also: https://unix.stackexchange.com/questions/119905/why-does-my-regular-expression-work-in-x-but-not-in-y – Sundeep Nov 14 '17 at 10:15