1

I have a MainDirectory/hundreds_of_subdirectories/thousands_of_fastas structure. For each fasta (a bioinformatics file format) in the bottom level, I am trying to change the file name as well as the first line in the file to retain only some parts of the file.

So that the following text, which makes up both the file name and the first name of the file:

947-length-1150-cov-1000|contig:JAECWU010000213.1|slice:817050-818200|uce:uce-1452|match:817550-817700|orient:{'+'}|probes:4.unaligned.fasta

becomes just:

uce-1452.unaligned.fasta

For editing the first line in the file, I have tried

sed '1 s/^[^|uce-]*(|uce-[0-9]).*/\1/' hundreds_of_subdirectories/*

but this isn't the ticket..

Then messing around with some kind of loop...

for i in *\*.unaligned.fasta; do sed -E 's/^[^|uce-]*(|uce-[0-9]).*/\1/'; done

For the file name, it was suggested that I use rename instead of sed, but with sed I have tried something like:

for x in hundreds_of_subdirectories/thousands_of_fastas*unaligned.fasta; do
    echo $x | sed -r 's/^[^|uce-]*(|uce-[0-9]).*/mv' -v "\0" "|uce-[0-9].*/\1/.unaligned.fasta"/
done

Am I even barking up the right tree? Thanks!

3
  • 1
    If you want to rename files, then the tool that you want is rename. sed is for editing the contents of files or standard output. May 19 at 23:28
  • If there all named with the same layout as your example this will work cut -d':' -f 4 | sed 's/|match/.unaligned.fasta/' May 20 at 0:20
  • Hi @JasonCroyle, thanks! Can you elaborate a bit more? I tried this in a subdirectory, but it did not do anything... ` for i in **.unaligned.fasta; do cut -d':' -f 4 | sed 's/|match/.unaligned.fasta/ ; done `
    – Malia W
    May 21 at 2:31
2

Using find and the Perl rename utility, you could use

find MainDirectory -type f -name '*.unaligned.fasta' \
  -exec rename -v -d 's/.*:(uce-\d+).*/$1.unaligned.fasta/' {} +

This only changes the filename part of each path (option -d) and leaves the filename unchanged if the destination filename already exists. Remove option -v for non-verbose output.

1
  • Thank you, Freddy, I tried the following: 'find /Users/Malia/Downloads/exploded_fastas/chymomyza_mxostata_exploded_fastas -type f -name '.unaligned.fasta' \ -exec rename -v -d 's/.:(uce-\d+).*/$1.unaligned.fasta/' {} +' but it does not appear to make changes...anything glaringly wrong to your eyes?
    – Malia W
    May 21 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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