how do I search for multiple patterns? #2586
-
I'm doing some tests with the following: ❯ fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g'
app-portage/eix
x11-wm/awesome
#!/usr/bin/env bash
echo "grep output"
grep "$(fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g')" test.txt
grep -c "$(fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g')" test.txt
echo ""
echo "rg output"
rg -U "$(fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g')" test.txt
rg -U --count "$(fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g')" test.txt Basically, I'd like to count how many times the output of the When running ❯ ./test.sh
grep output
x11-wm/awesome
app-portage/eix
2
rg output
Maybe I'm missing an option? is this because |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Please provide a command or sequence of commands that I can run on my machine that reproduces the problem. Your commands given depend on the directory contents of Note also that you've enabled |
Beta Was this translation helpful? Give feedback.
-
I can reproduce on an Arch Linux VM with the following: $ su -
# mkdir -p /var/db/pkg/app-portage/eix-99999999
# mkdir -p /var/db/pkg/x11-wm/awesome-9999
# fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g'
app-portage/eix
x11-wm/awesome
# echo "app-portage/eix:0" > test.txt
# echo "x11-wm/awesome:0" >> test.txt
# grep "$(fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g')" test.txt
app-portage/eix:0
x11-wm/awesome:0
# grep -c "$(fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g')" test.txt
2
# rg "$(fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g')" test.txt
the literal '"\n"' is not allowed in a regex
Consider enabling multiline mode with the --multiline flag (or -U for short).
When multiline mode is enabled, new line characters can be matched.
# rg -U "$(fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g')" test.txt
(no output)
# rg -U --count "$(fd --min-depth 2 --max-depth 2 -g "*-9999*" "/var/db/pkg" | cut -d "/" -f 5-6 | sed 's/-9999*//g')" test.txt
(no output)
As you can see in the sequence of commands above, I added |
Beta Was this translation helpful? Give feedback.
Well there's your problem. The pattern you've provided to ripgrep contains a
\n
. It looks like GNU grep treats that\n
as delimiting multiple patterns. ripgrep has different behavior. Instead it tells you that you can't search for\n
in a pattern unless multi-line mode is enabled. So by enabling multi-line mode, you're now searching for the entire pattern including the\n
. Just use the-f/--file
flag instead:It also works with grep.