需要将文件中上一行的复制文本添加到必须放置的下一行

时间:2012-03-28 15:39:09

标签: unix

我需要复制上面一行中与“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 

Tobeformated to

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 

1 个答案:

答案 0 :(得分: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), "";