2012-07-12 178 views
-1

標籤SED:用逗號使用AWK分隔或低於所用逗號使用AWK分隔或標籤SED以下

[BEGIN AccountID] 
     [BEGIN CallerID] 
      [BEGIN Billed Account Attributes] 
      1111111 
      1111111 
      1111111 
      [END Billed Account Attributes] 

     [BEGIN OBIO Tax] 
     10 
     20 
     30 
     [END OBIO Tax] 

     [BEGIN RINO Tax] 
     777 
     888 
     999 
     [END RINO Tax] 
    [BEGIN CallerID] 
[END AccountID] 


[BEGIN AccountID] 
    [BEGIN CallerID] 
     [BEGIN Billed Account Attributes] 
     2222222 
     2222222 
     2222222 
     [END Billed Account Attributes] 

     [BEGIN OBIO Tax] 
     40 
     50 
     60 
     [END OBIO Tax] 

    [BEGIN CallerID] 
[END AccountID] 

我想AWK或sed腳本來打印:

1111111,1111111,1111111, 10,20,30, 777,888,999 

2222222,2222222,2222222, 40,50,60, 0, 0, 0 
.... 
.... 
.... 

用逗號分隔,並且在沒有顯示RINO TAX時放入ZERO。

非常感謝!

+3

到目前爲止您嘗試過什麼?你能和我們分享一些代碼或想法嗎?如果我們知道你已經探索過什麼,那麼幫助就會更容易。 – Levon 2012-07-12 21:22:29

+1

這是功課嗎? – 2012-07-13 13:46:56

+0

這個我試過了: awk'BEGIN {OFS =「;」; }/^ \ [BEGIN AccountID \]/{f [nf = 0] = $ 0; while(getline && $ 0!〜/^\ [END AccountID \] /){f [++ nf] = $ 0;} print f [3],f [15];}'$ 1 – Oliveira 2012-07-13 15:54:41

回答

1

這可能會爲你工作(GNU SED):

sed '/\[BEGIN AccountID\]/,/\[END AccountID\]/!d;/\[BEGIN AccountID\]/{h;d};/./H;/\[END AccountID\]/!d;g;s/\n*\[[^\n]*\n*//g;s/\n/,/g;s/\s*//g;ta;:a;s/,//9;t;s/$/0,0,0/' file 

注:這將刪除空格和空行。

+0

太近了!有可能把每個寄存器放在一個新的行中? 非常感謝波通! – Oliveira 2012-07-13 15:57:38