Sample Header Ad - 728x90

How to individually process each path from a list of paths output from ripgrep

1 vote
2 answers
1882 views
I'm on Linux Ubuntu 18.04 and 20.04. Ripgrep (rg) can output a list of paths to files containing matches like this:
# search only .txt files
rg 'my pattern to match' -g '*.txt' -l
# long form
rg 'my pattern to match' --glob '*.txt' --files-with-matches
Output will be: > path/to/file1.txt > path/to/file2.txt > path/to/file3.txt etc. I'd like to then run another command on each path, such as tree $(dirname $PATH), to get a list of all files in the directory containing the matching file. How can I do that? I feel like xargs might be part of the answer maybe? But piping to xargs like this as a start seems to only handle the last-printed file:
rg 'my pattern to match' -g '*.txt' -l | xargs -0 -I {} dirname {}
Note: if you can demo with grep too that might be useful too for those without ripgrep, albeit ripgrep is super easy to install . ## References: 1. https://unix.stackexchange.com/questions/453183/ripgrep-print-only-filenames-matching-pattern/453188#453188
Asked by Gabriel Staples (2972 rep)
Dec 3, 2021, 06:15 AM
Last activity: Jul 26, 2023, 06:34 PM