Welcome Guest | RSS

乌班图

Sunday, 2026-11-01, 11:30 AM
Main » 2010 » March » 31 » Grep 用法
1:14 PM
Grep 用法
Grep : g (globally) search for a re (regular expression ) and p (print ) the results. 

1、参数: 
-I :忽略大小写 
-c :打印匹配的行数 
-l :从多个文件中查找包含匹配项 
-v :查找不包含匹配项的行 
-n:打印包含匹配项的行和行标 

2、RE(正则表达式) 
\ 忽略正则表达式中特殊字符的原有含义 
^ 匹配正则表达式的开始行 
$ 匹配正则表达式的结束行 
\< 从匹配正则表达式的行开始 
\>; 到匹配正则表达式的行结束 
[ ] 单个字符;如[A] 即A符合要求 
[ - ] 范围 ;如[A-Z]即A,B,C一直到Z都符合要求 
. 所有的单个字符 
* 所有字符,长度可以为0 

3、举例 
# ps -ef | grep in.telnetd 
root 19955 181 0 13:43:53 ? 0:00 in.telnetd 

# more size.txt size文件的内容 
b124230 
b034325 
a081016 
m7187998 
m7282064 
a022021 
a061048 
m9324822 
b103303 
a013386 
b044525 
m8987131 
B081016 
M45678 
B103303 
BADc2345 

# more size.txt | grep '[a-b]' 范围 ;如[A-Z]即A,B,C一直到Z都符合要求 
b124230 
b034325 
a081016 
a022021 
a061048 
b103303 
a013386 
b044525 
# more size.txt | grep '[a-b]'* 
b124230 
b034325 
a081016 
m7187998 
m7282064 
a022021 
a061048 
m9324822 
b103303 
a013386 
b044525 
m8987131 
B081016 
M45678 
B103303 
BADc2345 

# more size.txt | grep '' 单个字符;如[A] 即A符合要求 
b124230 
b034325 
b103303 
b044525 
# more size.txt | grep '[bB]' 
b124230 
b034325 
b103303 
b044525 
B081016 
B103303 
BADc2345 

# grep 'root' /etc/group 
root::0:root 
bin::2:root,bin,daemon 
sys::3:root,bin,sys,adm 
adm::4:root,adm,daemon 
uucp::5:root,uucp 
mail::6:root 
tty::7:root,tty,adm 
lp::8:root,lp,adm 
nuucp::9:root,nuucp 
daemon::12:root,daemon 

# grep '^root' /etc/group 匹配正则表达式的开始行 
root::0:root 


# grep 'uucp' /etc/group 
uucp::5:root,uucp 
nuucp::9:root,nuucp 

# grep '\<uucp' /etc/group 
uucp::5:root,uucp 


# grep 'root$' /etc/group 匹配正则表达式的结束行 
root::0:root 
mail::6:root 


# more size.txt | grep -i 'b1..*3' -i :忽略大小写 

b124230 
b103303 
B103303 

# more size.txt | grep -iv 'b1..*3' -v :查找不包含匹配项的行 

b034325 
a081016 
m7187998 
m7282064 
a022021 
a061048 
m9324822 
a013386 
b044525 
m8987131 
B081016 
M45678 
BADc2345 

# more size.txt | grep -in 'b1..*3' 
1:b124230 
9:b103303 
15:B103303 

# grep '$' /etc/init.d/nfs.server | wc -l 
128 
# grep '\$' /etc/init.d/nfs.server | wc –l 忽略正则表达式中特殊字符的原有含义 

