in reply to Re^2: How do I display only matches
in thread (SOLVED) How do I display only matches

I think you may be over complicating things with a regex that is both more complicated and harder to understand than necessary?? I mean it looks like the file is a Windows dir listing? I would suggest:
while (my $line = <$fh>) { print "$line" if $line =~ /^\s+Directory of/; }
No need to chomp if you are just going to add the line ending back in. Forcing at least one space at the beginning of the line narrows things down a lot. Putting in "Directory of" makes it very easy to understand what line of this file you are actually looking for. Please correct me if your dataset if more complicated than you've shown.

I would also add that in my work, keying a regex to a particular column number is usually a bad idea because counting the columns can be error prone and there can be some variance if the file could have been generated with "cut-n-paste". YMMV

Replies are listed 'Best First'.
Re^4: How do I display only matches
by tem2 (Novice) on Sep 24, 2019 at 07:04 UTC

    My problem is to detect directories which are out of order. The proper order of the tree should read as follows:

    Directory of D:\ \Q Directory of D:\ \R Directory of D:\ \S

    Due to operator mishandling, the directories can look more like this:

    12345678901234567890 Directory of D:\ \Q Directory of D:\ \Q\X Directory of D:\ \R Directory of D:\ \S

    I need to identify the anomalies like \Q\X. FINDSTR doesn't work, so I was able to come up with the regex \.{20}\\a-zA-Z\s\r\n/ There will always be 20 characters followed by a backslash, then a letter, then a space, and then a CRLF. The regex is accurate because it works in the online regex tester https://regex101.com/r/LFrvLp/11

    I have zero experience with perl scripting, I was looking for code to read a file and found the snippet with the "chomp" function. All I want is to read an input file and output a list of matches for the regex. Thank you for your assistance!