Saturday, July 27, 2024
HomeJava10 examples of grep command in UNIX and Linux

10 examples of grep command in UNIX and Linux


Hiya guys, if you wish to study the grep command in Linux and searching for some superior, hands-on, straightforward to make use of tutorial then you will have come to the proper place. Earlier, I’ve shared many examples based mostly tutorials to study important Linux instructions like discover, lsof, curl, sed, and chmod command and on this article, I’m going to share 10 examples of grep command in Linux. The grep command is likely one of the most often used UNIX command stands for “International Common Expression Print” together with discover, chmod, and tar command in UNIX. grep command in Unix working programs like Linux, Solaris, BSD, Ubuntu, or IBM AIX is used to go looking recordsdata for matching patterns.

By utilizing the grep command in Unix you’ll be able to search a file that accommodates a specific phrase or specific sample. UNIX grep command additionally offers a number of helpful command-line choices that can be utilized to reinforce the performance of the grep command.

This grep command tutorial shouldn’t be concerning the concept of UNIX grep however the sensible use of grep command in UNIX and right here I’m sharing my expertise on the usage of grep command in Linux with an purpose that this might function a fast information or tutorial for utilizing grep in UNIX for brand spanking new novices and assist them to know the grep command higher and its considerate utilization in UNIX or Linux.

Many individuals use grep only for discovering phrases in a file and missed the actual potential of grep by not utilizing all its highly effective command-line choices and its common expression functionality which couldn’t solely save quite a lot of time but in addition works as an amazing and highly effective software whereas analyzing a big set of information or log recordsdata.

By the way in which, in case you are new to Linux then I additionally recommend you undergo a complete Linux course to study some fundamentals instructions and fundamentals like Linux file system, permissions, and different basic items.

10 methods to make use of Grep command in Unix – examples

The next examples of grep command in UNIX are based mostly on my expertise and I take advantage of them every day in my work. Grep command can also be a part of any newbie’s UNIX command tutorial as it’s a vital command to study as a way to work effectively in any UNIX atmosphere like Redhat Linux, Ubuntu, IBM AIX, Oracle Solaris, or BSD.

Anyway, these examples are on no account full so please contribute you are personal grep command suggestions or how you’re utilizing grep in Linux to make it extra helpful and permit all of us to profit from one another’s expertise and work effectively in UNIX or Linux.

Instance 1: How you can ignore some phrases whereas doing a search utilizing grep in UNIX

Discovering related phrases and exclusion of irrelevant phrases. More often than not I search for Exception and Errors in log recordsdata and generally I do know sure Exception I can ignore so I take advantage of the grep -v choice to exclude these Exceptions

grep Exception logfile.txt | grep -v ERROR

This grep command instance will seek for the phrase “Exception” in logfile.txt and print them however since we have now piped out of the primary grep command to the second grep command which can exclude all strains which match the phrase “ERROR”

To make this grep instance extra concrete let’s have a look at one other instance, right here we have now a file which accommodates three strains as proven beneath:

$ cat instance.txt
UNIX working system
UNIX and Linux working system
Linux operation system

Now we need to search all strains in file instance.txt which accommodates the phrase UNIX however on the similar time would not comprise the phrase Linux.

$ grep UNIX instance.txt
UNIX working system
UNIX and Linux working system

Now to exclude all strains which comprise Linux we are going to apply one other grep command on this output with choice -v to exclude matching phrase as proven in beneath grep command:

$ grep UNIX instance.txt | grep -v Linux
UNIX working system

You possibly can see how helpful the grep -v command choice could be.

grep command examples in Linux

Instance 2: How you can depend the prevalence of a phrase in a file utilizing the grep command

If you wish to depend on a specific phrase within the log file you need to use the grep -c choice to depend the phrase. Beneath an instance of a command will print what number of instances the phrase “Error” has appeared in logfile.txt.

$ grep -c "Error" logfile.txt

If we apply this grep command on our instance file to search out what number of strains comprise phrase e.g. UNIX has occurred within the file:

$ grep -c UNIX instance.txt
2

Instance 3: printing strains earlier than and after of matching phrase utilizing grep

Typically we aren’t simply occupied with matching strains but in addition on strains round matching strains significantly helpful to see what occurs earlier than any Error or Exception. grep –context choice permits us to print strains round matching sample. 

Beneath an instance of grep command in UNIX will print 6 strains across the matching line of phrase “profitable” in logfile.txt

