简体   繁体   English

unix shell脚本-搜索字符串并格式化为json

[英]unix shell script - search string and formatting to json

I have a log file with multiple json message that is not formatted. 我有一个带有未格式化的多个json消息的日志文件。 Trying to write a shell script to search for a string in the logs and format the matching json and write to a file. 尝试编写Shell脚本以在日志中搜索字符串,并格式化匹配的json并写入文件。

For Example :- in the test.log file, 例如:-在test.log文件中,

type_logfile 2015-04-14 18:06:15,284 INFO ResponseLogService - {"header":{"time":"2015 06:06:13 PM EDT","reqId":"1","user":"baskar"}} type_logfile 2015-04-14 18:06:15,284信息ResponseLogService-{“ header”:{“ time”:“ 2015 06:06:13 PM EDT”,“ reqId”:“ 1”,“ user”:“ baskar” }}

type_logfile 2015-04-14 18:06:15,284 INFO ResponseLogService - {"header":{"time":"2015 06:06:13 PM EDT","reqId":"2","user":"raja"}} type_logfile 2015-04-14 18:06:15,284信息ResponseLogService-{“ header”:{“ time”:“ 2015 06:06:13 PM EDT”,“ reqId”:“ 2”,“ user”:“ raja” }}

type_logfile 2015-04-14 18:06:15,284 INFO ResponseLogService - {"header":{"time":"2015 06:06:13 PM EDT","reqId":"3","user":"baskar"}} type_logfile 2015-04-14 18:06:15,284信息ResponseLogService-{“ header”:{“ time”:“ 2015 06:06:13 PM EDT”,“ reqId”:“ 3”,“ user”:“ baskar” }}

I use the below command, 我使用以下命令,

grep -i "baskar" test.log | grep -o "{\"header\".*" | python -m json.tool > ~/search_result.log

The above command works fine if the search returns only one json message. 如果搜索仅返回一条json消息,则上述命令可以正常工作。 If there are multiple matches, this doesnt work and throw the following error. 如果有多个匹配项,则此操作无效,并引发以下错误。

Unable to decode the json message. 无法解码json消息。

Whereas, if i redirect the matched output to some file and then read the file line by line to format the json works. 而如果我将匹配的输出重定向到某个文件,然后逐行读取文件以格式化json格式。

 grep -i "baskar" test.log | grep -o "{\"header\".*" > ~/search_result.log

while read line
do
    name=$line    
    echo $name | python -m json.tool >> ~/formatted_search_result.log
done < ~/search_result.log

Since, writing to a temp file and then reading from the temp file to format the json hits the performance, looking for some efficient way of doing this. 由于写入临时文件然后从临时文件中读取以格式化json会影响性能,因此正在寻找一种有效的方法来执行此操作。

The expected output is, When i search for 预期的输出是,当我搜索时

grep -i "baskar" test.log grep -i“ baskar” test.log

,

{
    "header": {
      "user": "baskar",
      "reqId": "1",
      "time": "2015 06:06:13 PM EDT"
    }
  }
  {
    "header": {
      "user": "baskar",
      "reqId": "3",
      "time": "2015 06:06:13 PM EDT"
    }

} }

Thanks, Baskar.S 谢谢,Baskar.S

Use jq . 使用jq Given your sample text: 给定您的示例文本:

sed 's/.*Service - //' file | jq -s .

Remove the -s option if you don't want an array of objects. 如果您不需要对象数组,请删除-s选项。


You can filter the input to jq: 您可以将输入过滤到jq:

sed -n '/baskar/ s/.*Service - //p' file | jq  .

or let jq to the filtering 或让jq进行过滤

sed 's/.*Service - //' file | jq  'select(.header.user == "baskar")'

Either option produces 两种选择都会产生

{
  "header": {
    "user": "baskar",
    "reqId": "1",
    "time": "2015 06:06:13 PM EDT"
  }
}
{
  "header": {
    "user": "baskar",
    "reqId": "3",
    "time": "2015 06:06:13 PM EDT"
  }
}

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

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