简体   繁体   English

将sed One-Liner转换为Perl

[英]Convert sed One-Liner to perl

I am using this sed one-liner in my perl script. 我在我的perl脚本中使用了这个sed单行代码。 However, is there a way (I am sure there is) to do the same job in perl? 但是,有没有办法(我确定有)在perl中做同样的工作?

Here is my file: 这是我的文件:

$ 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

And I want the output as below and here is my sed solution: 我想要的输出如下,这是我的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

At the moment, I am not able to get it done in perl no matter what combination of regex I use. 目前,无论我使用哪种正则表达式组合,我都无法在perl中完成它。 And hence, I ended up calling this sed command in my perl script. 因此,我最终在我的perl脚本中调用了这个sed命令。

I am not looking for a perl script, but rather a one-liner. 我不是在寻找perl脚本,而是在寻找一种语言。

Thanks. 谢谢。

This perl one-liner should work: 这种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

This one will take backup of current file and update it: 这将备份当前文件并更新它:

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

If you want to have output in screen, then use this one: 如果要在屏幕上显示输出,请使用以下命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM