2015-11-26 26 views
0

因此,我從sybase數據庫輸出中獲取此輸出。 我需要取出雙引號並向表中插入一些行定義。我客串Perl是做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 

獎金 - 這將是在劇本全部完成後擺脫'' - ''是很好的 - 從csv文件中刪除它們會更好,但我需要它們將頭部放入int中,所以我無法擺脫它們,直到我知道標題桌子在。

回答

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。如果沒有找到,-,它只會打印該行。