A quick way to find specific lines of code across multiple files in a project

I am currently working on a C project that contains over 50 .h and .c files. I would like to know if there is a quick way to search for specific lines of code (e.g. ctrl + f for a window, for example), without having to actually search each file one by one.

Thank you in advance

+6
source share
5 answers

Linux / Unix has a grep command-line tool that you can use to search for multiple files for a string. For example, if I wanted to search for strcpy in all files:

 ~/sandbox$ grep -rs "strcpy" * test.c: strcpy(OSDMenu.name,"OSD MENU"); 

-r gives the search recursively, so you get all the files in all directories (from the current). -s ignores warnings if you run unreadable files.

Now, if you want to find something ordinary, and you can’t remember the case, there are options like -i to allow insenstive searches.

 ~/sandbox$ grep -rsi "myint" * test.c: int myInt = 5; test.c: int MYINT = 10; 

You can also use regular expressions if you forget exactly what you were looking for (indeed, the name "grep" comes from the command sed g / re / p - global / regular expression / print:

 ~/sandbox$ grep -rsi "my.*" * test.c: int myInt = 5; test.c: int MYINT = 10; test.c: float myfloat = 10.9; 
+6
source

You can use grep to search files using terminal / command line.

 grep -R "string_to_search" . 

-R be recursive, search in all subdirectories too

Then the line you want

Then this is the location. for current directory

+3
source

install cygwin if you are not using * nix and use find / grep, for example.

 find . -name '*\.[ch]' | xargs grep -n 'myfuncname' 
+3
source

If you are using a text editor and shell, you can use shell tools such as grep .

 grep -R "some pattern" directory 

However, you should use an IDE such as Eclipse (this is not only for Java), Netbeans (there is a C plugin), or KDevelop. In IDEs, there are keyboard shortcuts for things like "find everywhere when a highlighted function is called."

Or, of course, Emacs ...

+2
source

In windows, you can use findstr, which will find files containing strings that either match exactly or the regular expression matches the specified string / pattern.

 findstr /? 

from the command line will give you the ability to use. It can also rewrite subdirectories (/ s).

+1
source

All Articles