Skip to main content
added 194 characters in body
Source Link
Stéphane Chazelas
  • 584.4k
  • 96
  • 1.1k
  • 1.7k

Instead of matching [A-Za-z_] (which by the way matches a lot more than the English letters and underscore in most locales), use [^"] for any character other than " (by the way, the sample in your question has a (U+201D, RIGHT DOUBLE QUOTATION MARK) instead of a json quote (", U+0022, QUOTATION MARK) as the closing quote after the topic name, I'll assume that's not the case in your original files as that would make it invalid json):

sed 's/\("topic": "\)[^"]*/\1test_1/' < file.json

(note that it's your responsibility to make sure the topic name replacement is properly JSON-encoded: UTF-8 charset, control characters encoded (like \n for newline), and " encoded as \").

Instead of matching [A-Za-z_] (which by the way matches a lot more than the English letters and underscore in most locales), use [^"] for any character other than " (by the way, the sample in your question has a (U+201D, RIGHT DOUBLE QUOTATION MARK) instead of a json quote (", U+0022, QUOTATION MARK) as the closing quote after the topic name, I'll assume that's not the case in your original files as that would make it invalid json):

sed 's/\("topic": "\)[^"]*/\1test_1/' < file.json

Instead of matching [A-Za-z_] (which by the way matches a lot more than the English letters and underscore in most locales), use [^"] for any character other than " (by the way, the sample in your question has a (U+201D, RIGHT DOUBLE QUOTATION MARK) instead of a json quote (", U+0022, QUOTATION MARK) as the closing quote after the topic name, I'll assume that's not the case in your original files as that would make it invalid json):

sed 's/\("topic": "\)[^"]*/\1test_1/' < file.json

(note that it's your responsibility to make sure the topic name replacement is properly JSON-encoded: UTF-8 charset, control characters encoded (like \n for newline), and " encoded as \").

Source Link
Stéphane Chazelas
  • 584.4k
  • 96
  • 1.1k
  • 1.7k

Instead of matching [A-Za-z_] (which by the way matches a lot more than the English letters and underscore in most locales), use [^"] for any character other than " (by the way, the sample in your question has a (U+201D, RIGHT DOUBLE QUOTATION MARK) instead of a json quote (", U+0022, QUOTATION MARK) as the closing quote after the topic name, I'll assume that's not the case in your original files as that would make it invalid json):

sed 's/\("topic": "\)[^"]*/\1test_1/' < file.json