特定の文字列を置き換える方法は?

特定の文字列を置き換える方法は?

私のテキストファイルの例:

03/Oct/2016:06:39:50-0500,cd/base/0/48/2.png,206,1514
03/Oct/2016:06:39:50-0500,cd/base/1/46/3.png,206,5796
03/Oct/2016:06:39:50-0500,cd/base/2/45/4.png,206,2252
03/Oct/2016:06:39:50-0500,cd/base/3/46/78.png,200,7208
03/Oct/2016:06:39:50-0500,cd/base/4/45/43.png,206,2252
03/Oct/2016:06:39:50-0500,cd/base/5/46/8.png,200,7208
...

base/この記事では、次の規則に従って数字を変更する必要があります。

if that_number=0 then that_number=5
if that_number=1 then that_number=6
if that_number=2 then that_number=7
if that_number=3 then that_number=8
if that_number=4 then that_number=9
if that_number=5 then that_number=10

望ましい結果:

03/Oct/2016:06:39:50-0500,cd/base/5/48/2.png,206,1514
03/Oct/2016:06:39:50-0500,cd/base/6/46/3.png,206,5796
03/Oct/2016:06:39:50-0500,cd/base/7/45/4.png,206,2252
03/Oct/2016:06:39:50-0500,cd/base/8/46/78.png,200,7208
03/Oct/2016:06:39:50-0500,cd/base/9/45/43.png,206,2252
03/Oct/2016:06:39:50-0500,cd/base/10/46/8.png,200,7208

どうすればいいのかご存知ですか?

ベストアンサー1

Perlでは、コンテキストを一致させるのは簡単です。そして追加してください:

perl -pe 's,base/\K\d+,$& + 5,e' input_file

最初の部分(until)を忘れ、フォーマット内のすべての項目を一致させ、残りの部分を一致する項目()に5を加えたものに置き換えます。単純な文字列ではなく、Perl式で置き換えてください。base/<numbers>\K$&e

おすすめ記事