个人工具

“UbuntuHelp:Grep”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
 
(未显示3个用户的6个中间版本)
第12行: 第12行:
 
% grep 'Nicolas Kassis' *
 
% grep 'Nicolas Kassis' *
 
</nowiki></pre>
 
</nowiki></pre>
Notice the use of single quotes; This are not essential but in this example it was required since the name contains a space. Double quotes could also have been used in this example.
+
Notice the use of single quotes; the quotes are not essential, but in this example they are required because the name contains a space. Double quotes could also have been used in this example.
 
Now lets use some regular expressions...
 
Now lets use some regular expressions...
 
=== Grep Regular Expression ===
 
=== Grep Regular Expression ===
grep can search for complicated pattern to find what you need. Here is a list of
+
grep can search for complicated patterns to find what you need. Here is a list of
 
some of the special characters used to create a regular expression:
 
some of the special characters used to create a regular expression:
 
{|border="1" cellspacing="0"
 
{|border="1" cellspacing="0"
第34行: 第34行:
 
| [^] || Does not match any characters listed  
 
| [^] || Does not match any characters listed  
 
|-
 
|-
| \/ || Denotes the beginning  and end (respectively) of a word   
+
| \ || Denotes the beginning  and end (respectively) of a word   
 
|}
 
|}
 
So an example of a regular expression search would be
 
So an example of a regular expression search would be
第43行: 第43行:
 
For more details check:
 
For more details check:
 
* [[UbuntuHelp:BasicCommands|BasicCommands]]
 
* [[UbuntuHelp:BasicCommands|BasicCommands]]
* http://www.gnu.org/software/grep/doc/
+
* http://www.gnu.org/software/emacs/manual/html_node/emacs/Grep-Searching.html
 
* http://en.wikipedia.org/wiki/Grep
 
* http://en.wikipedia.org/wiki/Grep
 
* '''man grep''' and '''info grep''' on your computer
 
* '''man grep''' and '''info grep''' on your computer
 
----
 
----
[[category:CategoryDocumentation]]
 
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2010年5月19日 (三) 22:32的最新版本


What Is grep?

Grep is a command line tool that allows you to find a string in a file or stream. It can be used with Regular expression to be more flexible at finding strings.

How To Use grep

In the simplest case, grep can simply be invoked like this :

% grep 'STRING' filename

This is OK but it does not show the true power of grep. First this only looks at one file. A cool example of using grep with multiple file would be to find all files in a directory that contains the name of a person. This can be easily accomplished using a grep in the following way :

% grep 'Nicolas Kassis' *

Notice the use of single quotes; the quotes are not essential, but in this example they are required because the name contains a space. Double quotes could also have been used in this example. Now lets use some regular expressions...

Grep Regular Expression

grep can search for complicated patterns to find what you need. Here is a list of some of the special characters used to create a regular expression:

Grep Regular Expression
^ Denotes the beginning of a line
$ Denotes the end of a line
. Matches any one characters
* Matches 0 or more of the previous characters
.* Matches any number or type of characters
[] Matches on character for the one listed in the the Square brackets
[^] Does not match any characters listed
\ Denotes the beginning and end (respectively) of a word

So an example of a regular expression search would be

% grep "\<[A-Za-z].*" file

This will search for any word which begins with a letter upper or lower case. For more details check: