[英]Sed replace string with whitespaces
I have a string which has a format like this : 我有一个字符串,其格式如下:
{u city : u Zurich , u name : u Hauptbahnhof , u address : u Test address, C106, Bahnhofsstrasse }
I need to remove all the " u "
(with the spaces) and replace the ", u "
(with the spaces) with a line breaker, but i have no idea how i could do that. 我需要删除所有的
" u "
(带有空格),并用换行器替换", u "
(带有空格),但是我不知道该怎么做。
Is it possible with sed? sed有可能吗?
Output should be like 输出应该像
{city :Zurich
name :Hauptbahnhof
address :Test address, C106, Bahnhofsstrasse }
Thank you guys 感谢大伙们
The following seems to work (with some whitespace differences): 以下似乎有效(有一些空格差异):
's/, u /\n/g;s/\bu //g'
ie first replace all ", u "
with newlines, then remove all u
, where u
is not preceded by a word character. 即,首先用换行符替换所有
", u "
,然后删除所有u
,其中u
前面没有单词字符。
Note that the output isn't a valid JSON. 请注意,输出不是有效的JSON。
Use perl
command line substitution as below, used the \\b
tags for matching exact words and not mess up with other strings. 使用如下的
perl
命令行替换,使用\\b
标记来匹配确切的单词,而不会使其他字符串混乱。
perl -pe 's/\bu \b//g;' -pe 's/\b , \b/\n/g;' file
{city : Zurich
name : Hauptbahnhof
address : Test address, C106, Bahnhofsstrasse }
And as pointed by others, if it is a broken JSON
use jq
or other ways to fix it. 正如其他人指出的那样,如果
JSON
损坏,请使用jq
或其他方式对其进行修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.