0% found this document useful (0 votes)
102 views11 pages

Grep Command Examples

linux grep command examples

Uploaded by

rockpramod09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
102 views11 pages

Grep Command Examples

linux grep command examples

Uploaded by

rockpramod09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
207 Using Gren amp; Regular Exprassions to Search for Text Patter in Linux | DigalOceen community Sew Using Grep & Regular Expressions to Search for Text Patterns in Linux Introduction ‘One ofthe most useful and versatile commands in a Linux terminal environment is the “grep” command. The name "grep" stands for “global regular expression print” This means that grep can be used to see ifthe input receives matches a specified pattern ‘This seemingly rival program is extremely powerful when used correctly ts ability to sort input based on complex rules makes ia popular Ink in mary command chains ‘ie wil explore some options and then dive into using regular exaressions. Allo the techniques discussed inthis guide an be apalied to ‘managing your VPS server Table of Contents ‘Basic Usage + Common Options * Literal Matches + Anchor Matches + Matching Any Charact + Bracket Expressions + Repeat Paster Zero or More Times + Escaping Mete-Characters * Grouping | + Blernation + Quon + Speciying Match Repetions 4. Conclusion Basic Usage Initssimpest form, grep can be used! to match Iterl patterns within a text fle, This means that if you ass grep a werd to search for, it wil rink ‘ut every ne inte file containing that word Lets ty an exemple, We wil use grep to sear for every ine that contains the word “GNU” in the GNU Ger Ubuntu system, 1 Public Lieense version 3 on an cd use/share/conwon- Licenses rep "ow" PL-2 ‘he GW Genera Public License is a free, copyleft License for ‘ne sll) General Public License is intended to guarantee your freedon to [AU General Public License for Rost of eur software; it applies also to Developers that use the GW GPL protect your rights with tio steps: ‘this License” refers to version 3 of the iW General Public License 43. Use with the iW Affero General Publis License, under version 3 of the cw Affero General Public License into a single itp: cgjtlocsan,comlcommuntyutcralsusing-grop-rogUlar expressions to-search-fr-txt-pattrns-irlnux am 2rot7 Using Grep Samp; Regular Expressions to Search for Text Patterns in Linux | DigilalOcean ‘The fist argument, "GNU" isthe pattern we are searching fr, while the second argument, “GPL-3" isthe input fle we wish to search ‘The resulting output willie every line containing the pattern text In some Linux dstbutions, the searched! for pattem wil be highlighted in the resulting tines ‘Common Options. By deraut, grea wil simply search fr the exact speefied pattern within the Input le ane return the lines it finds. We ean make ths Behevior mote useful though by adding some optional lags to grea ifwe would want grep to ignore the "ease" of our search parameter and search for ath upper. anc lower-case variations, we can specty the "or ~ignare-case” ston. Wie will search fr each instance of the word license" (with upper lower, or mixed cases) In the same fleas before, grep -1 “License” GPt-3 of this License dacunent, but changing {¢ 1s not allowed “he GNU General Public License Ls a free, copyleft License for ‘he Licenses for nost software and other practical works are designed ‘tne GWU General Public License 4s intended to quarantee your Fresdon to Gd General ABLic License for nost of our softaare) St applies also to price. Our General Public Licenses are designed to nake sure that you (2) assert copyright on the software, and (2) offer you this License “this License” refers to version 3 of the GW General Public Lacense ‘Tne Program” refers to any copyrightable work Licensed under this [As you can see, we have been given results that contain: "LICENSE’ "icense’, and License" I there was an instance with "LiCeNGE® that Would have been returned a5 well Irwe want to tne all ines that do not contain a specified pattern, we can use the -V of "invert match” option, ‘Wie can search for every ne that does nat contain the word "then the BSD license th the folowing command farep -v "the" ALL rights reserved fodistribution and use fn source and binary forms, with or without vethout specific prion written permission “HS SOFTWARE 1S PROVIDED BY THE REGENTS AND CONTRIBUTORS “°AS IS" AND [As you can See, because we Gid nat specity the "ignore case” option the ast Wo i ss were returned as not having the ward "he Is offen useful to know the tne number that the matches occur on, Thi ean be accomplished by Using the “not "ine-number” option, ‘The previous example with this lag added woule return the following text farep -vn "the" 850 2:AML rights reserved. itp: cgtalocean,comlcommurtyuteralsusing-grop-ragUlar-xpressions-to-search-fr-txt-pattrns-irlinux amt 2017 Using Gren amp; Regular Exprassions to Search for Text Patter in Linux | DigalOceen “inedsctribution and use An source and binary forms, with or without 13: may be used to endorse or pronote products derived fron this software 44: without specific prior written permission. 15: 17:aMy EXPRESS OR THPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE "Now we ean reference the line number ifwe want to make changes to every ine that does not contain “the Regular Expressions In the introduction, we statee that grep stands for “global regular expression print: A “regular expression’ isa text sting that eeseribes a particular search pattern, Different applications and programming languages implement regular expressions sightly diferent We wil only be exoloring a small subset ‘ofthe way thal grep describes its patterns. Literal Matches ‘The examales above, when we searched fr the wores "GNU" and "the", we were actualy searching for very simple regular expressions, whieh ‘matched the exact string of characters "GNU" and the” Lis helpfulto always think of these as matching @ string of characters rather than matching a Word, This willbecame a more important istnetion as we learn mere complex patterns. Patterns that exactly specty the characters to be matched are called "Mt als" because they match the pattern Iteraly character-forcharact Allalphabetic ane numerical characters fas well ascertain other characters) are matched Iteraly unless mosiiied by other expression mechanisms Anchor Matches ‘Anchors are special characters that specify wherein the line 2 match must occur to be val For instance, using anchors, we can speci that we only want to know about the lines that match "GNU" atthe very beginning ofthe tne. To {9 this, We could use the * anchor before the itera sting, ‘This sting example will only mach "GNU" it occurs at the very Beginning of ane aren “saw La {2 General Public License for nost of our software; 1¢ applies also to [AW General Public License, you nay choose any version ever published Similarly the" anchor ean be used after @ sting to incite thatthe match will nly be vais if occurs a he very ene ofa ine, ‘ie will match every line ending withthe wore “ane” inthe following regular expression rep “anas ca ‘nat there 5 no warranty for this free software. For both users* ora ‘he precise terns an conditions for copying, distribution and License. Each Licensee {8 addressed as "you". “Liconsees" snc receive it, in any nedlun, provided that you conspicuously and alternative 1s allowed only oeeasSorally and roncomereially, srs network nay be denied when the nodification itself netertally and versely affects the operation of the network or violates the rules ond provisionally, unless and until the copyright holder explicitly ane itp. cstlocear.comicommuntytcralsusing-grop-reguar-expressions-to-search-for-text patternsin-lnax 2rpot7 Using Grep Samp; Regular Expressions to Search for Text Patterns in Linux | DigilatOcean receives 2 License fron the original licensors, to run, modify arc rake, use, sell, offer for sale, Snport and otheruise run, nodity ond Matching Any Character ‘The period character ()e used in regular expressions to mean that any single character can exist atthe snecfed location, For example, we want to maten anything that has two characters and then the sing “cep, we could Use the felling pattern grep "cept" 671-3 se, which Ls precisely where {t is most unacceptable. Therefore, we fnfringenert under applicable copyright law, oxcopt executing St on 2 tells the user that there 1s no warranty for the work (except to the License by making exceptions fron one of gore of Ste conditions form of a separately written License, of stated a¢ exceptions; You aay not propagate or modify © covered wirk exce 9. acceptance Not Required for Having Copies. 5 expressiy [As you can see, we have instances of bath “accept” and “except” ang valatlons of the two words. The pattern would also have matched “y2eapt if that was foun as wel Bracket Expressions By placing » group of characters within brackets ([* ane 7), We can specify thatthe characte a that postion ean be any one character foun uth the bracket group. This means that fwe wanted! to tnd the les that contain Yoo” or "wo" we could specify those variations suclnetly by using the fllowing grep “t{wolo" orL-3 your prograns, too ‘reedone that you receives, You must make sure that they, too, receive Developers that use the GAU GPL protect your rights with two steps: 1 computer network, with no transfer of 9 copy, $f not conveying. systen Librartes, or general-purpose tools or generally available free Corresponding Source fron a netuork server at ne charge Wie can see tat bth variations are found within the fle Bracket notation alzo allows uz some interesting options. We can have the patter match anything except the characters within a bracket by beginning the list of characters within the brackets with 2" character ‘This example is tke the pattern “ode, but will nt match the pattern “code grep “[elode” 6-3 model, to give anyone wo possesses the abject cose elther (2) 2 ‘ne only skentficant nace of use of the product. ‘You will notice that itp: cgjtlocsan,comicommunityutcralusing-grop-roguar expressions to-search-fr-txt-pattrns-irlinux inthe second Ine returned, there isin fact, the word "code This snot afalure ofthe regular expression or grep. ant 217 Using Grop Samp; Regular Expressions to Search for Text Patterns in Linux | DigilalOcean Rathes this Ine was returned because eater in the Ine, the pattern ‘mode’ found within the word "medel”, was found, The ine was returned because there was an instance that matched the patter. ‘Another helpful feature of brackets is that yeu can specify a range af characters instead of individually yping every available characte. ‘This means that f we want to find every Ine that begins witha captal letter we can use the following pattern: grep “a[A-z)" 6-3 (AU General public License for nost of our softaare; Jt applies also to States should not allow patents to restrict developnert and use of License. Each Licensee 1s addressed as "you". "Licensees" and Conponent, and (@) serves only to enable use of the work with that jor Conponent, or to inplevent 2 Standard Interface for which an Systen Libraries, or general-purpose tools or generally avatlable free User Product 15 transferred to the rectpient Jn perpetuity or for a Due to some legacy zortng issues, its offen more accurate to use POSIX character clasces instead of character ranges like we just used, ‘There are many character classes that ae outside ofthe scope of ths guide, but an example that would accomplish the same procecure as ‘above uses the [upper character cass within a bracket selector: rep “AI Lsuppers]]* 6PL-3 MU General. Public License for most of our softwares i¢ applies also to States should not allou patents to restrict development and use of License. Eaeh Licensee {8 addressed as "you". "Licensees" and Conpenent, and (0) serves only to enable use of the work with that afor Conponent, or to inplevent a Standard Interface for which an systen Libraries, or general-purpose tools or generally available free User Product 4s transferred to the recipient in perpetuity or for 3 Repeat Pattern Zero or More Times Finally, one ofthe most commonly used meta-characters isthe ™ whieh means "repeat the previous character of expression Zero oF more umes" Ir we wantea!to tnd each tine that contained an opening and closing parenthesis, with only eters and single spaces in between, we could use the following expression grap “((Aeza-z J*)" @PL-3 Copyright (©) 2007 Free Software Foundation, Ine ‘istesbution (with on without nods‘seation), making avatlable to the than the work a8 a whole, that (a) J Included in the normal fora of Corponent, ard (b) serves only to enable use of the work with that (4? any) on wach the executable work runs, or a compiler used to (including a physical distribution nediun), accompanied by the (Gnelusing a physical ekstrsbution nediun), acconpanied by place (gratis or for» enarge), Escaping Meta-Characters itp: cgjtlocsan,comicommuntyutcralusing-grop-rgUlar expressions to-search-fr-taxt-pattrns-irlnux si 207 Using Gren amp; Regular Exprassions to Search for Text Patter in Linx | DigalOceen Sometimes, we may want to search for a literal period ora Iteral opening bracket. Because these characters have special meaning in regular ‘expressions, we need to "escape" these characters to tll grep that we do net wish te use thelr special meaning inthis case We can escape characters by using the backslash character betere the character that would nermally have 2 special meaning For instance, we want to fne any line that begins with a capital eter and ends vith a peiod, we could use the fllowng expression. The ‘encing peti is escaped so that represents a itera petiod instead of the usual “any character meaning rep “*UACZ]. A$" PLB License would be to refrain entinely from conveying the Progran Extended Regular Expressions Grep can be used with an even mote extensive regular expression language by using the "-" flag or by caling the Yegrep” command instead, of rep, These options open up the capabilites of extended regular expressions" Exiended regular expressions include allot the basle meto- ‘enaracters, along with adtional meta-characters to express more complex matenes. Grouping ‘One of the easiest and most useful abilties that extended regular expressions open up isthe abilty to group expressions together to ‘manipulate of reference as one unit ‘Group expressions together using parentheses. you woul’ ke fo use parentheses without using extended regular expressions, you can ‘escape them with the backslash to enable tis funtionatiy arep “\(grouping\)" fite.txt grep -E “(grouping)” fite.txt carey “(grouping)” fe.txt ‘The above thiee expressions ate functionally equivalent Alteration ‘Similar to how bracket expressions can spelt siferent possible cholees fr single character matches, alternation allows you te specify alternate matehes for stings oF exoression sets Te Indicate alternation, we use the pige character“ These ate aften used within parenthetical grouping te specty that ane of to or mere possiblities should be considered a maten, ‘The following wil ind ether “GPL ar “General Public License” nthe text rep -£ “(PL |cenerai public License)” 6PL-3 ‘he GI General Public License ts a free, copyleft License for ‘tne GW General Public License is intenéed to guarantee your freedon to (WU General Public License foe most of our software; 1t applies also to price. Our Gonerai Public Licensee are designed to nake sure that you itp. cstlocear.comicommuntytcralsusing-grop-reguar-expressions-to-search-for-text patternsin-lnax en 2017 Using Gren amp; Regular Exprassions to Search for Text Patter in Linux | DigalOceen Developers that use the GW GPL protect your rights with two steps For the developers” and authors” protection, the GPL elearly explains ‘authors’ sake, the GPL reguires that modified versions be narked 25 have designed this version of the GPL to prohibit the practice for those ‘Alternation can select between more than two choices by acing addtional choices within the selection group separated by adultonal pipe ccnaracters Quantifiers Like the "" metaccharacter, that matched the previous character ar character set zero or more times, there are other meta-characters available In extended regular expressions that specity the number of occurrences: ‘To match a character zero or one times, you can use the °" character. This makes character or character zet that came before optional, in ‘The following matches “copyright” and "ight by putting "copy" in an optional grou: arep -E “(copy)?right” 6PL-3 Copycspht (€) 2067 Free Software Foundstion, Inc. To protect your rights, we need to prevent others fram denying you ‘ese rights or asking you to surrender the cights. Therefore, you have know their rignts. Developers that use the GW GPL protect your rights with two steps: (a) assert copyright on the software, and (2) offer you this License copyrehe" also means copyright-1ike laws that apply to other kinds of ‘The "character matches an expression one or more times. Tiss almost ike the ™* meta-characte, but withthe "= character, the ‘expression must match at least once. ‘The following expression matches the sting "ree" plus one or more characters that ae not whitespace: arep -€ “free[*{sspace:]"" @L-3 ‘ne GWU General Public License {= a free, copyleft License for ‘to take away your Freedon to share and change the works. ay contrast, ‘ne GU General Public License 4s Sntenced to quarantee your éovecon te nen we speak of free software, we are referring to ‘reedoe, not have the ‘reson to distribute copies of free software (and charge for you nocsfy it: responsibilities to respect the foeedon of others. ‘reedons that you received. You must make sure that they, too, receive protecting users’ ‘reecon to change the software, The systenatic Cf the GPL, a5 needed to protect the feeder of users patents cannot be used to render the progran non-free Specifying Match Repetition W-ign up for our newsletter. > 5 Got te ites tuterils on SysAdmin and open source topes. rep -€“(Aetaneteu](y" 3 cenanged, 20 that their problems will not be attributed erronsously £0 authors of previous versions. itp: cgjtalocean,comlcommurtytcralssing-grop-roglar-xpressions-to-search-fr-txt-pattrns-irlnux ™m 2017 Using Grop Samp; Regular Expressions to Search for Text Patterns in Linux | DiglalOcean receive it, in any nediun, provided that you conspicuoisly and ive under the previous paragraph, plus a right to possession of the covered work so as to satisfy sinultancously your obligations under this Irwe want to mateh any words that have between 16 and 20 characters, we ean use the following expression: rep -€ *((salpha:)}(26,20)" 3 certain responsibilities if you distribute copies of the softmare, or iF you nodity i: responsibilities to respect the freedon of others. ©) Prohibiting nisrepresentation of the origin of thit asterial, or Conclusion There are many times when grep wile usetulinfneing patterns within les oF within the fe system hlearehy, Lis worthile to become {aril with its options andl syntax te save yourself time wen you need Regula: expressions are even mote versal, and ean be Used with many pepular programs, For instance, many text eiters implement regular ‘expressions for searching and replacing text Furthermore, mast modem programming languages use regular expressions to perform procedures on specie pieces of data, Regular ‘expressions are ask that willbe transferrable to many common computer-elated tasks By Justin Elingwaod Elingwood ‘Uprete unsere What's the (big) deal with big data? Cee ee ey Peecirntis PD rant kena monet START HERE-READ TUTORIAL Related Tutorials {An Introduction to Useful Bash Aliases tions Sign up for our newsletter Get the latest tutorials on SysAdmin and open source topics. itp. cstlocean.comicommuntytoralsusing-grop-reguar -expressions-to-search-fo-text patternsin-lnx ant 207 Using Gren amp; Regular Exprassions to Search for Text Patter in Linx | DigalOceen | mostly need for searching snippets of code. Thanks forthe writeup! Groat arte Great tle thanke! 1 The flowing expression matenes the sting “re” plus ane at more chatacters that are nel whitespoce: grep -E "éree[[:space:]]#7 GPL-3 cea [sblank:]}4" GPL-3 Hi. neeea que aster to this question, Ihave the folowing two sing where i search for 20%4107(0ate son ster 50: & charactors ater st, ant Irom late it shoul be ignere. simple wore beeve there is ne match fer 20181107 in below two sting Fspeszortozoweo7s2i86coR (5-6 (12](0-9](3) (211-9 la[0-2))((6-2310-9]1(01])." ‘Should give you wnat you're looking for Here's an explanation 5-6) + atthe beginning ofthe ine, took or 5-6 of any character (22]{@-9]43} {forthe year) look for this format the number te 2 followed by 3 numbers ftom 0-9. (@(2-9]12{8-2]) + {lor the month look fr either 0 folowed by a number Inthe range 9 OR » followed by a number in the range 0-2 ({@-2){8= spiater)) the Sign up for our newsletter Get the latest tutorials on SysAdmin and open source topes. ‘he ysl ler tm very grateful te this atte itp. ctl ocear.com/communtytoralsusing-grop-reguar-expressions-to-searct-fo-text patternsinlnax on 2rot7 Using Grep Samp; Regular Expressions to Search for Text Patterns in Linux | DigilalOcean funtion snyfurnametanyeramorearg_K ) function bwveet.tt. atk funtion ace ) thanks | mesuy need it fr searching special enataters can you give me te command ? Nice Ail £90 we ad! this t90? Usage &: Search for multilepatters for single word forep~flewithpatternsixt fletosearch tt Nowmally we seach for ony one pattern Sut with fgrep we can give multiple pelle fr searching sing Usage 8: Sear for Sting in Zp Fes 2616p warning Narilogllogs ge I tinwewte 2p the tes with extension as “g2"We can seatch in Zp le by using above commane Sign up for our newsletter Get te latest tutorials on SysAdmin and open source topes. hits: cgtlocean,comleommurtyutoralssing-grop-rgUla-expressons-to-searc-fr-txt-pattrns-irlinux 2rot7 Using Grep Samp; Regular Expressions to Search for Text Palle in Linux | DigilatOcean Sign up for our newsletter Get te latest tutorials on SysAdmin and open source topes. itp: cgjtlocsan,comicommunityutcralusing-grop-roguar expressions to-search-fr-txt-pattrns-irlinux wn

You might also like