This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract first and last column of fasta-header

Hi everyone,

I have a multi-fasta file name multi.fasta with the following structure:

>A 124 B
ATCGTA...
>C 567 D
GTCAG...

My goal is to create a new file, with the new fasta-headers containing only the first and last column. If I use

awk -F" " '/>/ {print $1,$(NF)}' multi-fasta > modified_multi-fasta

This will print the fasta headers with the first and last columns, but won't print the nucleotide sequences. Can you guys please help me out?

sequence

3 answers

 awk '/^>/ {print $1,$(NF);next;} {print;}' 
$ awk '/^>/{$2 =""}1' test.fa

>A  B
ATCGTA...
>C  D
GTCAG...

$ awk '/^>/{print $1,$NF}!/>/' test.fa
$ cut -f1,3 -d" " test.fa
$ sed -r '/^>/ s/\s.*\s/ /1' test.fa
$ tr -d 0-9 <test.fa | tr -s " "

>A B
ATCGTA...
>C D
GTCAG...

You can use the "Rename header / Multipart header" operation of SEDA (https://www.sing-group.org/seda/manual/operations.html#multipart-header), it is very useful for this kind of FASTA headers.

Log in to answer this question.