Among my usually like to go, Gnu (http://www.gnu.org) is one of them. The fsf / unsco free software directory (http://www.gnu.org/directory/) is like a treasure galler who is deep, and it has an excellent software that is not countful, some of which have become my daily development process. Indispensable tools, such as Emacs, GREP, etc. Today I want to introduce you to GREP (GLOBAL Regular Expression) - an extremely powerful document search tool.
On the home page of GREP (http://www.gnu.org/software/grep), the introduction of GREP is only such a simple sentence:
GREP Searches One or More Input Files for Lines Containing a match to a specified pattern. By Default, Grep Prints the match lines.
Simply put, you can use grep to find a string located in a file and can control the results display. When you read the source code, GREP is almost essential. Through it you can find a large project's entrance and easily find the functions or variables you need between massive source code.
Let me take a simple example: I want to query Main in the .cs file in the current directory, I can easily complete this task via GREP, the command is as follows:
Grep '
Main
'* .Cs
The example output on my machine is:
ChineseDemo.cs: public static void
Main
()
Regdemo1.cs: static void
Main
(String [] ARGS)
Regdemo2.cs: public static void
Main
()
Regdemo3.cs: public static void
Main
()
It can be seen that all files that meet the conditions in the relevant directory can be found via GREP, and can print the contents of the query string. If I just want to determine which files contain main, don't print the content of the corresponding line, then you can use the following command:
Grep -l '
Main
'* .Cs
The output is:
ChineseDemo.cs
Regdemo1.cs
Regdemo2.cs
Regdemo3.cs
Only the corresponding file name is displayed.
After finding a few files containing the main string, I want to roughly check the information about the inner source code of a specific file, such as viewing the front and rear three lines of the regdemo1.cs file, the relevant commands are as follows :
GREP-C 3 'main' regdemo1.cs
The output is:
Class GroupingApp
{
Static void
Main
(String [] ARGS)
{
REG7 ();
}
The output results are fully compliant with my needs, and all the code within the three-line range in the three-line range before and after this line (the first line is blank).
The above query strings do not limit whether the results of the lookup result is a complete word, that is, themain and main_func are identified as a query target. If I want to limit the query string as a full word, then the / w parameter can be used, the relevant commands are as follows:
Grep -w '
Main
'* .Cs
The result is:
ChineseDemo.cs: public static void
Main
()
Regdemo1.cs: static void
Main
(String [] ARGS)
Regdemo2.cs: public static void
Main
()
Regdemo3.cs: public static void
Main
()
Note: The starting brackets behind Main will not be identified as words by the regular expression processing engine embedded in the GREP.
The above queries are just a simple query based on a single string, and the most powerful of GREP can directly use the regular expression, so that various complicated various query operations can be completed. For example, the simple command below is querying the string of arg starting in all current directory: CS files:
GREP -E 'arg [/ s] *' * .cs
The output is:
Regdemo.cs: private string arg;
Regdemo.cs: public static void
Main
(String [] ARGS)
Regdemo.cs: foreach (String Arg in args)
Regdemo.cs: console.writeLine (arg);
Regdemo1.cs: static void
Main
(String [] ARGS)
OK, the method of use of GREP will be briefly introduced here. If you are interested, you can go to GREP's master (http://www.gn/) to find related documents, you can also go to GNU FTP (ftp://ftp.gnu.org/gnu/grep/ ) Go to the download source code and compile, the latest version is 2.5. However, for most Windows developers, there is no corresponding MAKE environment on the machine, so it may be necessary to switch to the following two methods:
1. Install and configure Cygwin (http://www.cygwin.com), selecting GREP when selecting the installation component. After the installation is complete, remember to add the
2. Download and install the third-party manufacturer to develop the grep developed for the Windows platform. There are two current: Powergrep (http://www.powergrep.com) and Windows Grep (http://www.wingrep.com). I have not used these two tools, you can check the corresponding document to decide whether to use.