$ grep --context=6 profitable logfile.txt

Present a further six strains after matching very helpful to see what’s round and to print the entire message if it splits round a number of strains. You can too use command-line choice “C” as a substitute of “–context” for instance

$ grep -C 2 'hiya' *

Prints two strains of context round every matching line.

Instance 4: How you can search sample utilizing egrep and common expression

stands for prolonged grep and it’s extra highly effective than grep command in Unix and permits extra common expression like you need to use “|” choice to seek for both Error or Exception by executing only one command.

$ egrep 'Error|Exception' logfile.txt

Instance 5: How you can do case-insensitive looking out utilizing grep in Linux

If you wish to do a case insensitive search then use -i choice from the grep command in UNIX. grep -i command will discover any prevalence of each Error, error, and ERROR and fairly helpful to show any kind of Error from the log file.

$ grep -i Error logfile

Instance 6: How you can search patterns in gzip recordsdata utilizing the zgrep command

zgrep is one other nice model of grep command in Unix which is used to carry out the identical operation as grep does however with .gz recordsdata. Many instances we gzip the previous file to scale back the scale and later need to look or discover one thing on these recordsdata. zgrep is your man for these days. 

The beneath grep command will print all recordsdata which have “Error” on them.

$ zgrep -i Error *.gz

Instance 7: How you can do a recursive search in a listing utilizing grep in UNIX

If you wish to do a recursive search utilizing the grep command in Unix there are two choices both use “-R” command-line choice or enhance listing one after the other as proven beneath.

$ grep -R retailer *

This command will seek for a listing or file with the title saved within the present listing and it is all sub-directory. If you wish to study extra you too can see this recursive grep instance from my different article. 

Instance 8: UNIX command to show recordsdata names which comprise given phrase 

One other helpful grep command-line choice is “grep -l” which shows solely the file names which match the given sample. Beneath command will solely show file names which have ERROR?

$ grep -l ERROR *.log

grep -l ‘principal’ *.java will record the names of all Java recordsdata within the present listing whose contents point out `principal’.

Additionally, discover command in UNIX can be utilized instead of grep in lots of locations. 

Instance 9: grep command choice to show strains numbers

If you wish to see line variety of matching strains you need to use the choice “grep -n” beneath command will present on which strains Error has appeared.

$ grep -n ERROR log file.

Instance 10: How you can search the entire phrase in a file utilizing the grep command

You should use the grep -w command in UNIX to search out the entire phrase as a substitute of a simply sample, as proven within the following instance. This instance will solely print strains from logfile.txt which accommodates full phrase ERROR.

$ grep -w ERROR logfile.txt

Above grep command in UNIX searches just for situations of ‘ERROR’ which might be complete phrases; it doesn’t match `SysERROR’

For extra management, use `<‘ and `>’ to match the beginning and finish of phrases. For instance:

$ grep 'ERROR>' *

Searches just for phrases ending in ‘ERROR’, so it matches the phrase `SysERROR’.

Bonus GREP  Examples

Now I’ve two bonus examples of grep command in UNIX:

11) grep command in UNIX can present an identical sample in colour which is sort of helpful to focus on the matching part, to see matching sample in colour use beneath command.

$ grep Exception at this time.log --color

You can too create alias grep=’grep –color’ in your bash_profile file to keep away from typing –color each time.

12) There are three variations of grep command in UNIX  “grep,  fgrep, and egrep“. `fgrep’ stands for Fastened `grep‘, `egrep‘ Prolonged `grep’

And, here’s a good abstract of all of the grep instructions you will have realized thus far. You possibly can take the print out of this slide to maintain these grep instructions useful.

10 ways to use GREP command in UNIX

These examples of grep command in UNIX are one thing that I take advantage of every day; I’ve seen essentially the most subtle use of grep with a daily expression. I’ll record some extra examples of grep command in UNIX as I come throughout and discover it helpful to share.

As per my expertise having maintain on grep and UNIX discover command with information of normal expression can be nice in your daily life if it’s essential take a look at log recordsdata or config recordsdata or must do manufacturing assist on digital buying and selling programs or some other type of system which is operating on UNIX.

This record of grep command in UNIX is on no account full and I stay up for you guys sharing how you’re utilizing the grep command in UNIX.

Should you like this text, chances are you’ll discover the next UNIX tutorials additionally attention-grabbing:



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments