2014-03-29 47 views
0

我在我的perl腳本中使用此sed單線程。然而,有沒有一種方法(我確信有)在Perl中做同樣的工作?將sed單線轉換爲perl

這裏是我的文件:

$ cat all_log1.txt 
Looking into the table etl_f_gl_balance 
# of -1 values in etl_f_gl_balance.row_key: 
0 
# of -1 values in etl_f_gl_balance.ledger_key: 
1020 
# of -1 values in etl_f_gl_balance.gl_account_key: 
1020 
# of -1 values in etl_f_gl_balance.legal_entity_key: 
1020 
# of -1 values in etl_f_gl_balance.cost_center_key: 
995 
# of -1 values in etl_f_gl_balance.natural_account_key: 
1020 
# of -1 values in etl_f_gl_balance.posting_period_fiscal_key: 
5 
# of -1 values in etl_f_gl_balance.posting_period_key: 
5 

而且我想下面的輸出,這裏是我的sed解決方案:

$ sed ':a; N; $!ba; s/:\n/: /g' all_log1.txt 
Looking into the table etl_f_gl_balance 
# of -1 values in etl_f_gl_balance.row_key: 0 
# of -1 values in etl_f_gl_balance.ledger_key: 1020 
# of -1 values in etl_f_gl_balance.gl_account_key: 1020 
# of -1 values in etl_f_gl_balance.legal_entity_key: 1020 
# of -1 values in etl_f_gl_balance.cost_center_key: 995 
# of -1 values in etl_f_gl_balance.natural_account_key: 1020 
# of -1 values in etl_f_gl_balance.posting_period_fiscal_key: 5 
# of -1 values in etl_f_gl_balance.posting_period_key: 5 

目前,我沒有能夠得到它完成無論我使用哪種正則表達式組合,都使用perl。因此,我最終在我的perl腳本中調用了sed命令。

我不是在尋找一個Perl腳本,而是一個班輪。

謝謝。

+3

那麼你到目前爲止嘗試過什麼? 'perl -pe's /:\ n /:/''不起作用? – amon

+0

我不會發布輸出,但's2p':a; N; !$ BA; s \:\ n /:/ g''很有趣:['s2p' - sed to perl translator](http://man.cx/s2p) –

+0

FWIW,你原來的'sed'命令可以簡化爲'' 1! {N; s /:\ n /:/ g}''或''/ ^#/ {N; s /:\ n /:/ g}'' –

回答

4

這Perl的一個班輪應工作:

perl -pe 's/:\n$/: /' file 
Looking into the table etl_f_gl_balance 
# of -1 values in etl_f_gl_balance.row_key: 0 
# of -1 values in etl_f_gl_balance.ledger_key: 1020 
# of -1 values in etl_f_gl_balance.gl_account_key: 1020 
# of -1 values in etl_f_gl_balance.legal_entity_key: 1020 
# of -1 values in etl_f_gl_balance.cost_center_key: 995 
# of -1 values in etl_f_gl_balance.natural_account_key: 1020 
# of -1 values in etl_f_gl_balance.posting_period_fiscal_key: 5 
# of -1 values in etl_f_gl_balance.posting_period_key: 5 
+0

'e'開關是直接寫入命令行命令所需的命令? – aelor

+2

它在這裏用於相同的目的。用'-e'試試上面的命令,你就會知道。 – anubhava

+0

使用'-e'輸出什麼都沒有,文件也保持不變 – aelor

1

這一次將當前文件的備份和更新:

perl -i.bak -pe 's/\n$/ /g if /^#/gm' all_log1.txt 

如果你想在屏幕輸出,然後用這一個:

perl -pe 's/\n$/ /g if /^#/gm' all_log1.txt 
+0

但您的原始文件已更改。你可以打開並檢查 – aelor

+0

你可以使用'-pe'標誌來代替,如果你想在那裏打印結果本身 – aelor