Recursively search for text in R scripts.

December 13, 2016
By

(This article was first published on Quantitative Ecology, and kindly contributed to R-bloggers)

Often I find myself needing to search for .R or or .Rmd files in which I have used a specific function. There are a variety of searches that you can do; however, I wanted something that would work recursively at the command line of a Linux or Mac terminal. I found the answer here and have modified it a bit so that it works as a function in Bash (in this example I search through .R and . Rmd files).

Add the following to your ~/.bash_aliases file and then source ~/.bashrc:


findinRfun() {

if [ -z "$1" ]
then
echo "No argument supplied"
else
#search text is passed as argument $1
find . -type f \( -name "*.R" -o -name "*.Rmd" -o -name "*.r" -o -name "*.rmd" \) -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "$1"
fi
}

alias findinR=findinRfun

Now, at the command line, you can search through all the files below the current location that contain your search text.

findinR mysearchtext

To leave a comment for the author, please follow the link and comment on their blog: Quantitative Ecology.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)