ubuntu_grep

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值