在建立正则表达式模式时按jq中的值过滤 [英] FIltering by value in jq while building regex patterns

查看:170
本文介绍了在建立正则表达式模式时按jq中的值过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试创建输入的版本以输入到我的实际代码中时,我需要创建一个文件,该文件可以使包含"字符串,且该字符串包含大小写敏感的字符串,且其值为键值. 即构建正则表达式以实现包含" "camfrog,tubemate或soundclould".

While trying to create a version of input to feed into my actual code, I need to create a file that enable to get "contains" a string with case sensitive version of string, in a value for a key. i.e building a regex to implement "contains" "camfrog or tubemate, or soundclould" .

示例JSON输入:

{"appid":"537c6d4a9c4846b8bc44ebdf78ab8e2d","app_name":"TubeMate
YouTube Downloader","publisher_id":"1690d6387fcc441091a2f2d73f89709d"}
{"appid":"f8022204aaa7478a88fca1a417ddb125","app_name":"Camfrog
Android Smartphone","publisher_id":"085d0268a9674ce885a2f185ec895246"}
{"appid":"agltb3B1Yi1pbmNyDAsSA0FwcBih9tMUDA","app_name":"TuneIn Radio
- iPad","publisher_id":"agltb3B1Yi1pbmNyEAsSB0FjY291bnQYsv-PFAw"} {"appid":"537c6d4a9c4846b8bc44ebdf78ab8e2d","app_name":"TubeMate
YouTube Downloader","publisher_id":"1690d6387fcc441091a2f2d73f89709d"}
{"appid":"f8022204aaa7478a88fca1a417ddb125","app_name":"Camfrog
Android Smartphone","publisher_id":"085d0268a9674ce885a2f185ec895246"}
 {"appid":"92255b8b662148e59973b8eca128adde","app_name":"SubwaySimulator3D","publisher_id":"0d78f4d244ec4309b4aa06cdfb871341"}
{"appid":"agltb3B1Yi1pbmNyDAsSA0FwcBjq_6EUDA","app_name":"TuneIn
Radio","publisher_id":"agltb3B1Yi1pbmNyEAsSB0FjY291bnQYsv-PFAw"}
{"appid":"f7cc119ca9e1426c8d162d2d37c8558f","app_name":"Android Skout
New","publisher_id":"agltb3B1Yi1pbmNyEAsSB0FjY291bnQY7cCnEgw"}
{"appid":"agltb3B1Yi1pbmNyDAsSA0FwcBim6MAVDA","app_name":"Draw
Something
Android","publisher_id":"agltb3B1Yi1pbmNyEAsSB0FjY291bnQYgYC-FQw"}

从此Json输入中,我需要过滤名称类似于Camfrog的应用程序(可以是CAMFROG,camfrog等.因此,正则表达式必须不区分大小写.与此同时,我还需要输出一系列app_names,例如说"Camfrog","Tubemate","soundcloud"等. 我在这里查找了jq手册, http://stedolan.github.io/jq/manual/,但无法构建该表达式.

From this Json input I need to filter apps that have names "like" Camfrog (It can be CAMFROG, camfrog .. etc, so the regex has to be case insensitive.along with this, I need to output a series of app_names like, say "Camfrog", "Tubemate", "soundcloud" etc. I looked up the jq manual here, http://stedolan.github.io/jq/manual/, but couldn't build the expression.

这是我尝试的-:

 </home/ekta/Prototype1/sample.dat jq -c '{app_name:.app_name} |
 match(["Camfrog", "ig"])'  
 map(select(.app.name like "%Camfrog%" ))

但是我没有定义匹配&其编译错误.我该如何在Jq中做到这一点.

But I get match is not defined & a compile error thereof. How can I do this within Jq.

后备广告-: 我可以将其作为数据框加载到pandas中,并在那里进行正则表达式,但是由于我的文件中有很多我不需要的东西,因此我想在Jq中快速过滤.

fallback -: I could load this as a dataframe in pandas, and do the regex there,but since my file has a tons of things I dont really need, I would like to filter quickly in Jq.

过滤应用程序后的示例输出(我需要所有键,值与原始输出中的值相同:

sample output after filtering for the apps ( I need all the key,values as in orignal output-:

{"appid":"537c6d4a9c4846b8bc44ebdf78ab8e2d","app_name":"TubeMate
YouTube Downloader","publisher_id":"1690d6387fcc441091a2f2d73f89709d"}
{"appid":"f8022204aaa7478a88fca1a417ddb125","app_name":"Camfrog
Android Smartphone","publisher_id":"085d0268a9674ce885a2f185ec895246"}
{"appid":"537c6d4a9c4846b8bc44ebdf78ab8e2d","app_name":"TubeMate
YouTube Downloader","publisher_id":"1690d6387fcc441091a2f2d73f89709d"}
{"appid":"f8022204aaa7478a88fca1a417ddb125","app_name":"Camfrog
Android Smartphone","publisher_id":"085d0268a9674ce885a2f185ec895246"}

PPS:如果您可以教我钓鱼",而不仅仅是构建应该匹配的正则表达式,将不胜感激.

PPS : Will appreciate if you could "Teach me to fish", instead of just building the regex that should be matched.

关注问题-:

此外,当我尝试测试示例示例时,请在jq手册中,如-:

Also, when I try to test the sample examples, in the jq manual , like -:

echo [{"foo":1,"bar":2},{"foo":1,"bar":3},{"foo":4,"bar": 5}] | jq'unique(.foo)

echo [{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}] | jq 'unique(.foo)

我得到了错误:唯一(预期为0,但得到1)唯一(.foo)1编译错误的参数过多

I get , error: too many arguments to unique (expected 0 but got 1) unique(.foo)1 compile error

当jq手册读取时,示例示例如下-:

While the jq manual reads, a sample example as below -:

jq 'unique(.foo)'
Input [{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}]
Output    [{"foo": 1, "bar": 2}, {"foo": 4, "bar": 5}]

我还应该在这里尝试其他输入吗?

我构建词典的方式实际上是</home/ekta/SamplePrototype.dat jq -c '{appid:.app.id,,app_name:.app.name,publisher_id:.app.publisher_id}',但是我想像jq手册中所述进行测试.你能告诉我我在这里做错了什么吗?

The way I am building my dictionary really is , </home/ekta/SamplePrototype.dat jq -c '{appid:.app.id,,app_name:.app.name,publisher_id:.app.publisher_id}' , but I would like to test things as in jq manual. Could you give me pointers about what am I doing wrong here ?

推荐答案

这是我们的老好朋友Grep(和egrep)为我工作的

Here's what worked for my, by our old good friend, Grep(and egrep)

$<sample.dat  jq -c '{appid:.appid,app_name:.app_name}'  | egrep -i "camfrog|draw something"
{"appid":"f8022204aaa7478a88fca1a417ddb125","app_name":"Camfrog Android Smartphone"}
{"appid":"f8022204aaa7478a88fca1a417ddb125","app_name":"Camfrog Android Smartphone"}
{"appid":"agltb3B1Yi1pbmNyDAsSA0FwcBim6MAVDA","app_name":"Draw Something Android"}

这篇关于在建立正则表达式模式时按jq中的值过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