1

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?

2 Answers 2

3

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 
3
  • NB I assume the two strings after s/ are the ones you want to remove Commented May 12, 2015 at 21:47
  • 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. Commented May 12, 2015 at 21:54
  • @LuciaO if this is what works for you please remember to accept the answer with the tick mark. Commented May 12, 2015 at 22:26
0

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.