16 grep Command Examples to Help You in Real-World

grep, initially developed for Unix-based methods, is among the mostly used command line utilities in Linux bins.

The title comes from one other comparable command in ed software specifically g/re/p which stands for Gsearch domestically for a concerningbeneficiant expression and Pprint matching strains. grep really appears to be like for a selected sample or common expression from normal enter or file and prints the strains that meet the given standards. It’s typically used to filter out pointless particulars whereas printing solely the required data from massive log information.

The ability of normal expression, mixed with supported choices in grep, makes this attainable.

Right here we are going to talk about among the generally used grep instructions in several eventualities by sysadmin or developer.

So let’s get began…👨‍💻

grep command syntax

grep command expects a sample and non-obligatory arguments together with a file checklist if used with out piping.

$ grep [options] sample [files]

A easy instance is:

$ grep my file.txt
my_file
$

Discover a number of information

With grep you’ll be able to seek for the given sample not solely in a single however in a number of information. For instance, you’ll be able to seek for a sample in a number of information utilizing * wildcard.

$ sudo grep -i err /var/log/messages*

Exit:

$ sudo grep err /var/log/messages*
/var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: Utilizing IOAPIC for interrupt routing
/var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKA] (IRQs 5 9 10 *11)
/var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKB] (IRQs 5 9 *10 11)
/var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKC] (IRQs 5 *9 10 11)
/var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKD] (IRQs 5 9 10 *11)
/var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: Utilizing IOAPIC for interrupt routing
/var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKA] (IRQs 5 9 10 *11)
/var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKB] (IRQs 5 9 *10 11)
/var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKC] (IRQs 5 *9 10 11)
/var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKD] (IRQs 5 9 10 *11)
/var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: BERT: Boot Error File Desk assist is disabled. Allow it through the use of bert_enable as kernel parameter.
/var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKA] (IRQs 5 9 10 *11)
/var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKB] (IRQs 5 9 *10 11)
/var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKC] (IRQs 5 *9 10 11)
/var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Hyperlink [LNKD] (IRQs 5 9 10 *11)
/var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: BERT: Boot Error File Desk assist is disabled. Allow it through the use of bert_enable as kernel parameter.
/var/log/messages-20201227:Dec 27 19:11:21 centos7vm kernel: [drm:vmw_host_log [vmwgfx]] *ERROR* Didn't ship host log message.
/var/log/messages-20201227:Dec 27 19:11:21 centos7vm kernel: [drm:vmw_host_log [vmwgfx]] *ERROR* Didn't ship host log message.
$

You’ll be able to see from the above output that the file title is printed first earlier than the matching line is printed to point the place grep discovered the given sample.

Case insensitive search

grep gives to seek for a sample with out trying on the capital letter of the sample. Utilization -i flag to inform grep to disregard capitalization.

$ grep -i [pattern] [file]

Exit:

$ grep -i it text_file.txt
It is a pattern textual content file. It accommodates
performance. You'll be able to at all times use grep with any
sort of knowledge but it surely works greatest with textual content knowledge.
It helps numbers like 1, 2, 3 and so forth. in addition to
It is a pattern textual content file. It is repeated two instances.
$

Search by complete phrases

We do not at all times need a partial match, however as a substitute anticipate grep to match solely a full phrase. You are able to do that with -w flag.

$ grep -w [pattern] [file]

Exit:

$ grep -w is text_file.txt
It is a pattern textual content file. It accommodates
It is a pattern textual content file. It is repeated two instances.
$

Examine the variety of matches

Typically, as a substitute of the road really matched, we simply want the variety of profitable matches that grep made. We are able to use this rely -c selection.

$ grep -c [pattern] [file]

Exit:

$ grep -c is text_file.txt
2
$

Discover subfolders

It’s typically essential to go looking information not solely within the present workbook, but additionally in subfolders. With grep you are able to do that simply -r flag.

$ grep -r [pattern] *

Exit:

$ grep -r Hey *
dir1/file1.txt:Hey One
dir1/file2.txt:Hey Two
dir1/file3.txt:Hey Three
$

As you’ll be able to see, grep goes by means of every subdirectory inside a present listing and lists the information and features the place a match was discovered.

Reverse search

If you wish to discover one thing that does not match a sure sample, grep helps you to just do that -v flag.

$ grep -v [pattern] [file]

Exit:

$ grep This text_file.txt
It is a pattern textual content file. It accommodates
It is a pattern textual content file. It is repeated two instances.
$ grep -v This text_file.txt
a number of strains for use as a part of testing grep
performance. You'll be able to at all times use grep with any
sort of knowledge but it surely works greatest with textual content knowledge.
It helps numbers like 1, 2, 3 and so forth. in addition to
alphabets and particular characters like - + * # and so forth.
$

You’ll be able to examine the output of grep command on the identical sample and file with and with out -v flag. Of -vwhich strains don’t match the sample are printed.

Print line numbers

grep lets you print line numbers together with printed strains, making it simple to know the place the road is within the file. Utilization -n possibility as proven to get line numbers within the output.

$ grep -n [pattern] [file]

Exit:

$ grep -n This text_file.txt
1:It is a pattern textual content file. It accommodates
7:It is a pattern textual content file. It is repeated two instances.
$

Restrict grep output

For big information like logs and so forth the grep output will be lengthy and chances are you’ll solely want a set variety of strains within the output somewhat than every thing matching. We are able to use –m[num] to restrict the variety of printed strains by quantity. This is tips on how to use it:

$ grep -m[num] [pattern] [file]

Be aware tips on how to use -m flag impacts grep output for a similar set of circumstances within the instance beneath:

$ grep It text_file.txt
It is a pattern textual content file. It accommodates
It helps numbers like 1, 2, 3 and so forth. in addition to
It is a pattern textual content file. It is repeated two instances.
$ grep -m2 It text_file.txt
It is a pattern textual content file. It accommodates
It helps numbers like 1, 2, 3 and so forth. in addition to
$

Show further strains

Usually we’d like not solely the strains which have an identical sample, but additionally some strains above or beneath them for higher context.

It’s attainable to print a line above or beneath (or each) a line with a sample utilizing grep through the use of -A, -B or -C flags alongside num worth. Right here num specifies the variety of further strains to print which are simply above or beneath the matching line. This is applicable to all matches that grep finds within the specified file or file checklist.

$ grep -A[num] [pattern] [file]

OR

$ grep -B[num] [pattern] [file]

OR

$ grep -C[num] [pattern] [file]

The output beneath exhibits a standard grep output and a flagged output -A, -B And -C one after the other. Discover how grep interprets the flags and their values ​​and the modifications within the respective output. Of -A1 flag, grep prints 1 line simply after the matching line.

Likewise with -B1 flag, 1 line is printed simply earlier than the matching line. Of -C1 flag, 1 line is printed earlier than and after the matching line.

$ grep numbers text_file.txt
It helps numbers like 1, 2, 3 and so forth. in addition to
$ grep -A1 numbers text_file.txt
It helps numbers like 1, 2, 3 and so forth. in addition to
alphabets and particular characters like - + * # and so forth.
$ grep -B1 numbers text_file.txt
sort of knowledge but it surely works greatest with textual content knowledge.
It helps numbers like 1, 2, 3 and so forth. in addition to
$ grep -C1 numbers text_file.txt
sort of knowledge but it surely works greatest with textual content knowledge.
It helps numbers like 1, 2, 3 and so forth. in addition to
alphabets and particular characters like - + * # and so forth.
$

Checklist of file names

To print solely the title of the information during which a sample is discovered as a substitute of truly matching strains, use -l flag.

$ grep -l [pattern] [file]

This is an instance run:

$ grep -l su *.txt
file.txt
text_file.txt
$

Print actual strains

Typically we have to print strains that precisely match a sure sample, not a part of it. grep permits it -x flag to just do that.

$ grep -x [pattern] [file]

Within the instance beneath, file.txt accommodates a line with only one phrase “assist” and as such is matched by grep with -x flag whereas ignoring strains that will include the phrases “assist” together with different textual content.

$ grep -x assist *.txt
file.txt:assist
$ 

Match the beginning sequence

Utilizing common expressions we are able to discover a string at first of a line. This is tips on how to do it.

$ grep [options] "^[string]" [file]

Instance:

$ grep It text_file.txt
It is a pattern textual content file. It accommodates
It helps numbers like 1, 2, 3 and so forth. in addition to
It is a pattern textual content file. It is repeated two instances.
$ grep ^It text_file.txt
It helps numbers like 1, 2, 3 and so forth. in addition to
$

Watch how you employ ^ character modifications the output. ^ signifies the start of the string and grep ^It like every line that begins with the phrase It. It may be helpful to place in quotes if the sample accommodates areas, and so forth.

Match ending sequence

One other frequent common expression is matching the top of the road sample.

$ grep [options] "[string]$" [file]

Instance:

$ grep "." text_file.txt
It is a pattern textual content file. It accommodates
performance. You'll be able to at all times use grep with any
sort of knowledge but it surely works greatest with textual content knowledge.
It helps numbers like 1, 2, 3 and so forth. in addition to
alphabets and particular characters like - + * # and so forth.
It is a pattern textual content file. It is repeated two instances.
$ grep ".$" text_file.txt
sort of knowledge but it surely works greatest with textual content knowledge.
alphabets and particular characters like - + * # and so forth.
It is a pattern textual content file. It is repeated two instances.
$

We tried to make a match with a . signal on the finish of the road. Since dot (.) is a personality with a particular that means, we have to escape it character. Discover once more how the output varies if we simply match . character and after we use it $ to instruct grep to match solely strains that finish with . (not those that may include it someplace in between).

Use sample file

There could also be conditions the place you’ve a posh checklist of patterns that you just use regularly. As an alternative of writing it down each time, you’ll be able to specify and use a listing of patterns in a file -f flag. The file should include one sample per line.

$ grep -f [pattern_file] [file_to_match]

In our instance, we created sample filenames sample.txt with the content material beneath:

$ cat sample.txt
This
It
$

To make use of it, use -f flag.

$ grep -f sample.txt text_file.txt
It is a pattern textual content file. It accommodates
It helps numbers like 1, 2, 3 and so forth. in addition to
It is a pattern textual content file. It is repeated two instances.
$

Specify a number of patterns

grep permits specifying a number of patterns utilizing -e flag.

$ grep -e [pattern1] -e [pattern2] -e [pattern3]...[file]

Instance:

$ grep -e is -e It -e to text_file.txt
It is a pattern textual content file. It accommodates
a number of strains for use as a part of testing grep
It helps numbers like 1, 2, 3 and so forth. in addition to
It is a pattern textual content file. It is repeated two instances.
$

Specify Prolonged RegEx

grep additionally helps the usage of Prolonged Common Expressions or ERE -E flag. That is much like egrep command in Linux.

Utilizing ERE has a bonus if you wish to deal with metacharacters as they’re and never change them with strings like grep. This offers you extra flexibility with regards to escaping it, as now we have to do within the case of grep. That being mentioned, use it -E with grep equals egrep command.

$ grep -E '[Extended RegEx]' [file]

This is one use of ERE the place we wish to print strains which are uncommented or clean. That is particularly helpful if you wish to discover one thing in massive configuration information. I’ve moreover used -v flag to NOT print strains that match the sample '^(#|$)'.

$ sudo grep -vE '^(#|$)' /and so forth/ssh/sshd_config
HostKey /and so forth/ssh/ssh_host_rsa_key
HostKey /and so forth/ssh/ssh_host_ecdsa_key
HostKey /and so forth/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile      .ssh/authorized_keys
PasswordAuthentication sure
ChallengeResponseAuthentication no
GSSAPIAuthentication sure
GSSAPICleanupCredentials no
UsePAM sure
X11Forwarding sure
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem       sftp    /usr/libexec/openssh/sftp-server
$

Conclusion

The above examples are simply the tip of the iceberg. grep helps a spread of choices and generally is a very great tool within the hand of somebody who is aware of tips on how to use it successfully. We can’t solely use the examples given above, but additionally mix them in several methods to get what we’d like.

Please consult with the person web page to learn extra about it.

$ man grep

Subsequent, study examples of SFTP instructions.

Leave a Comment

porno izle altyazılı porno porno