Search code examples
powershellfindstr

windows powershell findstr incorrect behaviour


I am looking for a search string inside a json file:

> type .\input.json
[
    {"name": "moish"},
    {"name": "oren"}
]
> type .\input.json | findstr /n /l "\`"name\`": \`"or"
2:    {"name": "moish"},
3:    {"name": "oren"}

How come moish entry is found? what am I missing?


Solution

  • Note: The quoted lines below, originally a direct part of the answer, turned out not to apply to the problem at hand, because the escaping in the question is correct (the only thing missing was placing /c: directly before the string to make findstr.exe search for it as a whole).

    See this answer for a more comprehensive analysis of the problem.

    Escape the literal quotation marks by doubling them:

    type input.json |findstr /n /l """name"": ""or"

    ... or use single-quotes to qualify the search term:

    type input.json |findstr /n /l '"name": "or'

    .... or perhaps use the native PowerShell cmdlet Select-String instead of findstr:

    Select-String -LiteralPath input.json -Pattern '"name": "or'