个人工具

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

来自Ubuntu中文

跳转至: 导航, 搜索
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/grep}}
 
{{From|https://help.ubuntu.com/community/grep}}
 
{{Languages|UbuntuHelp:grep}}
 
{{Languages|UbuntuHelp:grep}}
== What Is grep? ==
+
== What Is grep? (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.
 
'''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'''是一个用来从文件或数据流中查找字符串的命令行工具,通过结合正则表达式'''Grep'''亦能获得更加灵活的匹配效果。
<pre><nowiki>
+
 
% grep 'STRING' filename
+
=== How To Use grep (如何使用Grep) ===
</nowiki></pre>
+
 
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 :
+
In the simplest case, grep can simply be invoked like this&nbsp;:
<pre><nowiki>
+
 
% grep 'Nicolas Kassis' *
+
举一个最简单的Grep使用例子:
</nowiki></pre>
+
<pre>% grep 'STRING' filename</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.
+
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&nbsp;:
Now lets use some regular expressions...
+
 
 +
我们可以使用Grep来简单地从一个文件中查找一个特定的字符串,但这仅仅最简单的功能而已。
 +
<pre>% grep 'Nicolas Kassis' *</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. 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
+
 
some of the special characters used to create a regular expression:
+
grep can search for complicated pattern to find what you need. Here is a list of some of the special characters used to create a regular expression:
{|border="1" cellspacing="0"
+
 
|||||'''Grep Regular Expression'''
+
{| cellspacing="0" border="1"
 
|-
 
|-
| ^ || Denotes the beginning of a line
+
|  
 +
|  
 +
| '''Grep Regular Expression'''
 
|-
 
|-
| $ || Denotes the end of a line  
+
| ^
 +
| Denotes the beginning of a line
 
|-
 
|-
| . || Matches any one characters
+
| $
 +
| Denotes the end of a line
 
|-
 
|-
| * || Matches 0 or more of the previous characters  
+
| .
 +
| Matches any one characters
 
|-
 
|-
| .* || Matches any number or type of characters  
+
| *
 +
| Matches 0 or more of the previous characters
 
|-
 
|-
| [] || Matches on character for the one listed in the the Square brackets
+
| .*
 +
| Matches any number or type of characters
 
|-
 
|-
| [^] || Does not match any characters listed  
+
| []
 +
| Matches on character for the one listed in the the Square brackets
 
|-
 
|-
| \/ || Denotes the beginning and end (respectively) of a word
+
| [^]
 +
| 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
 
So an example of a regular expression search would be
<pre><nowiki>
+
<pre>% grep "\&lt;[A-Za-z].*" file</pre>
% grep "\<[A-Za-z].*" file
+
This will search for any word which begins with a letter upper or lower case. For more details check:
</nowiki></pre>
+
 
This will search for any word which begins with a letter upper or lower case.
+
For more details check:
+
 
* [[UbuntuHelp:BasicCommands|BasicCommands]]
 
* [[UbuntuHelp:BasicCommands|BasicCommands]]
 
* http://www.gnu.org/software/grep/doc/
 
* http://www.gnu.org/software/grep/doc/
 
* 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:CategoryDocumentation]] [[Category:UbuntuHelp]]

2008年4月7日 (一) 01:12的版本


What Is grep? (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.

Grep是一个用来从文件或数据流中查找字符串的命令行工具,通过结合正则表达式Grep亦能获得更加灵活的匹配效果。

How To Use grep (如何使用Grep)

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

举一个最简单的Grep使用例子:

% 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来简单地从一个文件中查找一个特定的字符串,但这仅仅最简单的功能而已。

% grep 'Nicolas Kassis' *

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. Now lets use some regular expressions...

Grep Regular Expression

grep can search for complicated pattern 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: