For instance, you want to return all the lines of the file above which do not contain the word “title”, this can be done using the -v switch as follows :Īs you can see, the line which contains the word title is not longer returned. Imagine you have the following html file :Īnd you want to return all the lines which DO NOT contain a specific word. In order to display both lines, I mean that satisfy the -A criteria and the -B criteria at the same time, we use the -C option as follows :Ĭat index.html | grep -C 2 “/head” ⇒ shows 2 lines before and after Now to display the lines that come afterwards, simply issue the command below with the -A switch :Ĭat index.html | grep -A 1 “/head” ⇒ shows 1 line afterĬat index.html | grep -A 2 “/head” ⇒ shows 2 lines after
‘/head’ :Ĭat index.html | grep -B 1 “/head” ⇒ shows 1 line beforeĬat index.html | grep -B 2 “/head” ⇒ shows 2 lines before We would like to display the line that comes after the line with the word being sought. Let’s return to our test file below, i.e. It is also possible with grep to display the lines that are before or after the line that contains the search string.
#How to use grep in editpad lite how to#
Read: How to compress JPEG or PNG images in Linux using the terminal Number of lines before and after : -A -B -C options We then piped this out to the grep command in order to search for the installed packages which contain the word ‘sql’ while ignoring the case. Where we executed the command ‘dpkg –list’ which will list out the installed programs. In order to better understand the example above, issue the command below : Here the cat command prepares the content of the file ‘index.html’ before outputting it to the grep command in order for the latter to do its job of searching for the word ‘Domain’. In order to ignore the word case, you simply need to add the -i switch as follows :Īs a alternative to the above command, you can also use the cat command as follows giving you the same result: Now if you invoke the same grep command above but with a small alteration which consists in changing the first letter of the word ‘domain’ to uppercase :Īs you can see, this has returned no results because the grep command is case sensitive. In order to check that this a correct output, let’s see the content of the file named index.html by executing :Īs you can see, the result returned does indeed match the highlighted line above. This can be achieved by running the grep command below : In the example below, we want to find the line which contains the word ‘domain’ in a file called index.htm in the current directory. Let’s say you want to search for a given word in one specific file. Grep ‘first_string second_string’ file_nameĬommand option | grep ‘text’ Search files Grep ‘single_word’ first_file second_file
The basic grep usage syntax is as follows : The syntax of the grep command is easy but it can get complicated as well depending on the nature of the task to be carried out.
#How to use grep in editpad lite install#
Sudo apt-get install grep The grep command syntax and how to use grep If grep is not installed on your system for whatever reason, you can always perform an installation using your package manager (apt-get on Debian/Ubuntu) by running the command : Basically, grep, which stands for ‘global regular expression print’ looks up a specific file for lines which contain a match to some given strings or words or even regular expressions and outputs the corresponding line. It is one of the most invoked utilities on Linux or Unix. Simply put, grep is a powerful pattern based tool used to search text within files. In this article you will learn how to use the grep command on Linux along with simple examples to help you find a string or pattern within a given file.