I have a requirement to print all file name and line number which contain a text.
find . -name '*.cpp' -exec grep -n 'HELLO' {} \; > output.txt
but it's only printing the content and line number not the file name . How to achieve it? I am doing it on Solaris.
Here's an option using awk instead of grep:
find . -name '*.cpp' -exec awk '/HELLO/ { print FILENAME, NR }' {} + > output.txt
Whenever the pattern is matched, the filename and line number are printed. I have used -exec {} + so that multiple filenames are passed as arguments to a single invocation of awk, which should be quicker if you have lots of files to search.
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