#!/usr/bin/perl
use strict; # https://perlmonks.org/?node_id=11114988
use warnings;
my @lines = ("t13:45\n", "D13:45\n", "S13:45 Unicorn\n","D13:45\n",
"S13:45\n", "T13:45\n", "t13:45\n", "D13:45\n", "T13:46\n","t13:45\n
+",
"D13:45\n", "S13:45\n","D13:45\n", "S13:45 UNICORN\n", "T13:45\n",
"t13:45\n", "D13:45\n", "T13:46\n");
my $value = 'unicorn';
my @sections = grep /$value/i, (join '', @lines) =~ /^t.*?\nT.*?\n/gms
+;
print join "\n", @sections; # so there is a gap between sections
Outputs:
t13:45
D13:45
S13:45 Unicorn
D13:45
S13:45
T13:45
t13:45
D13:45
S13:45
D13:45
S13:45 UNICORN
T13:45