简体   繁体   English

匹配模式后提取子字符串

[英]Extract substring after matching pattern

I am trying to extract a substring after matching a pattern in a string. 我试图在匹配字符串中的模式后提取子字符串。

Now I can't share my hole file but let's take this example. 现在我无法分享我的孔文件,但让我们来看看这个例子。

From this string: 从这个字符串:

{"code":"S02A5","name":"18\" Leichtmetallräder Doppelspeiche 397","price":"0","standard":"false"}

I want to extract this substring 我想提取这个子字符串

18\" Leichtmetallräder Doppelspeiche 397

So far I tried the following : 到目前为止我尝试了以下内容:

This matches to many results 这符合许多结果

grep -oP '(?<="code":".....","name":")[^"]+'

I know that the first char after "name":" is always 1 , so I tried to use this in the following command, and the return is 8\\ which is not that bad because I can add the 1 afterwards. 我知道“name”之后的第一个字符:“总是1 ,所以我试着在下面的命令中使用它,返回是8 \\ ,这并不是很糟糕,因为我之后可以添加1

grep -oP '(?<="code":".....","name":"1)[^"]+'

The problem is that I can't find a way to retrieve the rest of the substring needed, because there's an extra quotation mark after that backslash. 问题是我找不到一种方法来检索所需的其余子字符串,因为在该反斜杠后面有一个额外的引号。

Any ideas how can I solve this? 任何想法如何解决这个问题?

That looks like JSON, use for example jq : 看起来像JSON,例如使用jq

$ jq '.name' file
"18\" Leichtmetallräder Doppelspeiche 397"

or 要么

$ jq -r '.name' file
18" Leichtmetallräder Doppelspeiche 397

Update : 更新

If you need to use grep 如果你需要使用grep

$ grep -oP '(?<="name":")(\\"|[^"])+' file
18\" Leichtmetallräder Doppelspeiche 397

Explained: 解释:

  • (?<="name":") positive lookbehind preceeded by "name":" (?<="name":")正面看后面"name":"
  • followed by \\" s or non-quotes 其次是\\"\\"或“非\\"

OR : 或者

Maybe it should be: 也许它应该是:

$ grep -oP '(?<="name":")((?<![^\\]\\)\\"|[^"])+' file

since that would match \\" and \\\\\\" but not \\\\" 因为那将匹配\\"\\\\\\"但不匹配\\\\"

If you are considering Perl, this should work 如果您正在考虑Perl,这应该可行

/tmp> export data='{"code":"S02A5","name":"18\" Leichtmetallräder Doppelspeiche 397","price":"0","standard":"false"}'
/tmp> echo $data | perl -ne  ' /\"name\":(.+?),/ and print "$1\n" '
"18\" Leichtmetallräder Doppelspeiche 397"
/tmp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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