1

I have a following list of files;

11F.fastq.gz
11R.fastq.gz
12F.fastq.gz
12R.fastq.gz

I'd like to rename these file names to the following;

11_S11_L001_R1_001.fastq.gz
11_S11_L001_R2_001.fastq.gz
12_S12_L001_R1_001.fastq.gz
12_S12_L001_R2_001.fastq.gz

I tried reanme as follows

rename 's/F.fastq/_S_L001_R1_001.fastq/' *.gz
rename 's/R.fastq/_S_L001_R2_001.fastq./' *.gz

but I dont quite know how to add file numbers (11 and 12 in this case) after "S". Any pointers will be appreciated.

akh22
  • 115

2 Answers2

2

zsh has its own builtin batch renaming tool: zmv available as an autoloadable function.

autoload -Uz zmv
typeset -A map=(F 1 R 2)
zmv -n '(<->)([FR])(.fastq.gz)' '${1}_S${1}_L001_R$map[$2]_001$3'

(remove the -n (dry-run) if happy).

Or using a scalar map=FR and use the index of the letter in that string for the replacement:

map=FR
zmv -n "(<->)([$map])(.fastq.gz)" '${1}_S${1}_L001_R$map[(Ie)$2]_001$3'
1

With Perl's rename commandline, you can capture the variable part ((...)) and use back references ($1):

rename -n 's/(\d+)F\.fastq/$1_S$1_L001_R1_001.fastq/' *F.fastq.gz
rename -n 's/(\d+)R\.fastq/$1_S$1_L001_R2_001.fastq/' *R.fastq.gz

Remove the -n if you're happy with the output.

pLumo
  • 22,565