#!/usr/bin/perl use warnings; use strict; my $save; while () { if (/([^{TAG}]*)/) { $save = $1; print $save; } } __DATA__ {TAG} 0012310002 and 0012310003 {COUNT} 000000 {COUNT2} 000000 and 100001 {TAG} 2304854