1

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>

Commas could be exist in any line of the of the file, no only those containing number_format.

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 far consulting this 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. I know this have to be with capturing regex group in sed.

Edit this post to add morre infor mation about I have done so far My expected output is:

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>

I can gey this result with the solution proposed by Romeo Ninov, but it only works on the screen and don't save the changes. Tha's why I Want to do it with sed, more if I want to change all file in a directory.

I divided the expresion containg the function number format in three regular expresion

number_format\(\$([a-zA-Z0-9_]{1,}(\[\$[a-zA-Z0-9_]{1,}\+[a-zA-Z0-9]{1,}(\]))*,))

[0-9], what I want to change and

(\,\"\,\"\,\"\.\"\))

if I try this code:

sed -E 's/(number_format\(\$([a-zA-Z0-9_]{1,}(\[\$[a-zA-Z0-9_]{1,}\+[a-zA-Z0-9]{1,}(\]))*,))/\14/' file

it gives me:

one line
echo number_format($row1[$z+1],42,",",".");
echo "<b><font color=red>".number_format($mount_01,42,",",".")."</font></b>";
echo "<b><font color=red>".number_format($mount_02,42,",",".")."</font></b>";
other line
<td bgcolor="red"><b><font color="white"><?echo number_format($general,42,",",".");?></font></b></td>
another line
<td><font size=4><b><?echo number_format($sum_total,42,",",".");?></font></b></td>

and if I use the last regex

sed -E 's/(\,\"\,\"\,\"\.\"\))/4\1/' file

gives me

one line
echo number_format($row1[$z+1],24,",",".");
echo "<b><font color=red>".number_format($mount_01,24,",",".")."</font></b>";
echo "<b><font color=red>".number_format($mount_02,24,",",".")."</font></b>";
other line
<td bgcolor="red"><b><font color="white"><?echo number_format($general,24,",",".");?></font></b></td>
another line
<td><font size=4><b><?echo number_format($sum_total,24,",",".");?></font></b></td>

But if I combine the first and las regex doesn't work it returs the original file without changes

sed -E 's/(number_format\(\$([a-zA-Z0-9_]{1,}(\[\$[a-zA-Z0-9_]{1,}\+[a-zA-Z0-9]{1,}(\]))*,))(\,\"\,\"\,\"\.\"\))/\14\2/' file

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>

So, how to make it work?

1
  • It's not so easy. There could be commas in other lines and even in the same line. Feb 9, 2023 at 22:13

1 Answer 1

0

You can try with awk. Something like will search for number_format and change the second field (with comma as delimiter)

awk -F\, 'BEGIN {OFS=","} /number_format/ { $2=4} {print $0}' input_file >output_file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .