我有一个这样的文件:
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>
逗号可以存在于文件的任何行中,而不仅仅是包含 number_format 的行。
使用此正则表达式: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
匹配:
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,",",".")
我想将第一个逗号后面的第一个数字更改为 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>
到目前为止我所咨询的内容邮政
我可以做这个
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,/'
它给了我这个输出:
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,",",".")
但它并没有按照我想要的方式在文件中进行替换。我知道这必须与捕获 sed 中的正则表达式组有关。
编辑这篇文章以添加有关我到目前为止所做的更多信息我的预期输出是:
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>
我可以使用 Romeo Ninov 提出的解决方案得到这个结果,但它仅在屏幕上有效,并且不保存更改。这就是为什么我想用 sed 来完成它,如果我想更改目录中的所有文件,则更多。
我将包含函数编号格式的表达式分为三个正则表达式
number_format\(\$([a-zA-Z0-9_]{1,}(\[\$[a-zA-Z0-9_]{1,}\+[a-zA-Z0-9]{1,}(\]))*,))
[0-9],我想要改变什么以及
(\,\"\,\"\,\"\.\"\))
如果我尝试这段代码:
sed -E 's/(number_format\(\$([a-zA-Z0-9_]{1,}(\[\$[a-zA-Z0-9_]{1,}\+[a-zA-Z0-9]{1,}(\]))*,))/\14/' file
它给了我:
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>
如果我使用最后一个正则表达式
sed -E 's/(\,\"\,\"\,\"\.\"\))/4\1/' file
给我
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>
但是,如果我结合第一个和 las regex 不起作用,它会返回原始文件而不进行任何更改
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>
那么,如何让它发挥作用呢?
答案1
您可以尝试使用awk
.类似的东西会搜索数字格式并更改第二个字段(以逗号作为分隔符)
awk -F\, 'BEGIN {OFS=","} /number_format/ { $2=4} {print $0}' input_file >output_file