2

次の2行のような入力があります。

TASK1,6,INITIAL,2013-01-15 19:20:40,PREPARING,2013-01-15 19:21:12,SCHEDULED,2013-01-15 19:21:13,TRANSLATING,2013-01-15 19:21:13,LOADING,2013-01-15 19:36:37,COMPLETE,2013-01-15 19:36:42
TASK2,5,INITIAL,2013-01-15 19:20:44,PREPARING,2013-01-15 19:21:13,SCHEDULED,2013-01-15 19:21:14,TRANSLATING,2013-01-15 19:36:37,TERMINAL,2013-01-15 20:28:10

これらの行を含むファイルをループする必要があり、各行についていくつかの時間差を計算します...計算などは問題ありませんが、これを解析する方法を理解しようとすると、時間の悪魔がいます。可変長"名前と値のペアの文字列。。。

基本的に、タスク#の後の#は、「ステータス」の数とそれに続くそれらのステータスおよびそれらの発生時間です。

私がやりたいのは、行の1つを取得して、それぞれの変数に値が割り当てられたこのようなものになってしまうことです。(例として最初の行を使用):

$TASK_ID=TASK1
$STATUS_COUNT=6
$INITIAL=2013-01-15 19:20:40
$PREPARING=2013-01-15 19:21:12
$SCHEDULED=2013-01-15 19:21:12
$TRANSLATING=2013-01-15 19:21:13
$LOADING=2013-01-15 19:36:37
$COMPLETE=2013-01-15 19:36:42
$TERMINAL=<NULL>

問題を悪化させるのは、タスクが複数回送信された場合、次のステータスのラウンドが最初のセットに追加されるだけで、次のような入力行が表示される可能性があることです。

TASK1,11,INITIAL,2013-01-15 19:20:40,PREPARING,2013-01-15 19:21:12,SCHEDULED,2013-01-15 19:21:13,TRANSLATING,2013-01-15 19:21:13,LOADING,2013-01-15 19:36:37,COMPLETE,2013-01-15 19:36:42,INITIAL,2013-01-15 20:20:40,PREPARING,2013-01-15 20:21:12,SCHEDULED,2013-01-15 20:21:13,TRANSLATING,2013-01-15 20:21:13,TERMINAL,2013-01-15 20:36:42

この場合、出力を次のようにします。

$TASK_ID=TASK1
$STATUS_COUNT=11
$INITIAL=2013-01-15 20:20:40
$PREPARING=2013-01-15 20:21:12
$SCHEDULED=2013-01-15 20:21:12
$TRANSLATING=2013-01-15 20:21:13
$LOADING=<NULL>
$COMPLETE=<NULL>
$TERMINAL=2013-01-15 20:36:42

私はこれにかなり困惑しています、誰か助けてもらえますか?

前もって感謝します

4

1 に答える 1

3
#!/bin/bash

# Splitting on commas, read the task ID and status count followed by all of the statuses,
# which we'll parse later.
while IFS=, read -r TASK_ID STATUS_COUNT STATUSES; do
(
    # Subtly, but importantly, we put the loop body inside parentheses so each loop
    # iteration runs in a sub-shell. This ensures that the $LOADING, $COMPLETE, etc.
    # variables we set don't leak into future iterations.

    echo "TASK_ID      = $TASK_ID"
    echo "STATUS_COUNT = $STATUS_COUNT"

    # Convert the comma-separated string $STATUSES into an array using `read -a'.
    IFS=, read -ra STATUSES <<< "$STATUSES"

    # Assign the statuses to named variables. A side benefit of this is that only the
    # last value of each status type is used.
    for ((i = 0; i < ${#STATUSES[@]}; i += 2)); do
        declare "${STATUSES[$i]}=${STATUSES[$((i+1))]}"
    done

    # Print each of the statuses, or <NULL> if that stage wasn't listed.
    echo "INITIAL      = ${INITIAL:-<NULL>}"
    echo "PREPARING    = ${PREPARING:-<NULL>}"
    echo "SCHEDULED    = ${SCHEDULED:-<NULL>}"
    echo "TRANSLATING  = ${TRANSLATING:-<NULL>}"
    echo "LOADING      = ${LOADING:-<NULL>}"
    echo "COMPLETE     = ${COMPLETE:-<NULL>}"
    echo "TERMINAL     = ${TERMINAL:-<NULL>}"

    echo
)
done

出力:

$ ./tasks < tasks.txt
TASK_ID      = TASK1
STATUS_COUNT = 6
INITIAL      = 2013-01-15 19:20:40
PREPARING    = 2013-01-15 19:21:12
SCHEDULED    = 2013-01-15 19:21:13
TRANSLATING  = 2013-01-15 19:21:13
LOADING      = 2013-01-15 19:36:37
COMPLETE     = 2013-01-15 19:36:42
TERMINAL     = <NULL>

TASK_ID      = TASK2
STATUS_COUNT = 5
INITIAL      = 2013-01-15 19:20:44
PREPARING    = 2013-01-15 19:21:13
SCHEDULED    = 2013-01-15 19:21:14
TRANSLATING  = 2013-01-15 19:36:37
LOADING      = <NULL>
COMPLETE     = <NULL>
TERMINAL     = 2013-01-15 20:28:10

TASK_ID      = TASK1
STATUS_COUNT = 11
INITIAL      = 2013-01-15 20:20:40
PREPARING    = 2013-01-15 20:21:12
SCHEDULED    = 2013-01-15 20:21:13
TRANSLATING  = 2013-01-15 20:21:13
LOADING      = 2013-01-15 19:36:37
COMPLETE     = 2013-01-15 19:36:42
TERMINAL     = 2013-01-15 20:36:42

(Glenn Jackmanが新しい要件に基づいて編集を追加)

events=(INITIAL PREPARING SCHEDULED TRANSLATING LOADING COMPLETE TERMINAL)

while IFS=, read -r TASK_ID STATUS_COUNT rest; do
    IFS=, read -ra STATUSES <<< "$rest"

    for (( i=0; i < ${#STATUSES[@]}; i+=2 )); do
        # if this this the initial event, reset all statuses
        if [[ ${STATUSES[i]} == ${events[0]} ]]; then
            for event in "${events[@]}"; do
                declare "$event="
            done
        fi
        declare "${STATUSES[i]}=${STATUSES[i+1]}"
    done
    for var in TASK_ID STATUS_COUNT "${events[@]}"; do
        printf "$%s = %s\n" $var "${!var:-<NULL>}"
    done

done
于 2013-01-22T23:08:08.683 に答える