Question

I've got a file where I need to extract lines that start with "x" that is immediately suffixed only with a non-negative number and nothing more. For example, from the following lines:

x10
x111 116
x437 444
x2095
x2103 2104
x111
x2451 2506
x20
x10
x40 50
x57 58
x76

I need to extract these ones:

x10
x2095
x111
x20
x10
x76

It looks easy but the following regex doesn't work for me and I can't figure out why:

FINDSTR /R /C:"^x[0-9][0-9]*$" file.txt

Am I doing it wrong or is it another quirk in FINDSTR?

Was it helpful?

Solution

findstr /R "^x[0-9][0-9]*$" file.txt

The /C switch is for a literal search string.

Edit: also note that there must be a newline at the end of file.txt (after the "x76") or findstr will not pick up that line because it doesn't match the "$" for "end of line".

OTHER TIPS

/C:string Uses specified string as a literal search string. You need to run

FINDSTR /R "^x[0-9][0-9]*$" file.txt

D'oh, the /C switch doesn't seem to be the reason. The command line given in the question actually works, at least for the input I provided. I've just launched it successfully at least once on Windows 7 x64. The other time, it failed (the command returned no output).

I'm sorry I upvoted your answers too early... two similar replies convinced me that the /C option was wrong...

EDIT: I've finally found it: the input file came from a tool that was adding lines with Windows-like line breaks. While the file was generated, it invoked external cygwin tool that was writing to the same file but with Unix-like line breaks. That's why FINDSTR was giving inconsistent result with this file along with "$" regex that didn't matched all lines it should.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top