Documente Academic
Documente Profesional
Documente Cultură
Grep
y Searchesfortextinafile y Cansearchforsimplewords:chair y Canlookforregularexpressions;morecomplex
Whatcanitdo?
y Copynextinputlineintopatternspace y Applyregularexpressiontopatterspace y Copiesmatchestooutput
grepFamily
egrep
Allowsformorecomplexpatterns
fgrep
grep
Usesallregularexpressions Onlyonethatallowssaving
Commands
Command b c i n s v x ffile Description Precedeseachlinebythefileblock# Printsonlyacountofthenumberof linesmatchingthepattern Ignoresupper/lowercasematching Showsthelinenumbers SilentMode(no output) Inverse Output(linesthatdontmatch) Printsonlylinesthatentirelymatch Listofstrings tobematchedareina file
Searching
y grepcansearchforfileswiththesamenameina
directory y Findcommandlocatesthepathofafile
Demo_file
y $catdemo_file
THISLINEISTHE1STUPPERCASELINEINTHISFILE. thislineisthe1stlowercaselineinthisfile. ThisLineHasAllItsFirstCharacterOfTheWordWithUpper Case. Twolinesabovethislineisempty. Andthisisthelastline.
Grepusage
y grep something
somefile.txtreturnsalllines withthewordsomethingfromsomefile.txt
y grep vsomethingsomething.txtreturnsalllines
thatdon'thavethewordsomethinginthem
y grep i somethingsomething.txtreturnsall
lineswithamixedupperandlowercase somethinginthem.
Advancedregularexpressions
y Linesthathavecatfollowedbydogonthesameline,but
y .
matchasshownbelow. y Syntax:grepA<N>"string"FILENAME
y Thefollowingexampleprintsthematchedline,along
withthe3linesafterit.
y $grep A3i "example"demo_file
y Linesthatdonotmatch
y grep vabove"demo_text
Countingthenumberofmatchesusinggrep c
y y y y
Displayonlythefilenameswhichmatchesthegivenpatternusinggrep l
y
$grep lthisdemo_*
y y
demo_file demo_file1
Showthepositionofmatchintheline
y y
Syntax: grep ob"pattern"file $cattempfile.txt 12345 12345 $grep ob"3"tempfile.txt 2:3 8:3
y y y
y y y
Extended/EnhancedgrepwithmoreREmetacharacters
y treats+,?,|,(,and)asmetacharacters. y Inbasicregularexpressions(withgrep),themeta
egrep(grepE)
characters?,+,{,|,(,and)losetheirspecialmeaning.
y If youwantgrep totreatthesecharactersasmeta
characters,escapethem\?,\+,\{,\|,\(,and\)
y Characterswithspecialmeaningtotheshell($,*,[,|,
Extended/EnhancedgrepwithmoreREmetacharacters
egrep(grepE)
Uses
y SearchforSpecificCharacters y ThefollowingexamplesearchesforeitherJ,orN,orR.
$egrep [JNR]employee.txt 200JasonDeveloperTechnology$5,500 400Nisha ManagerMarketing$9,500 500RandyDBATechnology$6,000
SearchforaRange
y Thefollowingexamplesearchestherange69.i.e
Itsearchesfor6,or7,or8,or9.
$egrep [69]employee.txt 300SanjaySysadmin Technology$7,000 400Nisha ManagerMarketing$9,500 500RandyDBATechnology$6,000
egrep ORExample
y Pipesymbolisusedforegrep OR.Thefollowing
searchesforeitherMarketingorDBA
y countthenumberoflinesinemployee.txtwhich
eitherstartwith3orendwith500
egrep c^3|500$'employee.txt 200JasonDeveloperTechnology$5,500 300SanjaySysadmin Technology$7,000 400Nisha ManagerMarketing$9,500
sed
y Sed istheultimatestreameditor y sedcanbeusedtofiltertextfiles.Thepatterntomatchistypically
includedbetweenapairofslashes//andquoted. y ForEXAMPLE,toprintlinescontainingthestring"1024",youmay use: y catfilename|sedn'/1024/p y Here,sedfilterstheoutputfromthecatcommand.Theoption"n"tells sedtoblockalltheincominglinesbutthoseexplicitlymatchingthe expression.Thesedactiononamatchis"p"=print. y Thecharacterafterthes isthedelimiter.Itisconventionallyaslash, becausethisiswhated,more,andvi use.Itcanbeanythingyouwant, however.Ifyouwanttochangeapathnamethatcontainsaslash say /usr/local/binto/common/bin youcouldusethebackslashtoquote theslash: y sed's/\/usr\/local\/bin/\/common\/bin/'<old>new
y Acommonuseofsedistomodifyeachlineofafileor
Changeoccurances
y changealloccurrencesof6.30to7.30youcouldusethe
command:
y sed's/6.30/7.30/'songs.txt>songs2.txt
Filteringlines
y Sedisalsofrequentlyusedtofilterlinesinafileor
stream.Forexampleifyouonlywantseethelines containing"John"youcoulduse:
y sedn'/John/p'songs.txt>johns.txt
5,JohnnyTrash,Title482,Price$6.50 7,JohnLennon,Title271,Price$7.90
y echoSunday|sed's/day/light/' y Therearefourpartstothissubstitutecommand:
y s
y Thiswouldoutputtheword"Sunlight"becausesed
foundthestring"day"intheinput.
Parenthesizefirstcharacterofeachword
y Thissedexampleprintsthefirstcharacterofevery
wordinparenthesis.
$echo"WelcomeToTheGeekStuff3aaa"|sed's/\(\b[A Z]\)/\(\1\)/g' (W)elcome (T)o(T)he(G)eek(S)tuff
Commify thesimplenumber
$catnumbers 1234 12121 3434 123 $sed's/\(^\|[^09.]\)\([09]\+\)\([09]\{3\}\)/\1\2,\3/g'numbers 1,234 12,121 3,434 123
By:Yogesh Chaudhari