簡體   English   中英

使用sed和regex用空間替換json值

[英]Substitute json value with space using sed and regex

我有多個json文件,看起來像下面的示例:

#sample json    
{"urlCurrent":"https://www.website1.com/inside/377/388/408/8002.html?utm_source=source&utm_medium=Click&utm_campaign=123","id":"00001"}
{"urlCurrent":"https://127.0.0.1/inside/414/756/765/34984.html","id":"00002"}
{"urlCurrent":"https://msdn.anything.com/en-us","id":"00002"}
{"urlCurrent":"https://web.something.com/","id":"00002"}

我希望json成為:

#result json    
{"urlCurrent":"https://www.website1.com/","id":"00001"}
{"urlCurrent":"https://127.0.0.1/","id":"00002"}
{"urlCurrent":"https://msdn.anything.com/","id":"00002"}
{"urlCurrent":"https://web.something.com/","id":"00002"}

我認為

sed -i 's/{regular expression}/\ /g' sample.json

用空格代替/之后的任何東西,都可以實現結果。 但是,我不知道如何使用正則表達式來匹配所需的模式。 我也不知道我應該搜索哪個關鍵字來實現這一目標。

有沒有辦法截斷urlCurrent成為我需要的結果? 提前致謝!


12/23更新:

sed -E -i -r 's!(http|ftp|https)://([0-9a-zA-Z\.]+)([0-9a-zA-Z\/\.?#=_&%~+-]+)!\2!g' sample.json
sed -i -r 's/(.*:\/\/?[^\/]+\/?)[^\"]*(.*)/\1\2/' sample.json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM