Taking benefit of the grep command’s lots of solutions
[ad_1]
The grep command can make it straightforward to discover strings in text information on Linux devices, but that is just a start off. It can be made use of to look for by means of these files for a number of strings or standard expressions at the identical time. It can also dismiss circumstance when wanted, and it can rely the strains in the ensuing output for you. This post shows how to use grep in all these methods.
Simple grep
The easiest grep command appears to be like the a single proven below. This “discover string in file” command will exhibit all the strains in the file that have the string, even when that string is only component of a for a longer period one particular.
$ grep phrase tale The wording indicates there was more to the story than any person wished to acknowledge. The sword experienced been left driving the get rid of. It was various days right before it was
Discovering multiple strings
There are a range of approaches to lookup for a team of strings in a single command. In the command under, the ‘|’ character serves as an “or” operate. The command will exhibit any lines in the file that include the phrase “xray”, the term “tape” or the two.
$ grep -E 'xray|tape' 4letters tape xray
The -E option permits “extended normal expressions” to give this operate. An additional choice is to listing every string separately pursuing the -e solution.
$ grep -e xray -e tape 4letters tape xray
You can also use a command like the a single under to locate several strings. The “|” people separate just about every term that you want to discover.
$ grep 'xray|tape' 4letters tape xray
This sort of grep command is not minimal to two strings. Insert as many strings as you require.
$ grep 'xray|tape|hope|boat' 4letters boat hope tape xray
You can also use frequent expressions like ^xr in instructions to seem for strings that start off with specific letters.
$ grep '^xr|tape|hope|boat' ../4letters boat hope tape xray xref
The exact lookup can be executed applying grep‘s -e solution. In this situation, every single string is incorporated next its own -e.
$ grep -e ^xr -e tape -e hope -e boat 4letters boat hope tape xray xref
If you only want to discover specific matches for your strings, use a command like the one particular down below that will only exhibit strings when they are incorporated in the file as complete terms – not substrings. The command under fails to come across the word “xray” simply because the “y” is omitted and the -w option is made use of.
$ grep -w 'xra|boat' 4letters boat
In the illustrations down below, the line containing the word “session” is only provided when the entire phrase is employed in the command.
$ grep -w 'fly|session' recording_instructions When you first open up a session on the command line, the oldest instructions in their history command quantities in the course of a single login session. want "on the fly". In other text, type "script" and every command that
$ grep -w 'fly|sessio' recording_commands want "on the fly". In other phrases, style "script" and every command that
Making use of typical expressions
Note that the grep -e command will allow for you to look for for strings that start with a hyphen. With out the -e, this would not perform.
$ grep -e -xray myopts -xray
The command under does not uncover the string.
$ grep -xray myopts
Disregarding circumstance
To discover strings in textual content files no matter of no matter whether the letters are in uppercase or lowercase, use the grep command’s -i (ignore scenario) possibility. In the examples down below, the word “it” is discovered 10 situations in the initially scenario and 11 instances when the -i option is made use of.
$ grep 'it' recording_instructions | wc -l 10 $ grep -i 'it' recording_commands | wc -l 11
Counting strains
And this is a little shock. As an alternative of using the wc command to depend the strains in the grep output, you can use the grep command alone. Just add the -c (rely) choice. The commands underneath make the similar effects as the prior two commands without 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 lot additional than come across every instance of a single string in a text file. In simple fact, it can glimpse for multiple strings in many unique means. It can also equally ignore case and rely the quantity of strains that match your ask for.
Copyright © 2023 IDG Communications, Inc.
[ad_2]
Source url