Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have the following list of files:

Dorn_Triatomine_A5201_sequence_1_unmappedforTdim_tdim.alleles.tsv
Dorn_Triatomine_A5201_sequence_1_unmappedforTdim_tdim.matches.tsv
Dorn_Triatomine_A5201_sequence_1_unmappedforTdim_tdim.snps.tsv
Dorn_Triatomine_A5201_sequence_1_unmappedforTdim_tdim.tags.tsv
Dorn_Triatomine_T9252_sequence_1_unmappedforTdim_tdim.alleles.tsv
Dorn_Triatomine_T9252_sequence_1_unmappedforTdim_tdim.matches.tsv
Dorn_Triatomine_T9252_sequence_1_unmappedforTdim_tdim.snps.tsv
Dorn_Triatomine_T9252_sequence_1_unmappedforTdim_tdim.tags.tsv

I would like to eliminate some of the repetitive string and rename the file as follow:

A5201_tdim.alleles.tsv
A5201_tdim.matches.tsv
A5201_tdim.snps.tsv

I tried using:

mv Dorn_Triatomine_*_sequence_1_unmappedforTdim_tdim.tags.tsv *_tdim.tags.tsv 

What would be the simplest way to achieve this task?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

This script ought to do it:

#!/bin/sh
for f in Dorn_Triatom* ; do
  mv "$f" `echo "$f" | sed -e 's/Dorn_Triatomine_//' -e 's/sequence_1_unmappedforTdim_//'`
done
share|improve this answer
    
NB I assume the two strings after s/ are the ones you want to remove –  Bjorn Munch May 12 at 21:47
    
Worked perfect. Thanks. –  Lucia O May 12 at 21:51
1  
Please note that it would give an error if there is no file starts with Dorn_Triatom and placing $f inside of double quotes would be safer in case of spaces in file names. –  Esref May 12 at 21:54
    
@LuciaO if this is what works for you please remember to accept the answer with the tick mark. –  roaima May 12 at 22:26

Using grep with PCRE to get the new file name:

#!/bin/bash
for file in *.tsv; do
  newname="$(grep -Po '^[^_]*_[^_]*_\K[^_]*_'<<<"$file")$(grep -Po '.*_\K.*$' <<<"$file")"
  mv "$file" "$newname"
done 

newname is combined of output of two grep operations:

  • grep -Po '^[^_]*_[^_]*_\K[^_]*_'<<<"$file" will output the A5201_ like strings from each filename

  • $(grep -Po '.*_\K.*$' <<<"$file") will match the last portion of the filename, after last _

  • $() is the bash command substitution pattern.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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