piping findstr's output

一笑奈何 提交于 2019-12-04 19:55:46

You are missing a trailing \" in your search pattern.

findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9]*.inc\"" all_pre.txt | more

The above 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 circumvent this "bug" in findstr, you can put your search into a batch file like this:

@findstr /r /c:"^# [0-9][0-9]* \"[a-zA-Z0-9_]*\.inc" %1

Name it something like myfindstr.bat and call it like that:

myfinsdtr all_pre.txt | more

You can now use the pipe and redirection operators as usual.

Hope that helps.

I can't really explain the why, but from my experience although findstr behaviour with fixed strings (e.g. /c:"some string") is exactly as desired, regular expressions are a different beast. I routinely use the fixed string search function like so to extract lines from CSV files:

C:\> findstr /C:"literal string" filename.csv >  output.csv

No issue there.

But using regular expressions (e.g. /R "^\"some string\"" ) appears to force the findstr output to console and can't be redirected via any means. I tried >, >>, 1> , 2> and all fail when using regular expressions. My workaround for this is to use findstr as the secondary command. In my case I did this:

C:\> type filename.csv | findstr /R "^\"some string\"" > output.csv

That worked for me without issue directly from a command line, with a very complex regular expression string. In my case I only had to escape the " for it to work. other characters such as , and . worked fine as literals in the expression without escaping.

I confirmed that the behaviour is the same on both windows 2008 and Windows 7.

EDIT: Another variant also apparently works:

C:\> findstr /R "^\"some string\"" < filename.csv > output.csv

it's the same principle as using type, but just using the command line itself to create the pipe.

If you use a regex with an even number of double quotes, it works perfectly. But your number of " characters is odd, redirection doesn't work. You can either complete your regex with the second quote (you can use range for this purpose: [\"\"]), or replace your quote character with the dot metacharacter.

It looks like a cmd.exe issue, findstr is not guilty.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!