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
Romeo Ninov
  • 17,484
  • You may also wish to look in apple.stackexchange.com - but please only post here or there. The answer to your question is in https://unix.stackexchange.com/questions/45212/remove-prefixes-from-filenames – K7AAY May 29 '19 at 16:05

2 Answers2

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
Romeo Ninov
  • 17,484
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"
Jim L.
  • 7,997
  • 1
  • 13
  • 27