[ad_1]

The grep command can make it straightforward to uncover strings in textual content information on Linux methods, but which is just a start off. It can be used to research by means of these data files for multiple strings or regular expressions at the exact time. It can also ignore scenario when wanted, and it can rely the strains in the ensuing output for you. This article displays how to use grep in all these techniques.

Basic grep

The simplest grep command appears like the a person proven beneath. This “locate string in file” command will clearly show all the strains in the file that include the string, even when that string is only aspect of a longer one.

$ grep term story
The wording indicates there was much more to the story than any individual needed to confess.
The sword had been still left powering the get rid of. It was numerous days prior to it was

Obtaining several strings

There are a amount of methods to research for a group of strings in a single command. In the command down below, the ‘|’ character serves as an “or” perform. The command will screen any lines in the file that contain the term “xray”, the phrase “tape” or both of those.

$ grep -E 'xray|tape' 4letters
tape
xray

The -E solution permits “prolonged common expressions” to offer this perform. Another possibility is to checklist every string individually subsequent the -e possibility.

$ grep -e xray -e tape 4letters
tape
xray

You can also use a command like the just one below to obtain several strings. The “|” people separate every term that you want to locate.

$ grep 'xray|tape' 4letters
tape
xray

This variety of grep command is not restricted to two strings. Add as lots of strings as you want.

$ grep 'xray|tape|hope|boat' 4letters
boat
hope
tape
xray

You can also use common expressions like ^xr in instructions to appear for strings that start out with certain letters.

$ grep '^xr|tape|hope|boat' ../4letters
boat
hope
tape
xray
xref

The identical lookup can be performed employing grep‘s -e selection. In this situation, each individual string is integrated subsequent its personal -e.

$ grep -e ^xr -e tape -e hope -e boat 4letters
boat
hope
tape
xray
xref

If you only want to obtain correct matches for your strings, use a command like the a single down below that will only screen strings when they are included in the file as total words and phrases – not substrings. The command underneath fails to obtain the term “xray” because the “y” is omitted and the -w alternative is made use of.

$ grep -w 'xra|boat' 4letters
boat

In the examples underneath, the line containing the word “session” is only integrated when the full word is employed in the command.

$ grep -w 'fly|session' recording_instructions
When you very first open a session on the command line, the oldest commands in
their record command numbers throughout a solitary login session.
want "on the fly". In other words and phrases, style "script" and every command that
$ grep -w 'fly|sessio' recording_instructions
want "on the fly". In other phrases, kind "script" and each and every command that

Working with normal expressions

Note that the grep -e command will make it possible for you to search for strings that commence with a hyphen. Devoid of the -e, this would not do the job.

$ grep -e -xray myopts
-xray

The command beneath does not locate the string.

$ grep -xray myopts

Disregarding case

To obtain strings in textual content data files no matter of regardless of whether the letters are in uppercase or lowercase, use the grep command’s -i (dismiss scenario) possibility. In the illustrations down below, the term “it” is observed 10 periods in the initial case and 11 occasions when the -i option is made use of.

$ grep 'it' recording_instructions | wc -l
10
$ grep -i 'it' recording_instructions | wc -l
11

Counting strains

And this is a little shock. As a substitute of employing the wc command to depend the traces in the grep output, you can use the grep command itself. Just include the -c (rely) option. The instructions below deliver the similar success as the previous two instructions without having piping the output to the wc command:

$ grep -c 'it' recording_commands
10
$ grep -ic 'it' recording_commands
11

Wrap-up

The grep command can do a great deal much more than uncover each occasion of a single string in a textual content file. In actuality, it can seem for multiple strings in numerous various ways. It can also both ignore circumstance and count the number of lines that match your ask for.

Copyright © 2023 IDG Communications, Inc.

[ad_2]

Resource connection