
In the example above this would return the lowercase “failure,” uppercase “FAILURE” or any combination thereof, such as “Failure.”. grep -i) to conduct a case insensitive search. This is an example of what grep might then return:Īpr 4 06:45:29 smatteso-vm1 sshd: pam_unix(sshd:auth): authentication failure logname= uid=0 euid=0 tty=ssh ruser= rhost= user=8boa5lv2rn8pso8Īpr 4 06:45:31 smatteso-vm1 sshd: pam_unix(sshd:auth): authentication failure logname= uid=0 euid=0 tty=ssh ruser= rhost= user=13hr26mnm8wo4kĪdd the -i switch (e.g. Let’s say I want to inspect the contents of the /var/log/secure log for any instances of a failure.

This is really one of the most elementary uses for grep. Here are 10 examples to help sharpen your skills. The options and patterns you can use with grep are varied and diverse. I’ll focus on regular grep for the purpose of this article, which is intended as a beginning tutorial for this handy and reliable command. The latter two have different methods for working with characters and search strings. There are multiple versions of grep plain regular grep, egrep (extended grep) and fgrep (fixed prep). The ‘^’ is used to display all lines that begin with the word you search for.Windows search is not without certain charms, but when I need to find files or their contents, or search for specific system information, the grep command in Linux never ceases to amaze me through its power and versatility. Here are 2 more search optimization techniques similar to options: Hen grep will search for the files with the name “git command” and print them out.ĭifferent options can be used together to narrow down your search or simply get better results. This command will first run the history command, which will send all the output as the input to the grep command (that is what pspie is used for, it sends the output of the first command as the input to the second command). Using Grep with pipe: history | grep “git command” The ‘-n’option printed out the line number of which the word is in, and the ‘-A 3’ option gave me 3 lines after the matching result. The option ‘-i’ made the grep command case insensitive which gave the output as ‘James William’ even though I entered ‘ james william’. The ‘-win – A 3’ is a combination of ‘-w’ option which I used so that I can forcibly choose only ‘James William’ and not the other names because the others have ‘ James William’ as a subscript in them. Now If I run a regular grep command with no options: Suppose I have a document ‘names.txt’ with the following names: James William, James Williams, James Williamson and James williamsons. This is better understood with an example: (let me take a different example) This option will match the whole word you want to search. This command will print out the entire document of cars.txt except for the line ‘Lamborghini is better than ferrari’. The option ‘v’ will print all the lines of the file which do not contain The command gave me 2 lines before and after the sentence it found the word ‘Lamborghini’. The 458 italia is my favorite car of themHowever I think,

For the rest of the article, I will be following the sample file cars.txt with the following content:Ĥ: ferrari SP3 monza is their latest halo carĩ : Test words: FeRrArI, feRRari, ferrARIġ0: The 458 italia is my favorite car of themġ5: Some of their cars also have scissor doors Let us look into the options of grep command in detail.īefore I go any further, let us take a standard text file to work on. We can also combine 2 or more options to combine their functionalities and narrow down the output to exactly how we want it. It prints the other outputs also (not only the 3rd output) because the word “ferrari” is in them too.īut what if we want to search only ‘ferrari’ or maybe even the line numbers it occurs in, or some context after and before the sentence the word is in? Well, that is why grep comes with a lot of predefined options to help us get a more accurate output. The grep command used above searches for the word ‘ferrari’ in the file ‘cars.txt’ and prints all the sentences in which it finds the word “ferrari”. Ferrari is a sports car manufacture ferrari SP3 monza is their latest halo car
