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. Read: How to copy a file to multiple directories in Linux 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |