0

I am working with about 100 different files that are coded in python, JavaScript, and HTML. Many of these files are coupled in complicated ways. One of the problems I am having pretty consistently is trying to trace a variable or object back to where it is defined. My question is whether there is a smarter way to grep for a common word. For example, here are a few key-value pairs being returned at the end of a python function.

return {
     "form": context["form"],
     "groups": context["groups"],
     "facilities": context["facilities"],
     "student": user,
     "topics": topics
 }

If I grep for topics in the directories where these files are located:

$ grep 'topics' * -R

I end up with way too many files to sort through. I can't narrow down my search to just .py files, because some JS and HTML files are coupled too. Any advice would be much appreciated!

ibanez221
  • 145
  • 3
  • 12
  • Are you looking for the absolute first time the variable is declared in all files? Or just for each file, the first time a ____ variable is declared? – Zhouster Aug 14 '14 at 23:44
  • Well in terms of this file, I want to trace it back to where it was declared, and see what other files are using it. Basically I have a lot of variables (i.e topics, exercise_log, exerciseData) that are used in the majority of the files of this project. I want to be able to trace them back for the context of the file I am working in, but also in reference to the files that import it as well. – ibanez221 Aug 14 '14 at 23:51
  • what file extensions are in the folder? – Padraic Cunningham Aug 14 '14 at 23:52
  • @PadraicCunningham you mean in the group of files I am working with? .py, .js, and .html – ibanez221 Aug 14 '14 at 23:54
  • Have you tried using an IDE? Pretty much anything but IDLE, whether it's PyCharm or emacs tags-mode, is going to have some "find definition" routine full of complex regexps and heuristics far better than what you're going to hack up in a couple minutes. – abarnert Aug 15 '14 at 01:06

2 Answers2

0

You could search for 'topics = ' to narrow it down to only places where a variable named topics is assigned to. This makes it easier to find out where a global variable originated from.

Jack
  • 2,223
  • 13
  • 23
0

One smart way, as stated by Jack, is to use the = sign to narrow down your search. You can further narrow this down to assignment with the assumption that the first time it occurs, it's probably getting assigned. This can be done as such:

for f in $(find .); do grep -m 1 'topics[[:space:]]*=' -R; done

This will apply the grep command on each file in the current directory and all subdirectories. It will then take the first time that the string occurs (in this case, 'topics').

Here is the manual page for grep that shows all the options: grep-man-page

Here is the Stack Overflow question that shows how to grep on each file and get first/last result: get-last-line-from-grep-on-multiple-files

Here is a Stack Overflow question dealing with whitespace in grep: grep-regex-whitespace

Community
  • 1
  • 1
Zhouster
  • 746
  • 3
  • 13
  • 23