Color a specific pattern in the output of `find`

Code

Example to identify all definitions of custom functions in files with the Rmd extension, and to color the names of these functions:

#!/usr/bin/bash

find . -name "*Rmd" -type f \
    -exec grep -E ' = function \(' {} + |
    grep -vE 'FUN|%' |
    grep --color -P '(?<=:).*(?= = fun)'

Output

The output contains several rows with this pattern:

filepath/filename.Rmd:function_name = function( ….

Explanations

  • find . -name "*Rmd" -type f: in all files with the Rmd extension in the folders currently (.) accessible…
  • -exec grep -E ' = function \(' {} +: …, identify all the lines containing = function(
  • grep -vE 'FUN|%': then, remove the lines containing FUN or %. I use this to remove the function that are defined in apply function and in pipes, for instance.
  • grep --color -P '(?<=:).*(?= = func)': then, color the characters between : and = func



Related (find) topics: