Skip to content

Unexpected (or incorrect?) multiline replace behavior #1311

@mqudsi

Description

@mqudsi

What version of ripgrep are you using?

ripgrep 11.0.1 (rev 34677d2)
+SIMD -AVX (compiled)
+SIMD +AVX (runtime)

How did you install ripgrep?

cargo from git

What operating system are you using ripgrep on?

Distributor ID: Debian
Description: Debian GNU/Linux 10 (buster)
Release: 10
Codename: buster

Describe your question, feature request, or bug.

I was trying to replace some of my usages of other commands (sed, tr, ag) with ripgrep now that it has proper multiline and pcre2 support features, and ran into the following odd behavior:

> printf "hello\nworld\n" | rg -U "\n" --color always 
hello
world 

This works fine, everything matches, nothing is highlighted because the newline can't be colored.

But when I try to replace, I get the following behavior:

> printf "hello\nworld\n" | rg -U "\n" -r "?" 
hello?
world?

which isn't correct because the newline has been correctly matched but incorrectly retained in the output.

Whereas the following does work:

> printf "hello\nworld\n" | rg --null-data "\n" -r "?"
hello?world?

But I don't see any documented reason why the former shouldn't give the same output as well. I can understand technically why this is happening (rg internally preserves the concept of lines but merely glosses over them for search purposes with -U), but the confluence of both -U and -r results in incorrect behavior: the output should either include the replacement character and no new lines (desirable) or the newlines and no replacement character (unfortunate but understandable limitation). I don't think including both can be considered correct, however.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA bug.rollupA PR that has been merged with many others in a rollup.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions