Issue
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:
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.
Answered By - user20284150 Answer Checked By - Mary Flores (WPSolving Volunteer)