Задать вопрос
@SteepNET

Преобразовать в JSON?

Добрый день!
Имеем такой вывод
default has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 0s
   No Members
   No Callers

110 has 0 calls (max unlimited) in 'rrmemory' strategy (100s holdtime, 78s talktime), W:0, C:12, A:18, SL:25.0% within 60s
   Members: 
      Local/801@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 4 calls (last was 6516 secs ago)
      Local/769@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 1 calls (last was 4535 secs ago)
      Local/744@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/739@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 1 calls (last was 6952 secs ago)
      Local/771@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 3 calls (last was 1905 secs ago)
      Local/756@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 1 calls (last was 9914 secs ago)
      Local/746@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 1 calls (last was 2874 secs ago)
      Local/774@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/755@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/748@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/779@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/726@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/728@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
   No Callers


Нужно все убрать и оставить только
(110)и(0) рядом с ним, это группа и сколько линий и перевести все это в json
Делаю так , по извращенски..

$data = shell_exec("asterisk -rx 'queue show'| grep 110");
$order   = array("has", "calls");
$data = str_replace($order, '', $data);
$data = substr($data, 0, -107);
/*echo "<pre>$data</pre>";*/

$data = explode(PHP_EOL, $data);
$result = [];
for($i = 0; $i < count($data); $i++){
$temp = explode(':', $data[$i]);
$result[$temp[0]] = $temp[1];
}
array_pop($result);
$result = json_encode($result);
echo '<pre>';
var_dump($result);
echo '</pre>';


Вывод string '[]' (length=2)
  • Вопрос задан
  • 291 просмотр
Подписаться 3 Простой 3 комментария
Решения вопроса 1
Expany
@Expany
$this->get('skill');
<?php
$regexp = '/\d+\s\w+\s\d+/iu'; // ыыы, регуларке обожае
// Тупа твой вывод ыыы
$response = "default has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 0s
   No Members
   No Callers

110 has 0 calls (max unlimited) in 'rrmemory' strategy (100s holdtime, 78s talktime), W:0, C:12, A:18, SL:25.0% within 60s
   Members: 
      Local/801@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 4 calls (last was 6516 secs ago)
      Local/769@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 1 calls (last was 4535 secs ago)
      Local/744@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/739@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 1 calls (last was 6952 secs ago)
      Local/771@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 3 calls (last was 1905 secs ago)
      Local/756@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 1 calls (last was 9914 secs ago)
      Local/746@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken 1 calls (last was 2874 secs ago)
      Local/774@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/755@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/748@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/779@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/726@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
      Local/728@from-queue/n (ringinuse disabled)[1;36;40m (dynamic)[0m[0m[0m[0m ([1;32;40mNot in use[0m) has taken no calls yet
   No Callers";
// хо ба
preg_match($regexp,$response,$match);
// не ну тут всо ясно
$match = explode(' ',$match[0]);
$json = [$match[0] => $match[2]]; // не ну а чо :D
echo json_encode($json);
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
trapwalker
@trapwalker
Программист, энтузиаст
import sys
import re
import json


r1 = re.compile(
    r"(?P<who>\S*) has (?P<ncalls>\d*) calls \(max (?P<maxcalls>\S*)\) in \'(?P<start>\S*)\' strategy \((?P<holdtime>\S*) holdtime, (?P<talktime>\S*) talktime\), W:(?P<w>\d*), C:(?P<c>\d*), A:(?P<a>\d*), SL:(?P<sl>\S*) within (?P<within>\S*)\s*$"
)


with open('ast.txt') as f:

    js = []
    append_to = []
    m = None
    for l in f:
        m = r1.match(l)
        if m:
            js.append(dict(members=[], callers=[], **m.groupdict()))
            m = None
        elif 'No Members' in l or 'No Callers' in l:
            pass
        elif 'Members:' in l:
            append_to = js[-1]['members']
        elif 'Callers:' in l:
            append_to = js[-1]['callers']
        elif l.strip():
            append_to.append(l)

    print(json.dumps(js, indent=2))


Также можно парсить еще одним регекспом и строки второго уровня. Если надо, напишу вам регексп.
Или что вам там требуется вообще?

Вот ещё вариант:
cat logfile.log | py -x "getattr(re.match('^(\S+) has (\S+) calls.*$', x), 'groups', lambda: None)()" | py -x "dict([eval(x)])"

Или так:
cat logfile.log | py -x "'{{\"{0}\": \"{1}\"}}'.format(*x.split()) if x.split()[1:2] == ['has'] and x.split()[3:4] == ['calls'] else None"
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы