2012-03-28 24 views
0

我需要從上面的行中複製文本匹配「DIV(」並將值放在行中小計是中。小計則不在報告中,我們需要補充的是在報告中還與值一起。需要將複製的文本從文件上面的行添加到它必須被放置的下一行

請幫我在這。如何寫一個shell腳本或用awk或sed的。

報告格式:

 
03/27/2012 - Emails Counts 

Test1 DIV(12345)     
Storenum Add Change 
----------- ---- ------ 
Store1  1  0      
      ---- ------ 
      1  0 
Test2 DIV(435335)     
Storenum Store Name  Add Change 
----------- --------------- ---- ------ 
Store2  Test Store2  2  1 
Store3  Test Store3  5  1 
Store4  Test Store4  0  1 
          ---- ------ 
           7  3 
          ---- ------          
Grand Total     8  3 

託比格式化爲

 
03/27/2012 - Emails Counts 

Test1 DIV(12345)     
Storenum     Add Change 
-----------     ---- ------ 
Store1      1  0      
          ---- ------ 
Test1 DIV(12345) Subtotal 1  0 
Test2 DIV(435335)     
Storenum Store Name  Add Change 
----------- --------------- ---- ------ 
Store2  Test Store2  2  1 
Store3  Test Store3  5  1 
Store4  Test Store4  0  1 
          ---- ------ 
Test2 DIV(435335) Subtotal 7  3 
          ---- ------          
Grand Total     8  3 
+2

你有什麼試過的?你可以聘請某人爲你做,但沒有你的任何意見,沒有人會幫你解決不存在的問題。 – Blender 2012-03-28 15:39:39

+0

如果您從數據庫構建報告(我希望如此),那麼您最好花時間學習如何編寫能夠生成所需輸出的查詢。編碼這樣的東西將是一個維護噩夢! ; - )!祝你好運。 – shellter 2012-03-28 17:59:22

回答

0
#!/usr/bin/perl -p 
next unless /DIV\(/; 
print; 
s/\s+$//; 
$div = $_." Subtotal"; 
$head = <>; 
$_ = <>; 
$pos = index $_, " ---- "; 
sub insbl { substr $_[0], $pos, 0, " "x(27-$pos); } 
insbl $head; print $head; 
insbl $_; print; 
do { insbl $_ = <>; print; } until /----/; 
$_ = $div.<>; 
substr $_, 27, $pos-27+length($div), ""; 
相關問題