他のファイルとの部分一致を使用してディレクトリの bash ファイルの名前を変更する

他のファイルとの部分一致を使用してディレクトリの bash ファイルの名前を変更する

data以下では、サブディレクトリ内の各IonCode_0000_ファイルペアの部分一致に基づいて、特定の実行で各サブフォルダの内容の名前を変更しようとしています。複数の実行がありますが、各実行は一意であり、名前変更の値はおよびにあります。以下のコードは私が考えていることを説明しています。ありがとうございます:)。data$1f1f1$uniqf1$2

set -xこれまでは大丈夫でしたが、Rename from:IonCode_0404 to:00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyyなぜこのようなことが起こるのか、どのように解決するのかわかりません。長い投稿についてお詫び申し上げます。この内容を把握できないので、すべての詳細を含めたいと思いました。ありがとうございます:)。

データ内容 ---名前を変更するファイルのペアです----

IonCode_0402_xxx_xxx_xxx.bam
IonCode_0402_xxx_xxx_xxx.bam.bai
IonCode_0404_xxx_xxx_xxx.bam
IonCode_0404_xxx_xxx_xxx.bam.bai

f1 ---部分的に一致するファイルの名前を---から変更します。

IonCode_0404 00-0000-xxx-xxx-xxx
IonCode_0402 11-1111-yy-yy-yyy
R_2019_00_00_00_00_00_xxxx_xx1-127-xxx_xxx_xxx_xxx_xx_xx_xx

IonCode_0402 22-2222-zz-zzzz-zzz
R_2019_00_00_00_00_00_xxxx_xx1-126-xxx_xxx_xxx_xxx_xx_xx_xx

名前変更後現在 --- ペアだけが次の2つの値に名前が変更されます。f1

IonCode_0402_xxx.xxx_xxx.bam
IonCode_0402_xxx.xxx_xxx.bam.bai
00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy_test.bam.bam
00-0000-xxx-xxx-xxx IonCode_0402 11-1111-yy-yy-yyy_test.bam.bam.bai

名前変更後に必要なデータ

11-1111-yy-yy-yyy_test.bam
11-1111-yy-yy-yyy_test.bam.bai
00-0000-xxx-xxx-xxx_test.bam
00-0000-xxx-xxx-xxx_test.bam.bai

強く打つ

set -x
dir=$1
for f in "$dir" ; do  ## operate on desired directory
  uniq=${dir##*/}  ## store run with no path as uniq
while read from to  ## start loop using from to
do
 (
   cd "$dir"/data  ## change directory to subfolder $from
   echo "Rename from:$from to:$to"  ## echo files
   for file in *.bam* 
   do
      newname=${file/$from*.bam/${to}_test.bam} ## rename adding text
      [ -f "$file" ] && [ "$newname" != "$file" ] && mv "$file" "$newname"  ## if names in uniq change file name
   done
 )
done <<<$(
   awk -F '\n' -v RS="" -v ref="$uniq" ' ## filter to this run in uniq
     $0 ~ ref {
         d=split($0, val); ## split on newline and space
         for(i=1;i<d;i++) print val[i];
      }' "$dir"/f1  
)  ## loop through f1 for unique run and populate from and to
done

ベストアンサー1

おすすめ記事