[ad_1]
The grep command tends to make it straightforward to uncover strings in text files on Linux systems, but which is just a start out. It can be used to lookup by means of these files for multiple strings or normal expressions at the identical time. It can also ignore scenario when required, and it can rely the traces in the ensuing output for you. This publish exhibits how to use grep in all these ways.
Simple grep
The easiest grep command appears like the a single proven below. This “come across string in file” command will exhibit all the strains in the file that incorporate the string, even when that string is only section of a for a longer time one.
$ grep term tale The wording implies there was additional to the story than any one desired to confess. The sword experienced been left driving the lose. It was many times ahead of it was
Acquiring numerous strings
There are a variety of methods to look for for a team of strings in a one command. In the command under, the ‘|’ character serves as an “or” perform. The command will display screen any traces in the file that contain the term “xray”, the term “tape” or each.
$ grep -E 'xray|tape' 4letters tape xray
The -E alternative allows “extended typical expressions” to deliver this purpose. Another choice is to listing each string individually following the -e choice.
$ grep -e xray -e tape 4letters tape xray
You can also use a command like the a single beneath to discover numerous strings. The “|” figures different just about every term that you want to come across.
$ grep 'xray|tape' 4letters tape xray
This sort of grep command is not constrained to two strings. Include as quite a few strings as you need.
$ grep 'xray|tape|hope|boat' 4letters boat hope tape xray
You can also use regular expressions like ^xr in commands to glimpse for strings that start out with specific letters.
$ grep '^xr|tape|hope|boat' ../4letters boat hope tape xray xref
The identical lookup can be carried out making use of grep‘s -e solution. In this case, every string is included pursuing its have -e.
$ grep -e ^xr -e tape -e hope -e boat 4letters boat hope tape xray xref
If you only want to find specific matches for your strings, use a command like the a single below that will only display strings when they are included in the file as total terms – not substrings. The command below fails to uncover the word “xray” for the reason that the “y” is omitted and the -w selection is applied.
$ grep -w 'xra|boat' 4letters boat
In the examples below, the line made up of the phrase “session” is only incorporated when the comprehensive phrase is applied 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 background command numbers throughout a solitary login session. want "on the fly". In other text, style "script" and each command that
$ grep -w 'fly|sessio' recording_instructions want "on the fly". In other words, form "script" and each and every command that
Employing regular expressions
Be aware that the grep -e command will allow for you to research for strings that commence with a hyphen. With out the -e, this would not work.
$ grep -e -xray myopts -xray
The command down below does not discover the string.
$ grep -xray myopts
Ignoring circumstance
To come across strings in textual content information no matter of whether or not the letters are in uppercase or lowercase, use the grep command’s -i (ignore case) possibility. In the illustrations below, the phrase “it” is found 10 occasions in the initially case and 11 situations when the -i option is used.
$ grep 'it' recording_commands | wc -l 10 $ grep -i 'it' recording_commands | wc -l 11
Counting strains
And this is a minimal shock. As an alternative of applying the wc command to count the strains in the grep output, you can use the grep command itself. Just increase the -c (count) selection. The commands underneath create the same final results as the past two instructions devoid of piping the output to the wc command:
$ grep -c 'it' recording_instructions 10 $ grep -ic 'it' recording_commands 11
Wrap-up
The grep command can do a good deal more than locate every instance of a single string in a text file. In point, it can search for various strings in several various ways. It can also both equally disregard scenario and rely the selection of strains that match your ask for.
Copyright © 2023 IDG Communications, Inc.
[ad_2]
Supply link