在bash中解析结果

时间:2018-10-05 11:07:09

标签: bash grep

我有2个查询,这些查询会给我结果:

  1. 服务
  2. 服务数量

示例:

service1:2.3.4
service2:55.66

我想将第一个查询的第一个值与第二个查询的第一个值等连接起来。在此示例中,应为:

select t.process, sum(datediff(second, t.StartTime, t.EndTime))
from
(
  select distinct d1.process, min(d2.StartTime) StartTime, max(d2.EndTime) EndTime
  from data d1
  left join data d2 on d2.EndTime > d1.StartTime and d2.StartTime < d1.EndTime
  group by d1.process, d1.id
) t
group by t.process

2 个答案:

答案 0 :(得分:1)

没有示例文件,很难编写一个有效的示例。但是我看到,这两个值都来自同一文本文件和同一行。因此,我将使用 awk 来做到这一点:

$ cat text
service1;some_other_text;2.3.4
service2;just_some_text;55.66

$awk -F ";" '{printf "%s:%s\n",  $1, $3}' test 
service1:2.3.4

对于JSON文件,如果可以使用 jg (例如apt-get install jg),则会更容易:

$ cat test.json
[
  {
    "name": "service1",
    "customfield_10090": "1.2.3"
  },
  {
    "name": "service2",
    "customfield_10090": "23.3.2"
  }
]

$jq '.[] | .name + ":" + .customfield_10090' test.json | sed 's/"//g'
service1:1.2.3
service2:23.3.2

sed是消除引号所必需的。

答案 1 :(得分:0)

您可以使用paste

paste -d: <(grep name test| grep -v expand | cut -c 22- | rev | cut -c 3- | rev) \
          <(grep customfield test |  cut -c 31- | rev | cut -c 2- | rev)

但是可能会有更好的方法。如果输入的是json,则可以使用jq作为更短,更有效的解决方案。