如何在json数据的两个反斜杠之间提取特定的数据/字符串

时间:2019-05-13 08:20:20

标签: regex shell

我只想从下面的代码中提取载波部分(u0026carrier = Jio%204G):

/v1.6/32359233/init?os=android%20%3A%208.0.0%20%3A%20O%20%3A%20sdk%3D26\u0026model=SM-J810G\u0026carrier=Jio%204G\u0026ct=13\u0026bundle=io.teslatech.callbreak\u0026mcc=405870\u0026d=1.75\u0026screen=1280%2C720\u0026optout=0\u0026ts=1556672633159\u0026manufacturer=samsung\u0026locale=en\u0026di=5.383004822116628\u0026play_version=11400000\u0026admob_enabled=true\u0026facebook%20enabled=false\u0026mopub%20enabled=true\u0026unit_id=unit-3943\u0026unit_id=unit-3944\u0026unit_id=float-3335\u0026unit_id=float-3336\u0026unit_id=float-3339","method":"GET","version":"v1.6","prefix":"hit","hostname":"adxserver-7d6bcd74d8-5zxsn

载体内容的长度不同。因此。我想提取两个反斜杠之间的数据,而不考虑其长度。

1 个答案:

答案 0 :(得分:0)

perl -lne 'print $1 if(/.*?(\\u[^\\u]+carrier=[^\\u]+)\\u.*/)' your_file

经过以下测试:

$ cat temp
/v1.6/32359233/init?os=android%20%3A%208.0.0%20%3A%20O%20%3A%20sdk%3D26\u0026model=SM-J810G\u0026carrier=Jio%204G\u0026ct=13\u0026bundle=io.teslatech.callbreak\u0026mcc=405870\u0026d=1.75\u0026screen=1280%2C720\u0026optout=0\u0026ts=1556672633159\u0026manufacturer=samsung\u0026locale=en\u0026di=5.383004822116628\u0026play_version=11400000\u0026admob_enabled=true\u0026facebook%20enabled=false\u0026mopub%20enabled=true\u0026unit_id=unit-3943\u0026unit_id=unit-3944\u0026unit_id=float-3335\u0026unit_id=float-3336\u0026unit_id=float-3339","method":"GET","version":"v1.6","prefix":"hit","hostname":"adxserver-7d6bcd74d8-5zxsn

$ perl -lne 'print $1 if(/.*?(\\u[^\\u]+carrier=[^\\u]+)\\u.*/)' temp
\u0026carrier=Jio%204G
$