如何使用GREP获取特定数据?

时间:2014-07-04 04:35:16

标签: grep

命令:

grep -oP '(?<=\"name\":\")[^"]*|(?<=\"title\":\")[^"]*' *.json >newjson

o / p得到,

10XANY10G_1.json:chMax
10XANY10G_1.json:Max Frequency in GHz
10XANY10G_1.json:up
10XANY10G_1.json:UP
10XANY10G_1.json:down
10XANY10G_1.json:DOWN
10XANY10G_1.json:CapabilityList
10XANY10G_1.json:Capabilities
10XANY10G_1.json:encoding
10XANY10G_1.json:Encoding

预期o / p:

chMax:"Max Frequency in GHz",

up:"UP",

down:"DOWN",

文件内容:

{"card":{"cardName":"10AN10G","portSignalRates":["10AN10G-1-OTU2","10AN10G-1-OTU2E","10AN10G-1-TENGIGE","10AN10G-1-STM64"],"listOfPort":{"10AN10G-1-OTU2":{"portAid":"10AN10G-1-OTU2","signalType":"OTU2","tabNames":["PortDetails"],"requestType":{"PortDetails":"PTP"},"paramDetailsMap":{"PortDetails":[{"type":"dijit.form.TextBox","name":"signalType","title":"Signal Rate","id":"","options":[],"label":"","value":"OTU2","checked":"","enabled":"false","selected":""},{"type":"dijit.form.TextBox","name":"userLabel","title":"Description","id":"","options":[],"label":"","value":"","checked":"","enabled":"true","selected":""},{"type":"dijit.form.Select","name":"Frequency","title":"Transmit Frequency",}}}}}}

3 个答案:

答案 0 :(得分:0)

我认为你正在寻找这个,

$ grep -oP '(?<=\"name\":\")[^"]*|(?<=\"title\":)[^,]*' file
signalType
"Signal Rate"
userLabel
"Description"
Frequency
"Transmit Frequency"

获得所需的输出

$ grep -oP '(?<=\"name\":\")[^"]*|(?<=\"title\":)[^,]*' file | paste -d: - -
signalType:"Signal Rate"
userLabel:"Description"
Frequency:"Transmit Frequency"

答案 1 :(得分:0)

我认为您的问题是,您正在使用|对这两个群组进行ORUE尝试删除|并且您将更接近您要查找的内容,但您可能需要添加一个术语对于nametitle不是立即之后的情况,请跳过任何介入标记,然后您可能必须聪明地处理具有条目的情况名字但没有标题。

正如所说grep 解析json的最佳工具还有很多其他的 - 我个人建议使用python和json库来加载你的文件,然后输出你需要的标签。

答案 2 :(得分:0)

以下是gnu awk(由于RS)版本提取数据:

awk -F\" '/title/ {print $3":"$7}' RS='name' file
signalType:Signal Rate
userLabel:Description
Frequency:Transmit Frequency