使用 bash 删除文件扩展名

使用 bash 删除文件扩展名

我尝试删除文件扩展名,./m161016_023529_42229_c101105162550000001823259804261747_s1_p0.2.bax.h5但得到了./m161016_023529_42229_c101105162550000001823259804261747_s1_p0.2. .2 有时是 .1 或 .3。

这是 bash 代码:

#!/bin/bash

for i in $(ls -d /work/waterhouse_team/All_RawData/Each_Cell_Raw/RSII_SMRT*/Analysis_Results);
do
  cd $i
  baxs=($(find . -type f -name "*.bax.h5"))

  cat <<EOF
  #qsub <<EOF
#!/bin/bash -l

#PBS -N bax2bam
#PBS -l walltime=150:00:00
#PBS -j oe
#PBS -l mem=50G
#PBS -l ncpus=4
#PBS -M [email protected]
##PBS -m bea

source /work/waterhouse_team/apps/pacbio/setup-env.sh
cd $i;

bax2bam ${baxs[@]} -o ${baxs[0]%%.bax.h5} --subread

EOF

done

这是 bash 输出:

  #qsub <<EOF
#!/bin/bash -l

#PBS -N bax2bam
#PBS -l walltime=150:00:00
#PBS -j oe
#PBS -l mem=50G
#PBS -l ncpus=4
#PBS -M [email protected]
##PBS -m bea

source /work/waterhouse_team/apps/pacbio/setup-env.sh
cd /work/waterhouse_team/All_RawData/Each_Cell_Raw/RSII_SMRT72/Analysis_Results;

bax2bam ./m161014_135413_42229_c101104702550000001823259804261737_s1_p0.1.bax.h5 ./m161014_135413_42229_c101104702550000001823259804261737_s1_p0.2.bax.h5 ./m161014_135413_42229_c101104702550000001823259804261737_s1_p0.3.bax.h5 -o ./m161014_135413_42229_c101104702550000001823259804261737_s1_p0.1 --subread

  #qsub <<EOF
#!/bin/bash -l

#PBS -N bax2bam
#PBS -l walltime=150:00:00
#PBS -j oe
#PBS -l mem=50G
#PBS -l ncpus=4
#PBS -M [email protected]
##PBS -m bea

source /work/waterhouse_team/apps/pacbio/setup-env.sh
cd /work/waterhouse_team/All_RawData/Each_Cell_Raw/RSII_SMRT73/Analysis_Results;

bax2bam ./m161016_023529_42229_c101105162550000001823259804261747_s1_p0.2.bax.h5 ./m161016_023529_42229_c101105162550000001823259804261747_s1_p0.3.bax.h5 ./m161016_023529_42229_c101105162550000001823259804261747_s1_p0.1.bax.h5 -o ./m161016_023529_42229_c101105162550000001823259804261747_s1_p0.2 --subread

我错过了什么?

先感谢您

答案1

您的字符串操作命令仅删除该.bax.h5部分。要删除额外的前导点和数字,请使用类似${baxs[0]%%.[0-9].bax.h5}.

答案2

在我看来,您想删除由三个点组成的扩展名,例如.2.bax.h5.如果这是真的,您可以为此使用参数扩展。另外,您不应该像处理文件一样使用ls和处理文件。find

#!/bin/bash

shopt -s globstar nullglob

for f in **/*.bax.h5; do
   mv -- "$f" "${f%.*.*.*}"
done

exit

*.bax.h5这将从工作目录开始递归地迭代匹配的文件。然后,它从字符串末尾开始删除扩展名,并删除所有内容,直到.总共看到 3 次。

相关内容