use strict; use warnings; while( <> ) { chomp; # Test for validity of input, and capture significant digits. # If input seems invalid, die. unless( m/^(?:2|42) # Match but don't capture your prefix. (\d{11})\s # Match and capture left-grouping of digits. (\d{13})\s # Match and capture mid-grouping of digits. (\d3)/x # Match and capture right-grouping of digits. ) { die "Improperly formatted input data:\n\tLine: $.\t$_\n"; } my( @columns ) = ( $1, $2, $3 ); # Retain the captures. # Look for "reject" codes. next if $columns[1] =~ m/^\d{6}(?:406|408)\d{4}\s$/; # Print a nicely formatted result. print "CH $columns[0] $columns[1] $columns[2] LAB\n"; }