简体   繁体   中英

Convert sed One-Liner to perl

I am using this sed one-liner in my perl script. However, is there a way (I am sure there is) to do the same job in 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 ':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. And hence, I ended up calling this sed command in my perl script.

I am not looking for a perl script, but rather a one-liner.

Thanks.

This perl one-liner should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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