regular expression – multiple replacement in file using sed and regex
I have a file like this:
one line
echo number_format($row1[$z+1],2,",",".");
echo "<b><font color=red>".number_format($mount_01,2,",",".")."</font></b>";
echo "<b><font color=red>".number_format($mount_02,2,",",".")."</font></b>";
other line
<td bgcolor="red"><b><font color="white"><?echo number_format($general,2,",",".");?></font></b></td>
another line
<td><font size=4><b><?echo number_format($sum_total,2,",",".");?></font></b></td>
with this regex: grep -Eo 'number_format\(\$([a-zA-Z0-9_]|([a-zA-Z0-9_]{1,}|)[a-zA-Z0-9_\\]{1,}\[\$[a-zA-Z0-9](\+[a-zA-Z0-9])*{1,}(\]))*,[0-9],",",".")' file
matches:
number_format($row1[$z+1],2,",",".")
number_format($mount_01,3,",",".")
number_format($mount_02,2,",",".")
number_format($general,3,",",".")
number_format($sum_total,4,",",".")
I want to change the first number after the first comma to 4
one line
echo number_format($row1[$z+1],4,",",".");
echo "<b><font color=red>".number_format($mount_01,4,",",".")."</font></b>";
echo "<b><font color=red>".number_format($mount_02,4,",",".")."</font></b>";
other line
<td bgcolor="red"><b><font color="white"><?echo number_format($general,4,",",".");?></font></b></td>
another line
<td><font size=4><b><?echo number_format($sum_total,4,",",".");?></font></b></td>
What I have so after consulting this
[https://stackoverflow.com/questions/68400709/bash-regex-replacing-the-second-field-in-a-csv-file-when-some-of-the-first-fi][post]
I can do this
grep -Eo 'number_format\(\$([a-zA-Z0-9_]|([a-zA-Z0-9_]{1,}|)[a-zA-Z0-9_\\]{1,}\[\$[a-zA-Z0-9](\+[a-zA-Z0-9])*{1,}(\]))*,[0-9],",",".")' file |sed -E '1,$ s/^("[^"]*"|[^",]*)(, *)[0-9]*,/\1\24,/'
It gives me this output:
number_format($row1[$z+1],4,",",".")
number_format($mount_01,4,",",".")
number_format($mount_02,4,",",".")
number_format($general,4,",",".")
number_format($sum_total,4,",",".")
But it doesn’t make the replacements in the file as I want
Read more here: Source link