Findstr output
Windows command line, I want to search for a file for all lines, starting with:
# NNN "<file>.inc" where NNN is a number and <file> any string.
I want to use findstr because I cannot require script users to install ack.
Here is the expression that I came up with:
>findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9_]*.inc" all_pre.txt The file to search is all_pre.txt .
So far so good. Now I want to pass this to another command, for example, more .
>findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9]*.inc" all_pre.txt | more The result of this is the same result as the previous command, but with the file name as the prefix for each line (all_pre.txt).
Then comes:
FINDSTR: cannot open | FINDSTR: cannot open more Why is the pipe not working?
all_pre.txt snapshot
# 1 "main.ss" # 7 "main.ss" # 11 "main.ss" # 52 "main.ss" # 1 "Build_flags.inc" # 7 "Build_flags.inc" # 11 "Build_flags.inc" # 20 "Build_flags.inc" # 45 "Build_flags.inc(function a called from b)" EDIT: I need to also avoid the dot in the regex. Not a problem, but worth mentioning.
>findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9_]*\.inc" all_pre.txt EDIT after Frank Bollack:
>findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9_]*\.inc.*" all_pre.txt | more doesn't work, although (I think) it should look for the same string as before, and any character as many times as necessary. That should include, " right?
Search template missing trailing \" .
findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9]*.inc\"" all_pre.txt | more This works for me.
Edit:
findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9]*\.inc.*\"" all_pre.txt | more This updated search string will now match these lines from your example:
# 1 "Build_flags.inc" # 7 "Build_flags.inc" # 11 "Build_flags.inc" # 20 "Build_flags.inc" # 45 "Build_flags.inc(function a called from b)" Edit:
To get around this βerrorβ in findstr , you can put your search in a batch file as follows:
@findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9_]*\.inc" %1 Call it something like myfindstr.bat and name it like this:
myfinsdtr all_pre.txt | more Now you can use pipe and redirection operators as usual.
Hope this helps.
I canβt explain the reason, but from my experience, although the behavior of findstr with fixed strings (for example, /c:"some string" ) exactly matches what you want, regular expressions are another beast. I usually use the fixed line search function to extract lines from CSV files:
C:\> findstr /C:"literal string" filename.csv > output.csv No problems.
But using regular expressions (for example /R "^\"some string\"" ) seems to force the output of findstr to the console and cannot be redirected by any means. I tried > , >> , 1> , 2> and everything crashes when using regular expressions. My workaround for this is to use findstr as a secondary command. In my case, I did this:
C:\> type filename.csv | findstr /R "^\"some string\"" > output.csv This worked for me without problems directly from the command line with a very complex regular expression string. In my case, I had to avoid "so that it works." Other characters, such as, and ... worked perfectly like literals in expression, without slipping.
I confirmed that the behavior is the same for both windows 2008 and Windows 7.
EDIT: Another option also works:
C:\> findstr /R "^\"some string\"" < filename.csv > output.csv this is the same principle as when using type , but just using the command line itself to create the channel.