Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with -f or --file switch and a file with a backslash character (\) #832

Closed
leftcoastbeard opened this issue Feb 23, 2018 · 6 comments
Labels
doc An issue with or an improvement to documentation. question An issue that is lacking clarity on one or more points.

Comments

@leftcoastbeard
Copy link

What version of ripgrep are you using?

ripgrep 0.8.1
-SIMD -AVX

What operating system are you using ripgrep on?

Windows 10 x64, 1709

Describe your question, feature request, or bug.

-f or --file switch causing issues with files that have back slash character (\)

If this is a bug, what are the steps to reproduce the behavior?

Using file.txt as this:

This is text \fixme \hello \break \choose, etc...

This produces:

rg hello -f file.txt
Error parsing regex near 'xme \hello' at character offset 21: Unrecognized escape sequence: '\h'.
(Hint: Try the --fixed-strings flag to search for a literal string.)

If I run without the -f switch I get:

rg hello file.txt
1:This is text \fixme \hello \break \choose, etc...

Another weird error: test.txt

This is a test string.
With slashes\forward in it.
Going to break\rg.

Running:

rg slashes -f test.txt
slashes: IO error for operation on slashes: The system cannot find the file specified. (os error 2)
No files were searched, which means ripgrep probably applied a filter you didn't expect. Try running again with --debug.

Should have done this:

rg slashes test.txt
2:With slashes\forward in it.

If this is a bug, what is the actual behavior?

Debug for first:

rg text -f file.txt --debug
Error parsing regex near 'xme \hello' at character offset 25: Unrecognized escape sequence: '\h'.
(Hint: Try the --fixed-strings flag to search for a literal string.)

Debug for second:
Gist Here

The --debug stopped working after this dumped (couldn't replicate the debug output again).
Show the command you ran and the actual output. Include the --debug flag in
your invocation of ripgrep.

@leftcoastbeard
Copy link
Author

leftcoastbeard commented Feb 23, 2018

Am I misinterpreting the usage of the -f/--file parameter? Am I supposed to be using the file as my search query? facepalm

EDIT: Yup. The Slashes example is still an issue though.

@okdana
Copy link
Contributor

okdana commented Feb 23, 2018

The -f file contains a list of patterns that rg should search for (instead of using a pattern supplied directly on the command line). For example, these are basically equivalent searches:

# Pattern supplied on command line
% rg 'foo.*bar' file1 file2 file3

# Pattern supplied via file with `-f`
% cat patterns
foo.*bar
% rg -f patterns file1 file2 file3

The patterns in the file have to follow the same regex syntax rules as a pattern supplied on the command line. In Rust's regex engine, the \ character can be used either to make characters that are normally special not special (for example, \* matches a literal * character) or the other way around (for example, \d matches a digit). Since \ itself is therefore a special character, you need to escape it to make it match a literal \ in your file.

So, instead of \fixme (or whatever), make your pattern \\fixme.

Alternatively, you might be able to use the -F option to make rg treat patterns as literal strings (see rg --help).

@leftcoastbeard
Copy link
Author

leftcoastbeard commented Feb 23, 2018 via email

@BurntSushi
Copy link
Owner

@leftcoastbeard Thanks for the bug report! Could you please help us understand how we can improve ripgrep's documentation such that it would have prevented this confusion?

@BurntSushi BurntSushi added question An issue that is lacking clarity on one or more points. doc An issue with or an improvement to documentation. labels Feb 23, 2018
@leftcoastbeard
Copy link
Author

leftcoastbeard commented Feb 23, 2018 via email

@BurntSushi
Copy link
Owner

@leftcoastbeard Done! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc An issue with or an improvement to documentation. question An issue that is lacking clarity on one or more points.
Projects
None yet
Development

No branches or pull requests

3 participants