handsraka.blogg.se

Grep vs egrep
Grep vs egrep













That means, we will get the lines that start with l, m, n, o, p, q, r, s, t, and u. Similarly, We can search for the lines that start with any character range between "l" to "u". Please note that the normal grep command can't do this. See? We have got all of the words that starts with either "l" or "o". Hence, our command for the above query will be: $ egrep '^(l|o)' file.txt Remember we use caret symbol (^) to search words at the beginning of line. However, It provides some additional functionalities, such as using complicated regex, than the normal grep command does out of the box.įor instance, we can search for any words that start with either "l" or "o". It will do all the things that grep will do. Egrep command examplesĮgrep stands for extended grep. Let us go ahead and learn the other two variants, namely egrep and fgrep. You should now have a basic understanding of grep usage. (dot).įor example, let us search for any word that has "n" in the file. $ grep x$ file.txtĪlso, you can find the words that contains any character using. Similarly, we can search for the words that ends with a particular letter(s), for example "x", like below. To search for the words that matches the pattern "tech" at the beginning of the line in a file, run: $ grep ^tech file.txt This is where special characters comes in handy. You don't want to display all the words that contains the string, but only the words that have the string "tech" at the beginning. $ grep tech file.txtīut what if you just wanted to search for the lines that only start with the word "tech". You know already, we can search for the words that contains the string "tech" like below.

#Grep vs egrep how to

Let me show you an example, so you can understand where and how to use those special characters. ^ - search at the beginning of the line.

grep vs egrep

We can also use some special characters or regular expressions in grep. The output of the file.txt is piped into grep and the words that contains the letters "os" in file.txt have been displayed. We can also pipe an output of a command into grep. Now, grep didn't care about the case and we got the words that contains both uppercase and lowercase letters in the result. Ostechn1x $ grep -i 'hello world' file.txt If you want, however, to ignore case sensitive, you can use "-i" flag like below. $ grep os file.txtīut, I have another word named "Ostechnix" in file.txt, but grep didn't list it. For example, when you search for "os", it is not going to display lines the contains uppercase letters i.e Os.Ĭheck the following example. This can be useful when you're working with a really long code.

grep vs egrep

You can also use -n flag to show the line numbers in the output: $ grep -n nix file.txt If the search string has two words, mention them inside single quotes like below. To do so, I run: $ grep nix file.txtĪs you see in the above output, we got two words that contains the matching pattern "nix". For example, I am going to look for the string "nix" in file.txt. To begin the search, just type grep followed by what it is you're looking for and where you are looking from.













Grep vs egrep