'Grep and CMD escaping
I am trying to grep this pattern: ^#include\s+"[^"]+"
Unfortunately, when I try the following in a Windows batch file, the pipe (|) and the sort are treated as inputs to grep, probably because of the unbalanced quotes.
> grep -P '^#include\s+"[^"]+"' --include=*.h --include=*.cpp | sort
grep: |: No such file or directory
grep: sort: No such file or directory
I tried different combinations of escaping, both with backslash and with caret, but could not get it to work.
Edit:
By trial and error, I got this to work: grep -P '^^#include\s+\"[^\"]+\^"' --include=*.h --include=*.cpp | sort
It is a weird mix of CMD escapes (^) and regex escapes (\), without an apparent rhyme or reason to which one needs to be used where.
I am leaving the question open in the hope that someone will offer a general explanation.
Solution 1:[1]
PowerShell has regex support. If you are on a supported Windows system, PowerShell was installed with it.
=== Do-Grepit.ps1
Get-ChildItem -Path '*.h','*.cpp' |
ForEach-Object { Select-String -Pattern '^#include\s+"[^"]+"' -Path $_ }
At an interactive console, using aliases could make for less typing, but aliases should not be coded into script files.
gci -pa *.h,*.cpp|%{sls '^#include\s+"[^"]+"' $_}
If you are desperate to run this from a cmd batch-file, then you could:
powershell -NoLogo -NoProfile -File "Do-Grepit.ps1"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
