Removing Middle of Filename

2019-07-31 13:38发布

问题:

I wish to remove the middle of filenames that look like the following:

Wm-no-Bis.GAGATTCC-ATAGAGGC.L001.R1.001.fastq.trim.R2.fastq
Pek-no-Bis.GAGATTCC-AGGCGAAG.L001.R1.001.fastq.trim.R1.fastq

To Get

Wm-no-Bis.trim.R2.fastq
Pek-no-Bis.trim.R1.fastq

As you can see the beginning and end of the files are slightly variable. I have attempted to use rename as follows:

rename 's/.*.*.*.*.*.trim.//g'
Wm-no-Bis.GAGATTCC-ATAGAGGC.L001.R1.001.fastq.trim.R2copy.fastq

This removes the entire file name except for the end. The issue is obviously my lack of understanding how to use wildcards.

回答1:

You can use this rename command:

rename -n 's/^([^.]+).*(\.trim\..*)$/$1$2/' *.fastq

When you're happy with output then remove -n option (dry-run)



回答2:

try

rename 's/\.\w+-\w+\..{4}\.R.\.\d{3}\.fastq//g'

source