Friday, May 12, 2023
HomeNetworkingProfiting from the grep command's many choices

Profiting from the grep command’s many choices


The grep command makes it simple to search out strings in textual content information on Linux programs, however that is only a begin. It may be used to go looking by these information for a number of strings or common expressions on the similar time. It might probably additionally ignore case when wanted, and it could actually rely the strains within the ensuing output for you. This submit exhibits use grep in all these methods.

Primary grep

The best grep command seems to be just like the one proven beneath. This “discover string in file” command will present all of the strains within the file that include the string, even when that string is just a part of an extended one.

$ grep phrase story
The wording suggests there was extra to the story than anybody needed to confess.
The sword had been left behind the shed. It was a number of days earlier than it was

Discovering a number of strings

There are a variety of how to seek for a gaggle of strings in a single command. Within the command beneath, the ‘|’ character serves as an “or” perform. The command will show any strains within the file that include the phrase “xray”, the phrase “tape” or each.

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

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

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

You can too use a command just like the one beneath to search out a number of strings. The “|” characters separate every phrase that you just need to discover.

$ grep 'xray|tape' 4letters
tape
xray

This type of grep command just isn’t restricted to 2 strings. Add as many strings as you want.

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

You can too use common expressions like ^xr in instructions to search for strings that start with explicit letters.

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

The identical search might be carried out utilizing grep‘s -e choice. On this case, every string is included following its personal -e.

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

In the event you solely need to discover precise matches on your strings, use a command just like the one beneath that may solely show strings when they’re included within the file as full phrases – not substrings. The command beneath fails to search out the phrase “xray” as a result of the “y” is omitted and the -w choice is used.

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

Within the examples beneath, the road containing the phrase “session” is just included when the total phrase is used within the command.

$ grep -w 'fly|session' recording_commands
While you first open a session on the command line, the oldest instructions in
their historical past command numbers throughout a single login session.
need "on the fly". In different phrases, sort "script" and every command that
$ grep -w 'fly|sessio' recording_commands
need "on the fly". In different phrases, sort "script" and every command that

Utilizing common expressions

Observe that the grep -e command will will let you seek for strings that start with a hyphen. With out the -e, this would not work.

$ grep -e -xray myopts
-xray

The command beneath does not discover the string.

$ grep -xray myopts

Ignoring case

To search out strings in textual content information no matter whether or not the letters are in uppercase or lowercase, use the grep command’s -i (ignore case) choice. Within the examples beneath, the phrase “it” is discovered 10 instances within the first case and 11 instances when the -i choice is used.

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

Counting strains

And here is a bit shock. As a substitute of utilizing the wc command to rely the strains within the grep output, you should utilize the grep command itself. Simply add the -c (rely) choice. The instructions beneath generate the identical outcomes because the earlier two instructions with out 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 much more than discover every occasion of a single string in a textual content file. In truth, it could actually search for a number of strings in a number of other ways. It might probably additionally each ignore case and rely the variety of strains that match your request.

Copyright © 2023 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments