简体   繁体   中英

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.

Is it possible with 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.

Note that the output isn't a valid JSON.

Use perl command line substitution as below, used the \\b tags for matching exact words and not mess up with other strings.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM