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

Blank lines to delineate different files should not be emitted when using -NI flags #1709

Closed
sheisnicola opened this issue Oct 19, 2020 · 13 comments
Labels
doc An issue with or an improvement to documentation. rollup A PR that has been merged with many others in a rollup.

Comments

@sheisnicola
Copy link

What version of ripgrep are you using?

ripgrep 12.1.1
-SIMD -AVX (compiled)
+SIMD +AVX (runtime)

How did you install ripgrep?

Homebrew.

What operating system are you using ripgrep on?

Darwin nicolaw-aframe 19.6.0 Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64 x86_64

Describe your bug.

Blank lines are still printed between matching when using -NI flags to surppress the printing of the filename and line numbers. If filenames and numbers are being suppressed, then the blank line to separate each file has no meaning or context so I would not expect them to be printed.

What are the steps to reproduce the behavior?

Assuming that there are multiple files in your /etc/ filesystem that contain the word host, then I would not expect any blank lines to be emitted from the following command; I would only expect to see lines that contain the word host emitted:

rg -NI host /etc

What is the actual behavior?

Blank lines are printed to delineate the results from one matching file to the next.

What is the expected behavior?

No blank lines should be emitted.

@BurntSushi BurntSushi added the bug A bug. label Oct 19, 2020
@BurntSushi
Copy link
Owner

So I actually wrote out a bug fix for this, but now I'm second guessing whether this is really a bug or not. It just seems weird to me to disable headings in this particular case, but leave them present when --no-filename is given but line numbers are displayed. I guess in my view, having that gap between different files matching might actually be desirable? Or at least, if this bug were fixed, having that gap would be impossible. But today, you can remove the heading with the --no-heading flag (or by redirecting ripgrep's output away from a tty).

I'm inclined to leave ripgrep's behavior as is.

@BurntSushi BurntSushi added the question An issue that is lacking clarity on one or more points. label Oct 19, 2020
@sheisnicola
Copy link
Author

That's a very fair point. Perhaps this is a command line help/documentation fix then.

As it turns out my use-case does involve pumping this into a pipe, but I immediately pushed it into another grep and sort -u to "fixup" the output that I first saw come out of rg. I didn't realise change in formatting in such a situation.

I suspect that many people will have rg recommended to them by a friend or colleague like myself when performing ad-hoc command line data-mining for various purposes, and thus will start out crafting an initial rg command line like I did for its speed, and then iterate to pump it into some loop and might make the same presumptions that I did.

Apologies if this wasted your time.

Thank you for the solution!

@BurntSushi
Copy link
Owner

I see, yes. People are sometimes caught off guard by the change in formatting depending on whether ripgrep is printing to a tty or not. But even core commands do this occasionally. Compare the output of ls and ls | cat for example. It's subtle!

I'll switch this over to a documentation bug, although it's a little tricky. Maybe the tty style behavior can be mentioned more prominently in the docs. But I think we're getting close to a point where there are a few things being mentioned prominently, which of course weakens the notion of "prominent." :-/

@BurntSushi BurntSushi added doc An issue with or an improvement to documentation. and removed bug A bug. labels Oct 19, 2020
@sheisnicola
Copy link
Author

nods

Absolutely. Can't fault you there!

@murlakatamenka
Copy link

Wow, finding that issue wasn't easy, but that's exactly the problem I encountered.

My situation: I extract 1 field from multiple json-like files. I only need the value, so I use replace like this:

rg --no-filename --no-line-number --color=never --max-count=1 '"name"\s+"(.*)"' "*.acf" --replace '$1'

And then I get values separated with empty lines that I don't need.

It wasn't trivial to find what caused it. I tried --no-context-separator, --null, --null-data flags, searched through discussions and issues here until I found this one.

So the solution is to use --no-heading flag and it appears that empty line was an empty heading. Not complaining, but that wasn't so obvious to figure it all out :)

@BurntSushi
Copy link
Owner

@murlakatamenka Can you suggest a way to make it easier to figure out? Other than changing ripgrep's behavior (which as explained above is perhaps not such a good idea), what would have reduced the amount of time you spent tracking down the problem?

@murlakatamenka
Copy link

Ah, yes, I should've been more constructive, sorry for that.

The problem is that I failed to find out how to change it, so it's discoverability thing. You see above I tried not-so-relevant flags. I did check tab-completion to rg --no-<TAB>. I also checked man page / issues for following keywords:

  • separator
  • delimiter
  • blank / empty line
  • multiline
  • multiple (meaning multiple files)

all until I found this issue and that a-ha! moment: blank line belongs to the heading, --no-heading flag is what was looked for.

I think possible solution would be add more info to help / man page.

--heading
This flag prints the file path above clusters of matches from each file instead of printing the file path
as a prefix for each matched line. This is the default mode when printing to a terminal.

Like here we don't have any mention that the heading includes that blank line - separator of clusters of matches.


I would say steps to reproduce the issue are:

  1. I'd like to extract some data from files
  2. rg <pattern>, check results in the output
  3. no need for line numbers -> --no-line-numbers, cool
  4. don't need filenames -> --no-filename, obviously
  5. no blank lines, please -> ???, user is stuck

I mostly agree with what @sheisnicola wrote:

If filenames and numbers are being suppressed, then the blank line to separate each file has no meaning or context so I would not expect them to be printed.

In our use cases -NI meant "hey, rg, I don't care about source / location of the data, fetch me just it" and those blank lines weren't desirable. Guess there can be situations where you don't need this metadata but still want the clustering of matches.


So for now --heading help / man page can be improved. Maybe some notice of how you control output when searching through multiple files? Otherwise Interactive TAB-completion is usually enough for me to discover ripgerp's options and flags.

@BurntSushi BurntSushi removed the question An issue that is lacking clarity on one or more points. label Jan 11, 2021
@markkrj
Copy link

markkrj commented Feb 4, 2022

Also, when showing colors and line numbers, there is no point in having a line break between files... It is clear enough to what file a result belongs. You can see with: rg my_search --heading --pretty | sed '/^$/d'

@murlakatamenka
Copy link

@markkrj I like empty lines between results per file, for example, so this is personal preference

@ltrzesniewski
Copy link
Contributor

Since this issue is still open, I thought I'd add my 2 cents. 🙂

I was also surprised by the default behavior when I asked ripgrep to "just show me the raw matches" by adding -oNI.

It just seems weird to me to disable headings in this particular case, but leave them present when --no-filename is given but line numbers are displayed.

Actually, I was also surprised that line numbers were displayed by default when file names weren't. Line numbers don't seem to be very useful out of context (without a file name).

I guess what I really expected is for -I to be the equivalent of --no-filename --no-line-number --no-heading, but I suppose this would be too big of a change now. Anyway, having one convenient option for this would have been nice.

Or at least, if this bug were fixed, having that gap would be impossible.

Wouldn't the --heading option enable this?

@BurntSushi
Copy link
Owner

Actually, I was also surprised that line numbers were displayed by default when file names weren't. Line numbers don't seem to be very useful out of context (without a file name).

This isn't true. If file names aren't shown, then it's because the file name is unambiguous. That is, only one file was searched. For example:

$ rg WARRANTY UNLICENSE LICENSE-MIT
LICENSE-MIT
15:THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

UNLICENSE
16:THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

$ rg WARRANTY UNLICENSE
16:THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

I guess what I really expected is for -I to be the equivalent of --no-filename --no-line-number --no-heading, but I suppose this would be too big of a change now. Anyway, having one convenient option for this would have been nice.

Indeed, too big of a change and not one that makes sense to me personally. I'm not even sure what such an option would be called. I'd suggest creating an alias or a wrapper script for it.

Or at least, if this bug were fixed, having that gap would be impossible.

Wouldn't the --heading option enable this?

No? The issue here is precisely that the gap exists when --heading is set. And --heading is set by default when printing to a tty.

In fact, when doing a recursive search, both --heading and --line-number are only set when printing to a tty. That's because ripgrep is assuming that when printing to a tty, you want to see "human friendly" output. But you can squash that pretty easily by piping the output of ripgrep into cat. For example, the following two commands are equivalent:

$ rg -IN --no-filename --color never host /etc

and

$ rg -I host /etc | cat

Because when you aren't printing to a tty, ripgrep simply doesn't enable --heading, --line-number or --color auto. This matches grep's behavior when not printing to a tty. The only difference between ripgrep and grep here is that grep doesn't take as many liberties with the output when printing to a tty.

Given that I still feel the same way that I did back when I first looked at this issue, I don't think I'm going to take any action here. So I'm going to close this.

@BurntSushi BurntSushi closed this as not planned Won't fix, can't repro, duplicate, stale Aug 16, 2022
@BurntSushi
Copy link
Owner

Sorry, I forgot I marked this as a doc bug. So the action here is to document this somewhere in the man page and/or GUIDE. And that hasn't been done yet. But otherwise, I have no plans to add new flags or change how ripgrep behaves today.

@BurntSushi BurntSushi reopened this Aug 16, 2022
@ltrzesniewski
Copy link
Contributor

That makes sense, thanks for the explanation! 🙂

