#!/usr/bin/perl # create names lookup table from first file while (<>) { $names{$_} = 1; last if eof; } $/ = "\n>"; # set input record separator # scan second file while (<>) { print if /^>?(\w+\n)/ && $names{$1}; } #### $ ./extract.pl file1 file2 >output #### $ perl -ne'$/eq"\n"?$nm{$_}++:/^>?(\w+\n)/&&$nm{$1}&&print;$/="\n>"if eof' file1 file2 >output #### $ perl -ne'1..eof&&($/=">")?${$_}++:/.+\n/&${$&}&&print' file1 file2 >output