2014-05-02 56 views
3

需要您使用以下awk語法的幫助。下面是我的捲曲輸出,我需要完善它一點點:從捲曲輸出AWK的時間,日期,狀態和順序

輸入:

 RSYNCA-BACKUP 
     RCYNCA 20140517 0021 2182097 2082097 
     2014820905820917 10:03:54 
     2014820905820917 10:37:43 
     0:33:49 


     RSYNCB-COPY 
     20140517 0020 2082097 1982097 7 6 20 
     2014820905820917 09:32:20 
     2014820905820917 10:59:20 
     1:27:00 


     RSYNCC 
     RCYNCE 20140517 0021 2182097 2082097 
     2014820905820917 10:03:54 
     2014820905820917 10:37:43 
     0:33:49 

     RSYNCD 
     20140517 0020 2082097 1982097 7 6 20 
     2014820905820917 09:32:20 
     2014820905820917 10:59:20 
     1:27:00 

輸出I接收使用AWK:

RSYNCA-BACKUP|20140502|RCYNCA|10:02:15|10:56:42|0:54:27|FINISHED 
RSYNCB-COPY|0022||15:31:06|  |0:06:04|INITIATED 

Job Name|sequence|date|start time|end time|runtime|status 

對於啓動s的工作tatus沒有結束時間,因此本場可空

那是什麼我運行,並得到搞砸AWK輸出

awk -v RS='FINISHED|INITIATED' -v OFS='|' '$0 { print $1, $3, $2, $8, RS }' 

RSYNCJOBNA|0021|20140502|2014820905820902|FINISHED|INITIATED 
RSYNCJOBNA|0022|20140502|2014820905820902|FINISHED|INITIATED 

我從捲曲輸入具有額外的空間我想,這可能是這個問題,這裏是一個真實的例子:

INITIATED 
      RSYNCA 
      20140502 0036 3682096 3582096 6 5 
      2014820905820902 17:31:08 
       0:17:16 ce eque 
      INITIATED 
      RSYNCA 
      20140502 0035 3582096 3482096 6 5 
      2014820905820902 17:01:10 
       0:47:14 ce eque 
      FINISHED 
      RSYNCA 
      20140502 0034 3482096 3382096 6 5 
      2014820905820902 16:31:03 
      2014820905820902 17:24:45 
      0:53:42 
      FINISHED 
      RSYNCA 
      20140502 0033 3382096 3282096 6 5 
      2014820905820902 16:01:09 
      2014820905820902 16:47:12 
      0:46:03 
+0

嘿Barmar,有一個問題關於你提供的awk答案,wor ks完全好btw,但是有時在我的輸入有差異,額外的字段被添加,我想刪除運行相同的awk命令。我編輯了上面的輸入。你能幫忙嗎? – Zerg12

回答

3
curl "URL" | 
    awk -v OFS='|' '/FINISHED|INITIATED/ { 
     status = $1; getline; 
     jobname = $1; getline; 
     sequence = $2; date = $1; getline; 
     start = $2; getline; 
     if (status == "FINISHED") { end = $2; getline } else { end = "  " } 
     runtime = $1; 
     print jobname, sequence, date, start, end, runtime, status; 
    }' 

與輸入輸出爲:

RSYNCA|0036|20140502|17:31:08|  |0:17:16|INITIATED 
RSYNCA|0035|20140502|17:01:10|  |0:47:14|INITIATED 
RSYNCA|0034|20140502|16:31:03|17:24:45|0:53:42|FINISHED 
RSYNCA|0033|20140502|16:01:09|16:47:12|0:46:03|FINISHED 
+0

非常感謝Barmar。任何機會都可以在1行完成,所以我可以管它捲曲? – Zerg12

+0

換行符只是讓閱讀更容易,您可以將其刪除。但爲什麼這會阻止你將它捲曲成捲曲呢? – Barmar

+0

我正在管道此AWK,但在輸出中僅接收一行,排除了已完成狀態的作業: RSYNCJOBA | 0034 | 20140502 | 16:31:03 | | 0:16:22 | \t \t \t INITIATED – Zerg12

3

下面是使用GNU AWK的一種方法。的script.awk

curl "$URL" | awk -f script.awk 

內容:

BEGIN { 

    RS="FINISHED|INITIATED" 
    OFS="|" 
} 

s { 
    print (\ 
     $1, \ 
     $3, \ 
     $2, \ 
     $9, \ 
     (s == "FINISHED" ? $11 : "  "), \ 
     ($NF ~ /:/ ? $NF : $(NF-2)), \ 
     s \ 
    ) 
} 

{ 
    s = RT 
} 

結果:像運行

RSYNCA|0036|20140502|17:31:08|  |0:17:16|INITIATED 
RSYNCA|0035|20140502|17:01:10|  |0:47:14|INITIATED 
RSYNCA|0034|20140502|16:31:03|17:24:45|0:53:42|FINISHED 
RSYNCA|0033|20140502|16:01:09|16:47:12|0:46:03|FINISHED 

另外,這裏是一個班輪:

curl "$URL" | awk 'BEGIN { RS="FINISHED|INITIATED"; OFS="|" } s { print $1, $3, $2, $9, (s == "FINISHED" ? $11 : "  "), ($NF ~ /:/ ? $NF : $(NF-2)), s } { s = RT }' 
+0

我不知道爲什麼人們無法贊成這個。 –