
私のテキストファイルの例:
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
形式の任意のものに一致し、最初の部分 ( まで) を無視して、一致した部分 ( )に残りを置き換えます。さらに 5 を加えると、置換は単なる文字列ではなく Perl 式になります。base/<numbers>
\K
$&
e
答え2
awk
+5
各数値に対する演算であるという事実を利用できます。
awk -F'/' '{$5+=5 ; print}' OFS='/' input_file
入力 ( ) と出力 ( )/
のフィールド区切り文字としてそれぞれ使用する場合、フィールド番号 5 に 5 を追加する必要があります。-F'/'
OFS='/')
ここではスラッシュの位置と数が重要であることに注意してください。
答え3
sed
動作するでしょう:
sed \
-e 's/base\/1/base\/6/' \
-e 's/base\/2/base\/7/' \
-e 's/base\/3/base\/8/' \
-e 's/base\/4/base\/9/' \
-e 's/base\/5/base\/10/' \
-e 's/base\/0/base\/5/'
「base/0」のケースを最後に置く必要があると思います。そうしないと、5 -> 10 のケースも有効になります。