1

there are 30 files named like this:

sample_kmer_41_2.lib1.bowtie.file1.27.faabyss_sample

I need the output to be:

abyss_sample_kmer_41_2.lib1.bowtie.file1.27.fa

I don't know how to do this, I've already screwed the names up a lot. thanks for any help.

1225
  • 105
  • 7

3 Answers3

4

Use the rename utility:

rename 's/(.*)\.fa(.*)_sample$/$2_$1/' *

rename is part of the perl package and is installed by default on debian-like systems. There is a different and incompatible rename utility that is provided as part of the util-linux packages.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • This is also a better alternative over the Bash script that I provided. – David Hoelzer Jun 01 '15 at 20:43
  • thanks for the answer, but I believe I have the incompatible rename utility and not the perl package - I tried rename before asking this question and it didn't work. – 1225 Jun 01 '15 at 20:49
2

If you have the Perl-based prename (possibly named rename) command, then:

prename 's/(.*)abyss_sample$/abyss_$1/' sample_kmer_*

The exact regex to use depends on how the names have been damaged. This should work for the example name given; it may need tweaking to work with other names.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

You could use a for loop. Since I'm always nervous about this stuff I'd recommend doing an echo first to make sure everything looks groovy...

#!/bin/bash
for FILE in sample_kmer_41_2*abyss_sample ; do
  NEWNAME=`echo $FILE | sed -e 's/abyss_sample//'`
  echo mv $FILE $NEWNAME
done

Provided that doesn't need tweaking, you can remove the echo or make modifications as necessary.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
  • 1
    Sure. The first one takes the current filename in the loop and sends it through sed to strip off the end of the file name. The second one simply prints `mv $FILE $NEWNAME` at the console. Echo will "echo" back at you whatever you give it to standard output. – David Hoelzer Jun 01 '15 at 20:46
  • @David Hoelzer It won't run unless a semicolon is put before `do` and the equal sign is stripped of spaces from both sides. – Raul Laasner Jun 01 '15 at 20:54
  • @RaulLaasner Oops! Thanks! – David Hoelzer Jun 01 '15 at 21:38