Quantcast
Channel: Active questions tagged bash+awk - Ask Ubuntu
Viewing all articles
Browse latest Browse all 102

Rename multiple files with variable extensions to remove beginning and end of filename

$
0
0

I have multiple files I need to rename as below:

5891_1_0_AD3884_ACTCTCGA_S10.bam5891_1_0_AD3884_ACTCTCGA_S10.bam.bai5891_1_AD3875_GAGCTTGT_S1.bam5891_1_AD3875_GAGCTTGT_S1.bam.bai5891_2_AD3876_ACACGGTT_S2_R1.bam5891_2_AD3876_ACACGGTT_S2_R2.bam.bai

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

AD3884.bamAD3884.bam.baiAD3875.bamAD3875.bam.baiAD3876.bamAD3876.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.bam5891_1_0_ad3884_ACTCTCGA_S10.bam.bai5891_1_AD3875_GAGCTTGT_S1.bam5891_1_AD3875_GAGCTTGT_S1.bam.bai5891_2_AD3876_ACACGGTT_S2_R1.bam5891_2_AD3876_ACACGGTT_S2_R2.bam.bai

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

AD3884.bamAD3884.bam.baiAD3875.bamAD3875.bam.baiAD3876.bamAD3876.bam.bai

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


Viewing all articles
Browse latest Browse all 102

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>