# Always read up to the next ">" at the beginning of a line: $/ = "\n>"; getc(INPUT); # skip the first ">" while () { # remove "\n>" chomp; # first line is the ID, the rest is the sequence my ($id, $seq) = /^([^\n]*)\n(.*)/s; # remove whitespace in the sequence $seq =~ s/\s+//g; # do something with $id and $seq ... # output the result in 72-column format print OUTPUT ">$id\n"; print OUTPUT "$1\n" while $seq =~ /(.{1,72})/g; }