Skip to main content
I add more informaton about the codes I have been working with and I emphasize the importance of working with sed.
Source Link
Emilio Galarraga
  • 1.3k
  • 1
  • 12
  • 20

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?

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?

add new information to questin, change "what I have after" by "what I have so far", fix the hyperlink to another post-
Source Link
Emilio Galarraga
  • 1.3k
  • 1
  • 12
  • 20

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:

What I have so afterfar consulting this

[https://stackoverflow.com/questions/68400709/bash-regex-replacing-the-second-field-in-a-csv-file-when-some-of-the-first-fi][post] post

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.

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:

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]

But it doesn't make the replacements in the file as I want

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:

What I have so far consulting this post

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.

Source Link
Emilio Galarraga
  • 1.3k
  • 1
  • 12
  • 20

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