0

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?

MazeGen
  • 180
  • 3
  • 14

3 Answers3

3

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".

taz
  • 1,506
  • 3
  • 15
  • 26
  • 2
    Yeah, `findstr` has some weird quirks, like an apparent lack of the `+` regex token...(see http://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman). If you don't control the input file there is probably a way to use redirect and/or string magic to append a newline to the contents of the file and pass that to `findstr` instead of the file itself, to ensure that all processed lines will in fact end in a newline. – taz Oct 15 '13 at 18:03
1

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

FINDSTR /R "^x[0-9][0-9]*$" file.txt
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

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.

MazeGen
  • 180
  • 3
  • 14