0

I tried to remove the file extension from ./m161016_023529_42229_c101105162550000001823259804261747_s1_p0.2.bax.h5 but I got ./m161016_023529_42229_c101105162550000001823259804261747_s1_p0.2. The .2 is sometimes .1 or .3.

This is the bash code:

#!/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 m.lorenc@qut.edu.au
##PBS -m bea

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

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

EOF

done

This is the bash output:

  #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 m.lorenc@qut.edu.au
##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 m.lorenc@qut.edu.au
##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

What did I miss?

Thank you in advance

  • It appears you want to remove the first dot and everything after. Is that true? – glenn jackman Dec 12 '17 at 0:13
2

Your string manipulation command is only removing the .bax.h5 part. To also remove an extra leading dot and digit, use something like ${baxs[0]%%.[0-9].bax.h5}.

  • 2
    It's possible all that's needed is ${...%%.*} – glenn jackman Dec 12 '17 at 0:13
  • 2
    @glennjackman it looks like he might be getting relative path names that have a leading ./, so that form trims too much. He's working in a path that has a wildcard in it, so there's no telling how many additional .s might be in the complete file path string. – bta Dec 12 '17 at 1:28
  • A cleaner solution is to use ${...%%.*} and enforce absolute paths in find by using find $(pwd) ...". – Travis Clarke Dec 12 '17 at 1:36
  • 1
    Using absolute paths won't save you from paths with a . in the file name (i.e. /etc/init.d/subfolder/some_file.2.bax.h5), which can certainly be the case when there's a wildcard in the search path. The safest option is to surgically extract the filename and avoid any assumptions about the rest of the string, even if it is noticeably uglier. – bta Dec 12 '17 at 1:44
  • @bta - I see what you mean. Assuming the extension is known to follow that pattern, then I agree with you. Might need more clarity from the OP to be certain. – Travis Clarke Dec 12 '17 at 2:01
0

It looks to me like you want to remove an extension consisting of three dots, e.g. .2.bax.h5. If that's true you can use a parameter expansion for this. Also, you shouldn't be using ls and find like you are to process the files.

#!/bin/bash

shopt -s globstar nullglob

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

exit

This will recursively iterate over files matching *.bax.h5, starting with the working directory. Then it removes the extension by starting at the end of the string and removes everything until it sees a ., three times in total.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.