Extract FASTA Identifiers

Foundational Bash Foundations Bash grep sed Text Processing
Significance:

Pulling the identifier list out of a FASTA file is how you check which sequences a
file actually contains before committing to an hour-long alignment run. It is also the first step in
cross-referencing two datasets — extract the IDs from each, then use comm or sort to find what they
share. Chaining a filter into a transformation is the fundamental Unix pattern.

Statement

FASTA-formatted text arrives on standard input.

For every header line, print the identifier: the header text with the leading > removed, truncated at
the first space if a description follows.

Print one identifier per line, in the order the records appear.

Input — read from standard input
Variable Type Description
fasta
line 1..n
str FASTA-formatted text on standard input
1 <= records <= 1000

These variables are already read for you in the starter code on the right.

Output

str one identifier per line, in record order, with the leading > and any description removed

Sample Cases
Sample 1
Input
>seq1 first sequence
ACGT
>seq2 second one
TTTT
Expected Output
seq1
seq2
Both descriptions are dropped, leaving just the identifiers.
Sample 2
Input
>plain
ACGT
Expected Output
plain
A header with no description at all.

Submit also runs your code against 3 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.

Constraints
  • Identifiers contain no spaces
  • Any text after the first space in a header is a description and must be dropped
  • Sequence lines are ignored entirely
  • Output order matches input order
Further Reading
  • Filter the header lines first, then transform them — two simple steps beat one complex expression.
  • sed 's/^>//' strips the leading marker; cut -d' ' -f1 takes the field before the first space.
  • awk '/^>/ {print substr($1, 2)}' does the whole job in a single tool if you prefer.

Judged against GNU Bash 5.1.0.


My Notes
Log in to save personal notes.
Console output will appear here when you click Run Code or Submit...
Expected: fasta — the whole input (FASTA-formatted text on standard input)
Next Problem
Extract a Column from a TSV Stream