Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1425  / 1 Year ago, sat, april 15, 2023, 10:41:09

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.


More From » bash

 Answers
3

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.


[#3237] Saturday, April 15, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
singerrupt

Total Points: 435
Total Questions: 111
Total Answers: 109

Location: Angola
Member since Tue, May 5, 2020
4 Years ago
singerrupt questions
;