my $match = "DOID:2055"; while (){ my ($name,$id,$genes) = m/(.*?)\s+(\S+?:\S+?)\s+(.*)/; print "$genes\n" if $id eq $match; } __DATA__ Charcot-Marie-Tooth disease DOID:10595 KIF20A MTMR2 MTM1 LMNA HOXD10 PRX NEFL EGR2 LITAF GARS NDRG1 ERBB3 HSPB1 EMP2 MPZ ERBB2 PMP22 MFN2 GJB1 Post-traumatic stress disorder DOID:2055 APOE FKBP5 CRH IL2 SLC6A3 MAOB DBH IL8