I have multiple files I need to rename as below:

5891_1_0_AD3884_ACTCTCGA_S10.bam 5891_1_0_AD3884_ACTCTCGA_S10.bam.bai 5891_1_AD3875_GAGCTTGT_S1.bam 5891_1_AD3875_GAGCTTGT_S1.bam.bai 5891_2_AD3876_ACACGGTT_S2_R1.bam 5891_2_AD3876_ACACGGTT_S2_R2.bam.bai 

I would like to remove everything except AD**** so that the resulting filenames would be:

AD3884.bam AD3884.bam.bai AD3875.bam AD3875.bam.bai AD3876.bam AD3876.bam.bai 

The number of underscores ("_") before and after the AD**** is not always consistent. Currently there are always 4 digits following the AD, but in the future it may increase to 5.

Ideally a bash solution from parameter expansion would be great (working in a Linux Ubuntu environment), though I have tried this without success. sed, awk, grep, or lastly rename solutions are also possible, though I have tried many versions of these without success. Any help would be much appreciated.

UPDATE

The rename solution from @steeldriver works perfectly.

I was able to make the solution from @Boba Fit work with the following modifications:

for file in * do fn=AD"${file#*AD}" ext=${file#*.} mv $file rename/${fn%%_*}.${ext} done 

New edit

One additional issue is that the AD may also be lowercase (ad). For example:

5891_1_0_ad3884_ACTCTCGA_S10.bam 5891_1_0_ad3884_ACTCTCGA_S10.bam.bai 5891_1_AD3875_GAGCTTGT_S1.bam 5891_1_AD3875_GAGCTTGT_S1.bam.bai 5891_2_AD3876_ACACGGTT_S2_R1.bam 5891_2_AD3876_ACACGGTT_S2_R2.bam.bai 

For the final result its ok if they are all capitalized (AD):

AD3884.bam AD3884.bam.bai AD3875.bam AD3875.bam.bai AD3876.bam AD3876.bam.bai 

But I need to be able to recognize upper and lowercase (case-insensitive) in the input.

2 Answers

I've written a bash script for you

#!/bin/bash for file in * do name=$(echo "$file" | tr '[:lower:]' '[:upper:]') name=AD${name#*_AD} name=${name::6} extension=${file#*.} mv ${file} ${name}.${extension} done 

With # I remove all the (smallest possible) part in front of the string that matchs *_AD. In the next line I cut the Sting in max length to 6. I write the extension by remogin the smales part that matches *..

Then we move the file to ${name}.${extension}.

Hope it works well.

7

With perl-based rename:

$ rename -n 's/.*(AD\d+).*?[.](.*)/$1.$2/' *.ba[im] rename(5891_1_0_AD3884_ACTCTCGA_S10.bam, AD3884.bam) rename(5891_1_0_AD3884_ACTCTCGA_S10.bam.bai, AD3884.bam.bai) rename(5891_1_AD3875_GAGCTTGT_S1.bam, AD3875.bam) rename(5891_1_AD3875_GAGCTTGT_S1.bam.bai, AD3875.bam.bai) rename(5891_2_AD3876_ACACGGTT_S2_R1.bam, AD3876.bam) rename(5891_2_AD3876_ACACGGTT_S2_R2.bam.bai, AD3876.bam.bai) 

Remove the -n once you are happy that it is doing the right thing.

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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