0

I have these files in a directory.

Mabel-A10_GTAGAGGA_L001_R1_001.fastq.gz
Mabel-A5_GGACTCCT_L001_R1_001.fastq.gz
Mabel-A11_GCTCATGA_L001_R1_001.fastq.gz
Mabel-A6_TAGGCATG_L001_R1_001.fastq.gz

I want the output to look like this.

Mabel-A10_L001_R1_001.fastq.gz
Mabel-A5_L001_R1_001.fastq.gz
Mabel-A11_L001_R1_001.fastq.gz
Mabel-A6_L001_R1_001.fastq.gz
1

2 Answers 2

0

You can try something like to rename them:

for i in *
do
o=$(echo $i|awk -F_ '{print $1,$3,$4,$5}')
mv "$i" "$o"
done
0

This script will output shell commands that perform a series of 'mv' commands to accomplish your task:

for FILE in *.fastq.gz
do
    L="${FILE%%_*}_"
    R="${FILE#${L}*_}";
    printf 'mv -vi "%s" "%s"\n' "$FILE" "$L$R"
done

Run that once, and examine the output. If there are lots of files, you may want to use less to review the commands.

If everything looks good, run the command again and pipe the output to bash.

It works by setting the variable L to everything left of the first underscore in the filename, plus the underscore itself. It then forms a string R by stripping the string in L from the front of the filename, and stripping further to the next underscore.

mv -vi "Mabel-A10_GTAGAGGA_L001_R1_001.fastq.gz" "Mabel-A10_L001_R1_001.fastq.gz"
mv -vi "Mabel-A11_GCTCATGA_L001_R1_001.fastq.gz" "Mabel-A11_L001_R1_001.fastq.gz"
mv -vi "Mabel-A5_GGACTCCT_L001_R1_001.fastq.gz" "Mabel-A5_L001_R1_001.fastq.gz"
mv -vi "Mabel-A6_TAGGCATG_L001_R1_001.fastq.gz" "Mabel-A6_L001_R1_001.fastq.gz"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.