Domanda

Often I do different projects and sometimes there is a lack of documentation.

So I decided to use open-source code for looking how people solved different problems.

The idea is if I run into function what I don't how to use I look for different developers used that function before.

Approach: I downloaded a few pretty decent projects done by other people and put them into one folder.

Now, if I don't know how a function is used (e.g. main() ), I do :

find . -name \*.py  | xargs cat | grep -n "main()"

Consequently I get examples of its use: enter image description here

But there is a problem. I don't know from which file examples are. It'd be perfectly if it was possible to get name of the file as well as number of line.
It seems to be limitation of use "cat" command because it mixes all files together and as result I get information about number not in the file but rather in cat output. So I feel this approach is bad in the root.

i.e. I want to be able to look for functions/symbols in plethora of source code and get information about the line and file where a certain combination was met.
I prefer console-way.

Any advice?

È stato utile?

Soluzione

Try this:

find . -name \*.py -exec grep -nH "main()" {} \;

Explanation:

The "-exec" option says to execute the following command, up until \; for each file it finds.

The "-H" option to grep causes it to print the name of the file in which the string was found.

The "-n" option causes grep to print the line numbers.

The {} is a placeholder that expands to the name of the file that "find" just found.

Altri suggerimenti

You need only grep command:

$ grep -nr 'main()' /path/to/projects/folder/* | grep '.py:'

Want to search source files ? Why not http://beyondgrep.com/ ?

I wont answer you from the point of the bash.

I dont know which editor/IDE are you using, but for code dissecting there is no better tool for me then:

  • Vim with Ctags combination
  • Ctrl-p,Ctrl-p funky and MRU plugin +
  • proper search and regex usage.
  • good vim debugger

There is no part of code that cant be examined. Please sorry if are using some other tools, I am just suggesting you what do I find is the best for code analysis for me.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top