Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

perl and sustitution

I have file:

return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec); 
$ld_div_asig = round(($ld_assigned/4),2);
$ld_rest=round(($ld_assigned-$ld_total),3);
$ld_div_asig = round(($ld_assigned/12),4);
$ld_div_asig = round(($ld_assigned/4),3);
$ld_rest1=round(($ld_assigned-$ld_total),3);
$ldec_total_balance=round($ldec_total_debit-$ldec_total_credit,3);
$ldec_debit=round($this->ds_Prebalance->getValue("total_debit",$li_z),2);
$ldec_credit=round($this->ds_Prebalance->getValue("total_credit",$li_z),2);
$ld_total_debit=round($rs_data->fields["total_debit"],2);

I want to change all round numeric aproximation to 4, so my expect is:

return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec); 
$ld_div_asig = round(($ld_assigned/4),4);
$ld_rest=round(($ld_assigned-$ld_total),4);
$ld_div_asig = round(($ld_assigned/12),4);
$ld_div_asig = round(($ld_assigned/4),4);
$ld_rest1=round(($ld_assigned-$ld_total),4);
$ldec_total_balance=round($ldec_total_debit-$ldec_total_credit,4);
$ldec_debit=round($this->ds_Prebalance->getValue("total_debit",$li_z),4);
$ldec_credit=round($this->ds_Prebalance->getValue("total_credit",$li_z),4);
$ld_total_debit=round($rs_data->fields["total_debit"],4);

if I try perl -pe 's/(round\((.*?),)([0-9])/$14/g' file
it gives me:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec); 
$ld_div_asig = );
$ld_rest=);
$ld_div_asig = );
$ld_div_asig = );
$ld_rest1=);
$ldec_total_balance=);
$ldec_debit=);
$ldec_credit=);
$ld_total_debit=);

But if add space at the sustitution part, perl -pe 's/(round\((.*?),)([0-9])/$1 4/g' file
it gives me:

return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec); 
$ld_div_asig = round(($ld_assigned/4), 4);
$ld_rest=round(($ld_assigned-$ld_total), 4);
$ld_div_asig = round(($ld_assigned/12), 4);
$ld_div_asig = round(($ld_assigned/4), 4);
$ld_rest1=round(($ld_assigned-$ld_total), 4);
$ldec_total_balance=round($ldec_total_debit-$ldec_total_credit, 4);
$ldec_debit=round($this->ds_Prebalance->getValue("total_debit",$li_z), 4);
$ldec_credit=round($this->ds_Prebalance->getValue("total_credit",$li_z), 4);
$ld_total_debit=round($rs_data->fields["total_debit"], 4);

It’s what I want but I’d like to get it without the aditional space between the , and the 4.

Is there a way to do the sustitution in perl or sed without adding the aditional space?

>Solution :

You could also write your one-liner as perl -pe 's/(round\((.*?),)([0-9])/${1}4/g' file — that is, write ${1} instead of $1. As you wrote it, Perl reads $14 as referring to capture group 14.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading