Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using grep to find all strings (with %) in files recursively

To find all occurrences of printf in all files recursively:

grep -rnw ~/bin/ -e "printf"

However, how to escape the search pattern printf '%0?

I failed in the following trials to escape the percentage sign:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

grep -rnw ~/bin/ -e "printf '%0"

grep -rnw ~/bin/ -e "printf \'\%0"

grep -rnw ~/bin/ -e "printf \'%%0"

Sample file:

#!/bin/sh

printf '%04d' "$var"

>Solution :

It’s not working because you used the -w option, which only matches whole words. In your file, %0 is the beginning of the word %04d. Since there’s no word boundary after %0, it doesn’t match.

Get rid of the -w option and it will match. However, it will also match

fprintf '%04d' "$var"

Since you want to match a word boundary before printf, use the \b word boundary pattern.

grep -rn ~/bin/ -e "\\bprintf '%0"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading