FINDSTR - Searching across Line Breaks

FINDSTR breaks lines immediately after every <LF>. The presence or absence of <CR> has no impact on line breaks.

The . regex metacharacter does not match <CR> or <LF>. But it is possible to search across a line break using a command line search string. Both the <CR> and <LF> characters must be matched explictly. If a multi-line match is found, only the 1st line of the match is printed. FINDSTR then doubles back to the 2nd line in the source and begins the search all over again - sort of a "look ahead" type feature.

Assume TEXT.TXT has these contents (could be Unix or Windows style)


Then this script

@echo off
::Define LF variable containing a linefeed (0x0A)
set LF=^

::Above 2 blank lines are critical - do not remove

::Define CR variable containing a carriage return (0x0D)
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

setlocal enableDelayedExpansion
::regex "!CR!*!LF!" will match both Unix and Windows style End-Of-Line
findstr /n /r /c:"A!CR!*!LF!A" TEST.TXT 

gives these results


Searching accross line breaks using the /G:FILE option is imprecise because the only way to match <CR> or <LF> is via a regex character class range expression that sandwiches the EOL characters. For <LF> (Ascii 0x0A) the range is the 3 characters 0x09 - 0x0B. For <CR> (Ascii 0x0D) the range is the 3 characters 0x0C - 0x0E but the next printable character is a ! (0x21).

Dave Benham - List of undocumented features and limitations of FINDSTR from StackOverflow

“They come from the end of the line, most of 'em. Small towns you never heard of: Pulaski, Tennessee; Brandon, Mississippi; Pork Van, Utah; Wampum, Pennsylvania. Two years' high school's about it, maybe if they're lucky a job waiting for them back at a factory, but most of 'em got nothing. They're poor, they're the unwanted, yet they're fighting for our society and our freedom. It's weird, isn't it? They're the bottom of the barrel and they know it. Maybe that's why they call themselves grunts, cause a grunt can take it, can take anything. They're the best I've ever seen, Grandma. The heart & soul.” ~ Platoon


FINDSTR - Search for a text string in a file.
Equivalent bash command (Linux): grep - Search file(s) for lines that match a given pattern

Copyright © 1999-2017
Some rights reserved