fertby.blogg.se

Grep usage in linux
Grep usage in linux






grep usage in linux

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”. The Filter takes input from one command, does some processing, and gives output.Ferrari is a sports car manufacture ferrari SP3 monza is their latest halo car Pipes ‘|’ send the output of one command as input of another command. We want to highlight only the lines that do not contain the character ‘a’, but the result should be in reverse order.įor this, the following syntax can be used. Let’s understand this with the help of an example. When you pipe two commands, the “filtered ” output of the first command is given to the next. A filter takes input from one command, does some processing, and gives output. Linux has a lot of filter commands like awk, grep, sed, spell, and wc. The example below shows reverse sorting of the contents in file ‘abc’. There are extensions to this command as well, and they are listed below. This command helps in sorting out the contents of a file alphabetically. Using the ‘i’ option grep has filtered the string ‘a’ (case-insensitive) from the all the lines. Let us try the first option ‘-i’ on the same file use above –

grep usage in linux

Shows just the name of the file with the string egrep is extended grep which extends functionality of grep and support more regular expression than grep command, It is also much faster than original unix. Shows all the lines that do not match the searched stringĭisplays only the count of matching lines Here, grep command has searched the file ‘sample’, for the string ‘Apple’ and ‘Eat’.įollowing options can be used with this command. It will scan the document for the desired information and present the result in a format you want. A better option is to use the grep command.

grep usage in linux

You may manually skim the content yourself to trace the information.

GREP USAGE IN LINUX CODE

Suppose you want to search a particular information the postal code from a text file.








Grep usage in linux