sedとregexを使用してファイル内で複数の置換を実行する

sedとregexを使用してファイル内で複数の置換を実行する

次のファイルがあります。

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を使用したいのですが、ディレクトリ内のすべてのファイルを変更したい場合はさらにそうです。

関数番号形式を含む式を3つの正規表現に分割しました。

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同様のものを試すことができます。数値形式2番目のフィールドを変更します(カンマを区切り文字として使用)。

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

おすすめ記事