I want to do a grep
in bash on a folder searching for the term foobar
. I then want to output all of the matches along w/ the filename and line number where the occurrences were found. Is this possible?
I need the output to look like:
{{filename}}:{{line_number}} - {{matched_text}}
grep -nrH 'regex' /dir/
-H
is the option that adds filenames, -n
adds line numbers, -r
is recursive
I get filenames even without the -H
unless there's only one file.
Add -i
for case-insensitive
Add -E
for extended regular expressions
Use the -n or --line-number option
grep -n foobar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With