Classic map-sort-map:
- Turn each line into an anonymous array with two items - the last column, and the full line.
- Sort those arrays by their first element (the last column).
- Print just the second element (the full line) for each anonymous array.
The only trick is, in #1, keep track of the smallest last column.
my $least;
print map { abs($_->[0]-$least)<500 && $_->[1] }
sort{ $a->[0] <=> $b->[0] }
map { /(\d+)$/;
$least = $1 if !defined $least or $1<$least;
[ $1, $_]
} <DATA>
__DATA__
ID1 ch1 70 mir abc xyz ch2 2050
ID2 ch1 90 mir abc xyz ch2 4000
ID3 ch1 100 mir abc xyz ch2 2045
ID4 ch1 120 mir abc xyz ch2 2025
# prints:
ID4 ch1 120 mir abc xyz ch2 2025
ID3 ch1 100 mir abc xyz ch2 2045
ID1 ch1 70 mir abc xyz ch2 2050