sed、単一のバックスラッシュを二重バックスラッシュに変換

sed、単一のバックスラッシュを二重バックスラッシュに変換

二重エスケープ/単一エスケープ改行文字を含むjson文字列があります。 Jsonパーサーは、文字列値に単一のバックスラッシュエスケープを許可しません。

脱出するにはすべて集める必要があります

内容は次のとおりです。

this newline must not be changed ---- \\n
this newline must be changed - \n

sed コマンドを実行すると、

 sed -i 's/([^\])\n/\\n/g' ~/Desktop/sedTest 

それは何も置き換えません

([^\])\n、このモードは、すでにバックスラッシュのあるモードを変更しないために使用されます。

ベストアンサー1

例の入力を考えると、次のようになります。

$ cat /tmp/foo
this newline must not be changed ---- \\n
this newline must be changed - \n

これはあなたが望むことを行うようです:

$ sed -e 's@\([^\]\)\\n@\1\\\\n@' /tmp/foo
this newline must not be changed ---- \\n
this newline must be changed - \\n

おすすめ記事