Introduction to GREP command
GREP means get regular expression and print but what is generally used for it is finding strings or sub strings within a file which is very handy for that in Linux. So the standard command is grep and then the string whichever you are looking for. In this topic, we are going to learn about GREP Command in Linux.
grep [options] pattern [files]
Options available are:
-h ==> it prints the matched lines, but do not print the filenames.
-l ==> it prints a list of a filenames only.
-n ==> it prints the matched lines and their line numbers.
-v ==> it prints out all the lines that do not match the pattern
-e exp ==> input expression with this option. It can use multiple times.
-f file ==> it takes patterns from a file, one per line
-E ==> Treats pattern as an extended regular expression.
-c ==> it prints only a count of the lines that match a pattern
-o ==> Print only the matched parts of a matching line with each such part on a separate output line.
-w ==> Match whole word
-l ==> prints list of filenames only.
-i ==> it will ignores case for matching
Examples of GREP Command in Linux
Here are the following examples mention below
grep world hello
Output: It will show a file or directory of the name hello.
Explanation: in this case, look for a world with file name, hello, and then it will return every line where there’s a match every line where it finds this pattern or this word in this file.
so, in this case, it found one line where there was a match and it returned hello world that’s the line.
grep 8 zip stock
Explanation: We can search multiple files we can do let’s say grip, in this case, our string is just a number eight and we’ll search in two files file1 and file2 so we can just add as many filenames on here as we want and it’ll search for this string inside of each of these files.
grep is *
Explanation: Now if we want to search every file in this current folder let’s see what we have here is five different files if we want to grep “is”. we can search every file in this folder and it returns each line in the word file it found this matches in a zip file it found.
grep -i line *
Explanation: we can also ignore the case so let’s say we do grep line which will search for the word line in every file in our current folder it only found matches in the word file and it found these three matches those three lines so if we do a grep – it will ignore case so it runs the same search except now it’s going to ignore case so it finds a couple more matches look here we found an all upper case line and here it found a couple of lines where the line is capitalized so the – I is one of the most useful operators to add on to the grep function going to ignore case in your search.
grep -v line word
Explanation: So this is going to do an inverse in other words everywhere every line where there’s not a match so should return every line except these three yeah so the word line does not appear.
grep -vi line word
Explanation: Add an “i” option there and what that does is ignore the case so, in other words, it found a match online where it was all uppercase and so it inverted that in other words, it doesn’t give us this line back this is the only line of text that doesn’t contain the word line in this file name.
grep -l 88*
grep 88 *
Explanation: now if we just maybe want to find all the file names that contain a match we don’t care about seeing the line themselves but we just want to see all the file names so we can do grep – L that’ll give us back the file names that contain the string 88 and there’s only one is “number”file right now if we don’t include this – L if we just search for it gives us back the line number of text including the name of the file
grep -c * 88
Explanation: And if you want the count of matches how many times a match is found in that file we could do grep – C for the count and then let’s just search for a simple text here 3, the number three and we’ll search in every file so that will give us the count in every file of lines where the number three appears so in hello it doesn’t appear at all it only has hello world so there are no numbers in number it appears five times and where it’s in stock there are two lines that have the letter number three now this is not the total number of times the number three appears is the number of lines that the number three appears on what we’re counting is lines
grep -r 945 z*
Explanation: so if you want to search recursively within all the subdirectories you can do grip – r which will do a recursive search
grep -i "los gatos" *
Explanation: Let’s search for “los gatos” and then we’ll search in * so all files this is going to find everything that has “los gatos” letters in it with this space in it and it’s going to ignore case and so here we get one hit in the files of the directory
This is a guide to GREP Command in Linux. Here we discuss the Examples of GREP Command in Linux along with the Explanation and syntax. You may also have a look at the following articles to learn more –