Skip to content

--max-count breaks -passthru #2094

Open
@balupton

Description

@balupton

What version of ripgrep are you using?

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

How did you install ripgrep?

n/a

What operating system are you using ripgrep on?

Darwin redacted 21.1.0 Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/RELEASE_X86_64 x86_64

Describe your bug.

echo $'a\nb\nc\na\nb\nc' | rg --no-line-number --color never --multiline -m 1 --passthru 'b' --replace 'B'

outputs

a
B

not the expected output of:

a
B
c
a
b
c

This breaks the beneficial usage of --passthru, which is often used like so:

rg --multiline --passthru --max-count 1 "$pattern" --replace "$replace" "$file" | sponge "$file"

In my use case, I want to replace the first match with something, and trim away the remaining matches:

random="$RANDOM$RANDOM$RANDOM$RANDOM"
if rg --multiline --passthru --max-count 1 "$pattern" --replace "$random" "$file" | sponge "$file"; then
    rg --multiline --passthru "$pattern" --replace '' "$file" | sponge "$file" || :
    rg --multiline --passthru "$random" --replace "$replace" "$file" | sponge "$file"
fi

What are the steps to reproduce the behavior?

Run the code snippet earlier.

What is the actual behavior?

Run the code snippet earlier.

What is the expected behavior?

Documented earlier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions