
When you had option to show the N lines after match, you have the -B option for the opposite. B is the option which prints the specified N lines before the match. The following example prints the matched line, along with the 3 lines after it. A is the option which prints the specified N lines after the match as shown below. Word - word consists of a sequence of letters, digits and underscores.Įxample to show the difference between WORD and word WORD - WORD consists of a sequence of non-blank characters, separated with white space. You may want to do several navigation in relation to the words, such as: Please create the following demo_text file for this example. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match. When doing a grep on a huge file, it may be useful to see some lines after the match. Displaying lines before/after/around the match using grep -A, -B and -C $ grep -iw "is" demo_fileĪnd this is the last line. Please note that this output does not contain the line “This Line Has All Its First Character Of The Word With Upper Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not for “this”. The following example is the WORD grep where it is searching only for the word “is”. When you search for “is”, without any option it will show out “is”, “his”, “this” and everything which has the substring “is”. The following example is the regular grep where it is searching for “is”. Just doing out a normal search will show out all the lines. If you want to search for a word, and to avoid it to match the substrings use -w option.
#Grab grep pattern after a symbol full
Checking for full words, not for sub-strings using grep -w

Search for the given string in a single file

This Line Has All Its First Character Of The Word With Upper Case.Īnd this is the last line. This line is the 1st lower case line in this file. THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. In this article let us review 15 practical examples of Linux grep command that will be very useful to both newbies and experts.įirst create the following demo_file that will be used in the examples below to demonstrate grep command. Earlier we discussed 15 practical examples for Linux find command, Linux command line history and mysqladmin command. This is part of the on-going 15 Examples series, where 15 detailed examples will be provided for a specific command or functionality. You should get a grip on the Linux grep command.
