I'm very new to ripgreb, but very impressed by it.
I have a very special triggy case that I was wondering if could be done
Say I have two markdown files
...
## 5.9 Subheading
plz, plz, plz do NOT find me!
# 6 Heading
## 6.1 subheading
plz, plz, plz FIND me!
## 6.2 subheading
plz, plz, plz do NOT find me!
...
and
...
## 6.1 subheading
plz, plz, plz FIND me too!
## 6.2 subheading
plz, plz, plz do NOT find me too!
...
Would it be possible to search within ## 6.1
and ## 6.2
for lines with more than one comma per line?
So it displayed something like this
$rg <magic-search term>
221 plz, plz, plz FIND me!
256 plz, plz, plz FIND me too!
Currently I just do this to see it
rg '## 6.1' --before-context 0 --after-context 4
but that does not search for multiple commas after '## 6.1' or limit the search to be after
I found a "solution" for ripgrep and just dropping it here:
$rg '## 6.1.*## 6.2' --multiline --multiline-dotall
220 ## 6.1 subheading
221 plz, plz, plz FIND me!
222 ## 6.2 subheading
255 ## 6.1 subheading
256 plz, plz, plz FIND me too!
257 ## 6.2 subheading
Currently it also prints out the headers (## 6.1
and ## 6.2
)
Trying to avoid that by doing something like this but that just removes this first #
and not the entire line
$rg '(?:(?!## 6.1).)*## 6.2' --multiline --multiline-dotall --pcre2
220 # 6.1 subheading
221 plz, plz, plz FIND me!
222 ## 6.2 subheading
255 # 6.1 subheading
256 plz, plz, plz FIND me too!
257 ## 6.2 subheading
Thanks goes to chriserin
Converting my comment to answer so that solution is easy to find for future visitors.
You may use this awk
:
awk -F, '/^## 6\.1 /{p=1; next} /^## 6\.2 /{p=0} p && NF > 1' file
Flag p
will be set to 1
when we encounter ## 6.1
and will be reset when we encounter ## 6.2
.
To use with wildcards that also prints filenames:
awk -F, '/^## 6\.1 /{p=1; next} /^## 6\.2 /{p=0}
p && NF > 1{print FILENAME ":", $0}' *