in reply to Perl script to match a regexp in the prior line and other multiple lines

G'day kshitij,

"I am not able to link the regexp logic to match it based on the output file"

That shouldn't matter as "regexp logic" is not required here. You can do this with Perl's string-handling functions; which are quite likely to be faster than a regex solution (test with Benchmark).

This code:

#!/usr/bin/env perl use strict; use warnings; my $scan_data_found = 0; my @pins; my $format = "%-7s %-6s %-7s %s\n"; my @scan_line_data; printf $format, qw{pattern offset pin_num H/L}; while (<DATA>) { chomp; next unless length; if ($scan_data_found) { if (@scan_line_data) { my $caret_pos = 0; while (1) { $caret_pos = index $_, '^', $caret_pos; last if $caret_pos == -1; printf $format, @scan_line_data[0,1], join('', map $_->[$caret_pos], @pins), $scan_line_data[2][$caret_pos]; ++$caret_pos; } @scan_line_data = (); } else { push @scan_line_data, ((split ' ')[0,1], [split //]); } } else { if (0 == index $_, 'pattern') { $scan_data_found = 1; } else { push @pins, [split //]; } } } __DATA__ p p p p p Pin Numbers 3 2 1 8 9 1 2 3 4 5 pattern offset scan1 2965 H L H L H ^ scan2 2200 L H H L H ^ scan3 1100 H L L L L ^ scan4 1500 L L H H H ^ scan5 2800 H H L H H ^ ^

Produces this output:

pattern offset pin_num H/L scan1 2965 p13 H scan2 2200 p22 H scan3 1100 p22 L scan4 1500 p13 H scan5 2800 p22 H scan5 2800 p95 H

The split function does take a regex as its first argument; however, that's not the line matching regex you were talking about.

— Ken