How do I rename *.faa filenames for an expression located inside each file in [brackets]?

I would like to rename the filenames *.faa (a typical name is protein-176.faa) for a name that is located inside each .faa file in brackets [Escherichia coli DSM123].

For example the file protein-44.faa includes the first lines:

>ABI60899.1 Multidrug resistance protein B [Granulibacter bethesdensis CGDNIH1]
MVRQGRPSQSREQEDISEPGTQGRGWMQARHVHHWRFHHRRAVHKDLMTQPRSSRPVSSRILALIVSAALFMELMDGTIL
ATALPQMAQSFDVAPLQMSVALTAYLLSLAVFIPASGWMADRFGSRRIFMGAIALFVTGSMVCGMANALPEMVIARLVQG
AGGAMMVPVGRLLLLRNVPRHELVSAIAWMTIPATLGPVLGPPVGGFLTTWLSWRWIFYINLPIGLIGMGLAARFVPNVT
EAELRPLDVKGLLLSGTALASLLWAMETLGRGPSGTDGMALSSAAILTLIGLGSGWLYLRHSRTIPHPILNPMLMRIRTF
RLSVLGGACSRVVAGAMPFLLPMTMQLGMGMSAAESGSLTFVGAAGSLLIRPWAAGILRRFGFRRVMIWNGALSSTAVLL
CATFQPSWPHGWFFLVLAPAGLFQALQFIAYNTIAYADVPRERMSEATSFYTTFQQMTLSAGICIAGISVSLSMLAGPRT
QPDMTDFATGFVTIATISALAILCASRLNSTDGQDLSRKA
>ABI60900.1 Uroporphyrinogen decarboxylase [Granulibacter bethesdensis CGDNIH1]
MNKPILRVLRGEALPVPPVWLMRQAGRYLPEYREVRAKAGSFLGLATHPEWAAEVTLQPIRRFGMDAAILFSDILMLPWA

and should be renamed to Granulibacter bethesdensis CGDNIH1.faa

I have hundreds of files with suffix .faa in a folder and I am bored of trying this manually. There must be a way to do it quickly and elegantly.

The solution provided here does not work:

I appreciate your help! Thank you!

1 Answer

The solution you linked simply picks out the first line of the file (head-1), selects the first space-delimited string using cut, and then removes the FASTA header marker > using tr.

If you want to do something more complicated, you will need to modify the pipeline - for example using sed:

$ for f in *.faa; do echo mv --no-clobber -- "$f" "$(sed -E 's/.*\[([^]]*)\]/\ "$f")"; done
mv --no-clobber -- protein-44.faa Granulibacter bethesdensis CGDNIH1.faa

Remove the echo once you are satisfied that it is doing the right thing.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like