perl匹配正则表达式后,perl将字符串插入文件中

时间:2015-11-26 08:01:50

标签: perl

所以我从sybase数据库输出中获取此输出。 我需要取出双引号并在表中插入一些行定义。我猜perl是做它的工具

 "NO Comm","-"
"INVESTMENT","29541","3860237","0","ANEW","(null)","BP    ","frantzla","A"
"DERIVS","110000","2875587","0","ANEW","0","BP    ","frantzla","A"
"DERIVS","4089","1185810","0","IBKD","0","BP    ","unsalr  ","A"
"COMMERCE","705","41185","0","ANEW","(null)","BP    ","madiganj","A"
"FIRST","504","236","0","ANEW","0","CPS   ","kilkenki","A"
" ","-"
"Opera Pending","-"
"FIRST","827921","23565575","20699","ANEW","2.5","CPS   ","kilkenki","A"
"INC","218780","3596112","5472","ANEW","2.5","CPS   ","madiganj","A"
" ","-"
"ETC Pending","-"
"BANK , PITTSBURGH","251508","10803056","1258","IBKD","0.5","CPS   ","dennyad ","A"
"SYDNEY","4500","178173","45","IBKD","1","CPS   ","dennyad ","A"
" ","-"
"Confirmed Booking","-"
"MOTHER FUND","22400","1187584","1781","IBKD","15","BP    ","crouzejo","A"
"ASSET MANAGEMENT, SGIIC, S.","45010","614834","922","IBKD","15","BP    ","ruizdemi","A"

一旦我采用"双引号"我想出了一些表格,这些表格最终都以", - "就在他们之后。 我需要在talbe名称后面添加一个标题来确定行。所以我所要做的就是永恒的时间 perl脚本命中" \ w + \, - "它需要做的就是将这一行插入下一行。

Client , Shares , Notional, TotalComm, Status, Comm, CommType, Owner, TradingBasis

它是下面值的标题。     它打印出标题 - 但不是整个报告。     这是一个CSV文件,我需要将这些标题放在表格上。

#!/usr/bin/perl
use strict;
use warnings;
open my $fhIn,  '<', 'today.txt'          or die $!;
my $add_trip = "Client , Shares , Notional, TotalComm, Status, Comm, CommType, Owner, TradingBasis" ;
while (<$fhIn>) {
   my $print_flag = 0 ;
   tr/\"//d;
   if(/\w+\,-/) {
       $print_flag = 1 ;
           if($print_flag) {
           print "$add_trip\n" ;
       } else { print $_ ;  }
   }
}

结果 - 不打印表格只打印我要插入的行:

Client , Shares , Notional, TotalComm, Status, Comm, CommType, Owner, TradingBasis
Client , Shares , Notional, TotalComm, Status, Comm, CommType, Owner, TradingBasis
Client , Shares , Notional, TotalComm, Status, Comm, CommType, Owner, TradingBasis
Client , Shares , Notional, TotalComm, Status, Comm, CommType, Owner, TradingBasis

我需要更像这样的东西:

ETC Pending,-
Client , Shares , Notional, TotalComm, Status, Comm, CommType, Owner, TradingBasis
BANK , PITTSBURGH,251508,10803056,1258,IBKD,0.5,CPS   ,dennyad ,A
SYDNEY,4500,178173,45,IBKD,1,CPS   ,dennyad ,A
 ,-
Confirmed Booking,-
Client , Shares , Notional, TotalComm, Status, Comm, CommType, Owner, TradingBasis
MOTHER FUND,22400,1187584,1781,IBKD,15,BP    ,crouzejo,A
ASSET MANAGEMENT, SGIIC, S.,45010,614834,922,IBKD,15,BP    ,ruizdemi,A

奖金 - 摆脱&#39;,&#39;会很棒。在脚本完成之后 - 从csv文件中删除它们会很好,但是我需要它们把它放在头文件中,所以在我知道表格的标题所在之前我无法摆脱它们。

1 个答案:

答案 0 :(得分:1)

首先要做的两件事:

  • 由于这是一个CSV文件,因此最安全的方法是使用CSV解析模块,例如Text::CSV
  • 由于您要将,-与正则表达式匹配,因此您应该确保没有任何字段恰好包含该字符串

话虽如此,试着替换它:

if(/\w+\,-/) {
    $print_flag = 1 ;
        if($print_flag) {
        print "$add_trip\n" ;
    } else { print $_ ;  }
}

用这个:

if(/\w+\,-/) {
    print "$add_trip\n";
} else {
    print;
}

如果我们按照您的方式重新格式化,则更容易看到问题:

if(/\w+\,-/) {
    $print_flag = 1 ;
    if($print_flag) {
        print "$add_trip\n";
    } else {
        print $_;
    }
}

如果/\w+\,-/不匹配,则不会发生任何事情,因为print $_语句在else语句中的某个级别太深。此外,除非您在代码中的其他位置使用它,否则无需设置$print_flag = 1。即使您这样做,也不需要if($print_flag),因为您只需在上一行中设置$print_flag = 1

<强>替代:

我并不完全清楚你想要的输出是什么,但如果上面的不是它,那么可能是:

if(/\w+\,-/) {
    s/,-//;
    print "$_$add_trip\n";
} else {
    print;
}

这将检查,-,如果发现它将删除它,请打印该行,然后打印$add_trip。如果找不到,-,则只打印该行。