ファイル名セットからn番目の文字を削除する方法は?

ファイル名セットからn番目の文字を削除する方法は?

こんにちは、私はバッチファイルの名前を変更するためのbashソリューションを探しています。低速撮影映像に取り込むデジタルカメラのファイルがあります。

1000個の画像JPGファイルの名前が変更されるたびに、次のようにジャンプします。

./P1020998.JPG
./P1020999.JPG
./P1030001.JPG
./P1030002.JPG

After Effectsにインポートすると、画像の数が連続して表示されるように5番目の場所から「0」を削除できる再利用可能なシェルコマンドをハッキングしようとしています。

ファイル名セットからn番目の文字を削除する最も速く簡単な方法を知っている人はいますか?私はいつもそのディレクトリにある一連の画像に加えて、他のファイルがないディレクトリに画像をグループ化します。

検索してみたところ、これまでの回避策はファイル名から「0」文字をすべて削除するようでした。これは私が特に望むものではありません。ただ5番目の文字を削除することは私が望む結果です。時間をかけてこの質問に答えてくれてありがとう。

ベストアンサー1

使用している機能に関するマニュアルページを引用することをお勧めします。次のEXPANSIONセクションではman bash

${parameter:offset:length}
       Substring  Expansion.  Expands to up to length characters of the value of parameter starting at the character specified by offset.  If parameter is @, an 
       indexed array subscripted by @ or *, or an associative array name, the results differ as described below.  If length is omitted, expands to the substring
       of  the value of parameter starting at the character specified by offset and extending to the end of the value.  length and offset are arithmetic expres‐
       sions (see ARITHMETIC EVALUATION below).

       If offset evaluates to a number less than zero, the value is used as an offset in characters from the end of the value of parameter.  If length evaluates
       to  a  number less than zero, it is interpreted as an offset in characters from the end of the value of parameter rather than a number of characters, and
       the expansion is the characters between offset and that result.  Note that a negative offset must be separated from the colon by at least  one  space  to 
       avoid being confused with the :- expansion.

内部的に算術演算を実行できるため、${}次のようにコードをパラメータ化できます。

#!/bin/bash
position=5

for f in *JPG; do
   mv -- "$f" "${f:0:$position-1}${f:$position}"
done

exit

ここに迅速かつ汚れた解決策があります。ファイル名の長さやその他はチェックしませんが、このようなハッカーには問題ありません。

おすすめ記事