@BurntSushi BurntSushi added the rollup A PR that has been merged with many others in a rollup. label Nov 25, 2023
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Nov 28, 2023
14.0.2 (2023-11-27)
===================
This is a patch release with a few small bug fixes.

Bug fixes:

* [BUG #2654](BurntSushi/ripgrep#2654):
  Fix `deb` release sha256 sum file.
* [BUG #2658](BurntSushi/ripgrep#2658):
  Fix partial regression in the behavior of `--null-data --line-regexp`.
* [BUG #2659](BurntSushi/ripgrep#2659):
  Fix Fish shell completions.
* [BUG #2662](BurntSushi/ripgrep#2662):
  Fix typo in documentation for `-i/--ignore-case`.


14.0.1 (2023-11-26)
===================
This a patch release meant to fix `cargo install ripgrep` on Windows.

Bug fixes:

* [BUG #2653](BurntSushi/ripgrep#2653):
  Include `pkg/windows/Manifest.xml` in crate package.


14.0.0 (2023-11-26)
===================
ripgrep 14 is a new major version release of ripgrep that has some new
features, performance improvements and a lot of bug fixes.

The headlining feature in this release is hyperlink support. In this release,
they are an opt-in feature but may change to an opt-out feature in the future.
To enable them, try passing `--hyperlink-format default`. If you use [VS Code],
then try passing `--hyperlink-format vscode`. Please [report your experience
with hyperlinks][report-hyperlinks], positive or negative.

[VS Code]: https://code.visualstudio.com/
[report-hyperlinks]: BurntSushi/ripgrep#2611

Another headlining development in this release is that it contains a rewrite
of its regex engine. You generally shouldn't notice any changes, except for
some searches may get faster. You can read more about the [regex engine rewrite
on my blog][regex-internals]. Please [report your performance improvements or
regressions that you notice][report-perf].

[report-perf]: BurntSushi/ripgrep#2652

Finally, ripgrep switched the library it uses for argument parsing. Users
should not notice a difference in most cases (error messages have changed
somewhat), but flag overrides should generally be more consistent. For example,
things like `--no-ignore --ignore-vcs` work as one would expect (disables all
filtering related to ignore rules except for rules found in version control
systems such as `git`).

[regex-internals]: https://blog.burntsushi.net/regex-internals/

**BREAKING CHANGES**:

* `rg -C1 -A2` used to be equivalent to `rg -A2`, but now it is equivalent to
  `rg -B1 -A2`. That is, `-A` and `-B` no longer completely override `-C`.
  Instead, they only partially override `-C`.

Build process changes:

* ripgrep's shell completions and man page are now created by running ripgrep
with a new `--generate` flag. For example, `rg --generate man` will write a
man page in `roff` format on stdout. The release archives have not changed.
* The optional build dependency on `asciidoc` or `asciidoctor` has been
dropped. Previously, it was used to produce ripgrep's man page. ripgrep now
owns this process itself by writing `roff` directly.

Performance improvements:

* [PERF #1746](BurntSushi/ripgrep#1746):
  Make some cases with inner literals faster.
* [PERF #1760](BurntSushi/ripgrep#1760):
  Make most searches with `\b` look-arounds (among others) much faster.
* [PERF #2591](BurntSushi/ripgrep#2591):
  Parallel directory traversal now uses work stealing for faster searches.
* [PERF #2642](BurntSushi/ripgrep#2642):
  Parallel directory traversal has some contention reduced.

Feature enhancements:

* Added or improved file type filtering for Ada, DITA, Elixir, Fuchsia, Gentoo,
  Gradle, GraphQL, Markdown, Prolog, Raku, TypeScript, USD, V
* [FEATURE #665](BurntSushi/ripgrep#665):
  Add a new `--hyperlink-format` flag that turns file paths into hyperlinks.
* [FEATURE #1709](BurntSushi/ripgrep#1709):
  Improve documentation of ripgrep's behavior when stdout is a tty.
* [FEATURE #1737](BurntSushi/ripgrep#1737):
  Provide binaries for Apple silicon.
* [FEATURE #1790](BurntSushi/ripgrep#1790):
  Add new `--stop-on-nonmatch` flag.
* [FEATURE #1814](BurntSushi/ripgrep#1814):
  Flags are now categorized in `-h/--help` output and ripgrep's man page.
* [FEATURE #1838](BurntSushi/ripgrep#1838):
  An error is shown when searching for NUL bytes with binary detection enabled.
* [FEATURE #2195](BurntSushi/ripgrep#2195):
  When `extra-verbose` mode is enabled in zsh, show extra file type info.
* [FEATURE #2298](BurntSushi/ripgrep#2298):
  Add instructions for installing ripgrep using `cargo binstall`.
* [FEATURE #2409](BurntSushi/ripgrep#2409):
  Added installation instructions for `winget`.
* [FEATURE #2425](BurntSushi/ripgrep#2425):
  Shell completions (and man page) can be created via `rg --generate`.
* [FEATURE #2524](BurntSushi/ripgrep#2524):
  The `--debug` flag now indicates whether stdin or `./` is being searched.
* [FEATURE #2643](BurntSushi/ripgrep#2643):
  Make `-d` a short flag for `--max-depth`.
* [FEATURE #2645](BurntSushi/ripgrep#2645):
  The `--version` output will now also contain PCRE2 availability information.

Bug fixes:

* [BUG #884](BurntSushi/ripgrep#884):
  Don't error when `-v/--invert-match` is used multiple times.
* [BUG #1275](BurntSushi/ripgrep#1275):
  Fix bug with `\b` assertion in the regex engine.
* [BUG #1376](BurntSushi/ripgrep#1376):
  Using `--no-ignore --ignore-vcs` now works as one would expect.
* [BUG #1622](BurntSushi/ripgrep#1622):
  Add note about error messages to `-z/--search-zip` documentation.
* [BUG #1648](BurntSushi/ripgrep#1648):
  Fix bug where sometimes short flags with values, e.g., `-M 900`, would fail.
* [BUG #1701](BurntSushi/ripgrep#1701):
  Fix bug where some flags could not be repeated.
* [BUG #1757](BurntSushi/ripgrep#1757):
  Fix bug when searching a sub-directory didn't have ignores applied correctly.
* [BUG #1891](BurntSushi/ripgrep#1891):
  Fix bug when using `-w` with a regex that can match the empty string.
* [BUG #1911](BurntSushi/ripgrep#1911):
  Disable mmap searching in all non-64-bit environments.
* [BUG #1966](BurntSushi/ripgrep#1966):
  Fix bug where ripgrep can panic when printing to stderr.
* [BUG #2046](BurntSushi/ripgrep#2046):
  Clarify that `--pre` can accept any kind of path in the documentation.
* [BUG #2108](BurntSushi/ripgrep#2108):
  Improve docs for `-r/--replace` syntax.
* [BUG #2198](BurntSushi/ripgrep#2198):
  Fix bug where `--no-ignore-dot` would not ignore `.rgignore`.
* [BUG #2201](BurntSushi/ripgrep#2201):
  Improve docs for `-r/--replace` flag.
* [BUG #2288](BurntSushi/ripgrep#2288):
  `-A` and `-B` now only each partially override `-C`.
* [BUG #2236](BurntSushi/ripgrep#2236):
  Fix gitignore parsing bug where a trailing `\/` resulted in an error.
* [BUG #2243](BurntSushi/ripgrep#2243):
  Fix `--sort` flag for values other than `path`.
* [BUG #2246](BurntSushi/ripgrep#2246):
  Add note in `--debug` logs when binary files are ignored.
* [BUG #2337](BurntSushi/ripgrep#2337):
  Improve docs to mention that `--stats` is always implied by `--json`.
* [BUG #2381](BurntSushi/ripgrep#2381):
  Make `-p/--pretty` override flags like `--no-line-number`.
* [BUG #2392](BurntSushi/ripgrep#2392):
  Improve global git config parsing of the `excludesFile` field.
* [BUG #2418](BurntSushi/ripgrep#2418):
  Clarify sorting semantics of `--sort=path`.
* [BUG #2458](BurntSushi/ripgrep#2458):
  Make `--trim` run before `-M/--max-columns` takes effect.
* [BUG #2479](BurntSushi/ripgrep#2479):
  Add documentation about `.ignore`/`.rgignore` files in parent directories.
* [BUG #2480](BurntSushi/ripgrep#2480):
  Fix bug when using inline regex flags with `-e/--regexp`.
* [BUG #2505](BurntSushi/ripgrep#2505):
  Improve docs for `--vimgrep` by mentioning footguns and some work-arounds.
* [BUG #2519](BurntSushi/ripgrep#2519):
  Fix incorrect default value in documentation for `--field-match-separator`.
* [BUG #2523](BurntSushi/ripgrep#2523):
  Make executable searching take `.com` into account on Windows.
* [BUG #2574](BurntSushi/ripgrep#2574):
  Fix bug in `-w/--word-regexp` that would result in incorrect match offsets.
* [BUG #2623](BurntSushi/ripgrep#2623):
  Fix a number of bugs with the `-w/--word-regexp` flag.
* [BUG #2636](BurntSushi/ripgrep#2636):
  Strip release binaries for macOS.
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. rollup A PR that has been merged with many others in a rollup.
Projects
None yet
Development

No branches or pull requests

5 participants