You are on page 1of 4

Bash Gude for Begnners

Irev Chapter 4. IeguIar expressIons ext


4.2. ExampIes usng grep
4.2.1. What s grep?
grep searches the Input LIes Ior IInes contaInIng a match to a gIven pattern IIst.
When It Lnds a match In a IIne, It copIes the IIne to standard output (by deIauIt), or
whatever other sort oI output you have requested wIth optIons.
Though grep expects to do the matchIng on text, It has no IImIts on Input IIne Iength
other than avaIIabIe memory, and It can match arbItrary characters wIthIn a IIne. I
the LnaI byte oI an Input LIe Is not a newline, grep sIIentIy suppIIes one. SInce
newIIne Is aIso a separator Ior the IIst oI patterns, there Is no way to match newIIne
characters In a text.
Some exampIes.
ca1hy -> grep roo1 /e1c/passWd
Joo1.x.0.0.Joo1./Joo1./b1h/bash
opeJa1oJ.x.11.0.opeJa1oJ./Joo1./sb1h/hoog1h
ca1hy -> grep -n roo1 /e1c/passWd
1.Joo1.x.0.0.Joo1./Joo1./b1h/bash
12.opeJa1oJ.x.11.0.opeJa1oJ./Joo1./sb1h/hoog1h
ca1hy -> grep -v bash /e1c/passWd ] grep -v no!ogzn
syhc.x.5.0.syhc./sb1h./b1h/syhc
shu1dowh.x.6.0.shu1dowh./sb1h./sb1h/shu1dowh
ha1.x.7.0.ha1./sb1h./sb1h/ha1
hews.x.9.13.hews./vaJ/spoo/hews.
ma1hu.x.47.47../vaJ/spoo/mqueue./dev/hu
x1s.x.43.43.X Foh1 SeJveJ./e1c/X11/1s./b1h/1ase
Jpc.x.32.32.PoJ1mappeJ RPC useJ././b1h/1ase
hscd.x.28.28.hSC0 0aemoh././b1h/1ase
hamed.x.25.25.hamed./vaJ/hamed./b1h/1ase
squ1d.x.23.23../vaJ/spoo/squ1d./dev/hu
dap.x.55.55.L0AP bseJ./vaJ/1b/dap./b1h/1ase
apache.x.48.48.Apache./vaJ/www./b1h/1ase
ca1hy -> grep -c 1a!se /e1c/passWd
7
ca1hy -> grep - s -/.bash* ] grep -v hzs1ory
/home/ca1hy/.bashJc.PS1="\|\033|1,44m\]$bSER 1s 1h \w\|\033|0m\] "
WIth the Lrst command, user ccthy dIspIays the IInes Irom /e1c/passwd contaInIng the
strIng root.
xampIes usIng grep http.//tIdp.org/II/ash-egInners-GuIde/htmI/sect_04_...
1 de 4 19-04-2012 15.23
Then she dIspIays the IIne numbers contaInIng thIs search strIng.
WIth the thIrd command she checks whIch users are not usIng bash, but accounts
wIth the noIogn sheII are not dIspIayed.
Then she counts the number oI accounts that have /b1h/1ase as the sheII.
The Iast command dIspIays the IInes Irom aII the LIes In her home dIrectory startIng
wIth -/.bash, excIudIng matches contaInIng the strIng history, so as to excIude matches
Irom -/.bash_h1s1oJy whIch mIght contaIn the same strIng, In upper or Iower cases.
ote that the search Is Ior the string "ps", and not Ior the commcnd ps.
ow Iet's see what eIse we can do wIth grep, usIng reguIar expressIons.
4.2.2. Grep and reguIar expressons
1f you are not on 1nux
We use G! grep In these exampIes, whIch supports extended reguIar
expressIons. G! grep Is the deIauIt on IInux systems. I you are workIng on
proprIetary systems, check wIth the -v optIon whIch versIon you are usIng. G!
grep can be downIoaded Irom http.//gnu.org/dIrectory/.
4.2.2.1. 1ne and word anchors
Irom the prevIous exampIe, we now excIusIveIy want to dIspIay IInes startIng wIth the
strIng "root".
ca1hy -> grep "roo1 /e1c/passWd
Joo1.x.0.0.Joo1./Joo1./b1h/bash
I we want to see whIch accounts have no sheII assIgned whatsoever, we search Ior
IInes endIng In ".".
ca1hy -> grep :$ /e1c/passWd
hews.x.9.13.hews./vaJ/spoo/hews.
To check that PATh Is exported In -/.bashJc, Lrst seIect "export" IInes and then search
Ior IInes startIng wIth the strIng "IATI", so as not to dIspIay hAhPATh and other possIbIe
paths.
ca1hy -> grep exor1 -/.bashrc ] grep `\<P4Th`
expoJ1 PATh="/b1h./usJ/1b/mh./1b./usJ/b1h./usJ/oca/b1h./usJ/ucb./usJ/db1h.$PATh"
SImIIarIy, \> matches the end oI a word.
xampIes usIng grep http.//tIdp.org/II/ash-egInners-GuIde/htmI/sect_04_...
2 de 4 19-04-2012 15.23
I you want to Lnd a strIng that Is a separate word (encIosed by spaces), It Is better
use the -w, as In thIs exampIe where we are dIspIayIng InIormatIon Ior the root
partItIon.
ca1hy -> grep -W / /e1c/1s1ab
LABEL=/ / ex13 de1au1s 1 1
I thIs optIon Is not used, aII the IInes Irom the LIe system tabIe wIII be dIspIayed.
4.2.2.2. Character cIasses
A brccket expression Is a IIst oI characters encIosed by "|" and "|". t matches any
sIngIe character In that IIst, II the Lrst character oI the IIst Is the caret, "", then It
matches any character OT In the IIst. Ior exampIe, the reguIar expressIon
"|0123456789|" matches any sIngIe dIgIt.
WIthIn a bracket expressIon, a rcnge expression consIsts oI two characters separated
by a hyphen. t matches any sIngIe character that sorts between the two characters,
IncIusIve, usIng the IocaIe's coIIatIng sequence and character set. Ior exampIe, In the
deIauIt C IocaIe, "|a-d|" Is equIvaIent to "|abcd|". Many IocaIes sort characters In
dIctIonary order, and In these IocaIes "|a-d|" Is typIcaIIy not equIvaIent to "|abcd|", It
mIght be equIvaIent to "|abCcd|", Ior exampIe. To obtaIn the tradItIonaI
InterpretatIon oI bracket expressIons, you can use the C IocaIe by settIng the LC_ALL
envIronment varIabIe to the vaIue "C".
IInaIIy, certaIn named cIasses oI characters are predeLned wIthIn bracket
expressIons. See the grep man or InIo pages Ior more InIormatIon about these
predeLned expressIons.
ca1hy -> grep ]y1] /e1c/group
sys.x.3.Joo1,b1h,adm
11y.x.5.
ma1.x.12.ma1,pos111x
11p.x.50.
hobody.x.99.
1oppy.x.19.
x1s.x.43.
h1shobody.x.65534.
pos111x.x.89.
n the exampIe, aII the IInes contaInIng eIther a "y" or "I" character are dIspIayed.
4.2.2.3. WIdcards
!se the "." Ior a sIngIe character match. I you want to get a IIst oI aII Lve-character
ngIIsh dIctIonary words startIng wIth "c" and endIng In "h" (handy Ior soIvIng
crosswords).
xampIes usIng grep http.//tIdp.org/II/ash-egInners-GuIde/htmI/sect_04_...
3 de 4 19-04-2012 15.23
ca1hy -> grep `\<c...h\>` /usr/share/dc1/Words
ca1ch
cash
co1h
coach
couch
cough
cJash
cJush
I you want to dIspIay IInes contaInIng the IIteraI dot character, use the -F optIon to
grep.
Ior matchIng muItIpIe characters, use the asterIsk. ThIs exampIe seIects aII words
startIng wIth "c" and endIng In "h" Irom the system's dIctIonary.
ca1hy -> grep `\<c.*h\>` /usr/share/dc1/Words
ca1ph
cash
ca1ch
cheeseco1h
chee1ah
--ou1pu1 om111ed--
I you want to Lnd the IIteraI asterIsk character In a LIe or output, use sIngIe quotes.
Cathy In the exampIe beIow Lrst trIes LndIng the asterIsk character In /e1c/pJo11e
wIthout usIng quotes, whIch does not return any IInes. !sIng quotes, output Is
generated.
ca1hy -> grep * /e1c/pro11e
ca1hy -> grep `*` /e1c/pro11e
1oJ 1 1h /e1c/pJo11e.d/*.sh , do
Irev Iome ext
IeguIar expressIons !p Iattern matchIng usIng
ash Ieatures
xampIes usIng grep http.//tIdp.org/II/ash-egInners-GuIde/htmI/sect_04_...
4 de 4 19-04-2012 15.23

You might also like