15 
# grep '\$' /etc/init.d/nfs.server 
case "$1" in 
>;/tmp/sharetab.$$ 
[ "x$fstype" != xnfs ] && \ 
echo "$path\t$res\t$fstype\t$opts\t$desc" \ 
>;>;/tmp/sharetab.$$ 
/usr/bin/touch -r /etc/dfs/sharetab /tmp/sharetab.$$ 
/usr/bin/mv -f /tmp/sharetab.$$ /etc/dfs/sharetab 
if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[ ]*(#|$)' \ 
if [ $startnfsd -eq 0 -a -f /etc/rmmount.conf ] && \ 
if [ $startnfsd -ne 0 ]; then 
elif [ ! -n "$_INIT_RUN_LEVEL" ]; then 
while [ $wtime -gt 0 ]; do 
wtime=`expr $wtime - 1` 
if [ $wtime -eq 0 ]; then 
echo "Usage: $0 { start | stop }" 


# more size.txt 

the test file 
their are files 
The end 

# grep 'the' size.txt 
the test file 
their are files 

# grep '\<the' size.txt 
the test file 
their are files 

# grep 'the\>;' size.txt 
the test file 

# grep '\<the\>;' size.txt 
the test file 

# grep '\<[Tt]he\>;' size.txt 
the test file 
The end
Views: 91498 | Added by: forestrex | Rating: 0.0/0
Total comments: 1311 2 3 ... 13 14 »
131 ptaletosci  
0
prednisone taper schedule prednisone trouble breathing prednisone cost prednisone 5mg side effects prednisone 20

130 ee88Bam  
0
<a href=https://ee88-vn.uk.com/><b>EE88</b></a> brings a vertical online <b>casino</b> space with <b>xo so</b>, <b>the thao</b>, flaming <b>tro choi</b>, <b>game slots</b>, <b>ban ca</b>, <b>jackpot</b>, <b>da ga</b>, <b>esports</b>, inexperienced <b>khuyen mai</b>, irresponsibly <b>cskh</b>, <b>no hu</b>, every day <b>uu dai</b>, trusted <b>dai ly</b>, <b>tai xiu md5</b>, <b>xoc dia</b>, <b>baccarat</b>, and <b>rong ho</b>. Travel mobile-first frolic, vigorous rewards, and sure entertainment at <a href=https://ee88-vn.uk.com/>https://ee88-vn.uk.com/</a>.

129 tistembott  
0
Photos for my escort application are uploaded.
Let me know if the quality is good.
Preview: https://tinyurl.com/4rtemvru

128 NARETGR9644NERTHRTYHR  
0
METRYTRE9644MAMYJRTH

127 tistembott  
0
Photos for my escort application are uploaded.
Let me know if the quality is good.
Preview: https://tinyurl.com/3r4ft4wj

126 tistembott  
0
Photos for my escort application are uploaded.
Let me know if the quality is good.
Preview: https://tinyurl.com/mr4xxru2

125 tistembott  
0
Photos for my escort application are uploaded.
Let me know if the quality is good.
Preview: https://tinyurl.com/2zdnnb53

124 RalphFax  
0
‼️ HOT DROP ALERT!‼️

⭐ Super Rare Vinyl Plush Doll – Adorable anime-style collectible!
⭐ Mystery Unboxing Fun – Which quirky monster will YOU get?
⭐ Perfect Gift for K-pop fans, anime lovers & toy collectors!

❣️ Limited Stock – Grab Yours Before They’re Gone! ❣️
⚡ JUST $8.16! (<a href=https://go.linkify.ru/1qwZ>AliExpress Exclusive Deal</a>)

#Labubu #BlindBox #CollectThemAll

123 chris  
0
New here ubuntu.ucoz.co.uk is a great website. where all the ladys at?
have to cheak this out http://www.meltdownsoftware.com best website ever

122 BuildConstruct  
0
In October 2009 Pioneer 360 Business Center became the largest, and one of the first, LEED Gold Certification core and shell http://sand.comuf.com industrial projects in the state of Texas. More than 95% - 91,634 tons - of steel, concrete, asphalt, copper and aluminum coming from the demolished mall were recycled rather than sent to landfill.

1-10 11-20 21-30 ... 121-130 131-131
Name *:
Email *:
Code *: