
Search for text in multiple files at once

So, like I said I especially use this if I’m searching for some text in a computer program or a function or something that I want to see the context surrounding that match. We can see that we got our match and there are 3 lines before the match and 3 lines after the match. This is also the one that I use most often as well. Print the number of lines before and after our matchĪnd if you want to see the lines before and after your match then you can use the Uppercase -C option. We can see that now instead of getting the lines before our match now we’re getting 3 lines after our match. Now if we want to see a certain number of lines after our match then we can use the Uppercase -A option. Print the number of lines after our match So we have 3 lines before our match here. $ grep -w -B 3 "Johnny Depp" file.txtĪs you can see we got our match but now there’s some additional context here. So, let’s just pass in a 3 which we’ll say that we want to see 3 lines before our match. I’m doing this separate from the other options that we added in because we want to pass in an argument to this. So I’m going to go ahead and pull up the last command that we just ran and now we want to add in this Uppercase -B option. So, if we want to see a certain number of lines that come before our match then we can use the uppercase -B option.
#Grep options in linux with examples code
So we might want to also see a certain number of lines before and after the match to get an idea of the context of where this match was found.Īnd this is especially common if you’re searching for something in a computer program that you wrote and want to see the surrounding code of the match and there are several ways that we can do this. Now another common bit of information that you might want is some additional context of where this match is found. Print the number of lines before our match It found a lowercase version on line 3 of that file and an uppercase version on line 1 of that file. $ grep -win "john" file.txtĪs you can see that now we’re getting some information about these matches. Now just finding those matches doesn’t give us information other than knowing our text is actually there but sometimes you want some additional information.įor example, it’s common to want to know the line number of where it found our match, and to do this we can add the -n option to our search. $ grep -wi "john" file.txtĪs you can see that now we get the lowercase version of that match and the regular case version of that match as well. So we can tell it to not be case sensitive by using the -i option. Now I also added a lower-case version of that name in that file but it’s currently not returning that because grep is case sensitive. It only returned the John Abraham.īecause we put in that we only wanted whole words. $ grep -w "John" file.txtĪs you can see that it didn’t return Johnny Depp. We only wanted to search for John and not return like Johnny depp. So now let’s say that we only wanted it to return a match if it was the whole word. The following command will search these three patterns: linux, depp and, sahu. You can search multiple patterns simultaneously. Now the second line was johnny depp but it still found within this name. $ grep "John" file.txtĪnd we can see that we got two results here where it found two lines containing john and return those entire lines. Instead, let’s search for john and see if that has any results. When there are no results it just jumps to the next line. If I run that then you can see it didn’t find any results. So let’s search this file using grep to see if it contains the text, Andrew. I am here in my terminal and in my current directory, there is a file named file.txt that has a bunch of random names and dummy data.
#Grep options in linux with examples how to
How to search file in Linux using the grep commandįirst of all, the most basic example is simply searching for some text within a normal file. You must follow the syntax given below to use the grep command. So, let’s go ahead and look at some practical examples where you might use this. Print number of lines before and after the match Print only a count of selected lines per File Print only names of files with selected lines Show only the part of a line matching Pattern


Print the number of lines before and after our match

How to search file in Linux using the grep command
