Saturday, February 11, 2017

grep command with examples part -2

welcome back friends, today i am going to show you some more features of grep command
so lets begin, some times you may to search one of the multiple patterns in a file, thanks to "-e" options
lets have a look :
i easily searched "Canada" and "Japan" in file "Beautiful_places.txt" using -e option
( this one is easy to remember as well "-e" for extended search pattern ☺) 



it is also possible to combine two different options , for example if you want to see the count of occurrence of either of two different patterns ( for instance total count of occurrence of either Canada or Japan in this case )
you can combine "-c" with "-e"


see this is where Unix starts getting powerful, you can combine different commands and also different options.and you may get the amazing results.

grep comes with one more useful option "-f" . this option is particularly useful while using grep command inside a shell script. since you can specify  multiple patterns using  a file

lets see following file parts.txt , this file contains two patterns "Trump" and "Ball"

  

my target file where i am going to search my pattern has been shown below :



lets see following what happens when you try to supply your search patterns using the file "parts.txt"



So it did produce the desired result ,

Wait more magic is coming with grep command  as we are getting more demanding

Now i demand grep to search the lines with pattern "ia", but "ia" should be prefixed by  either "s" or "d" nothing else. In short i am telling grep to search lines containing "sia" or "dia" but not "lia"
so output should contain "Asia" and "India" but not "Australia", but wait being stubborn kid i am also telling grep that it should not use "-e" or "-f" option . 




Well grep is smarter than i  could imagine , it has got many ways to achieve its goal, look below
it just used rectangular brackets [] and ruled like a boss



moral of the story : Never mess with grep !!!

last but not the least ( for today ). you can use "-E" option and specify an "logical OR" condition
check it out -


Thank you, Danke, Je vous remercie, Дякую, धन्यवाद for reading this post, have a nice day

Visit Other posts -


if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com

No comments:

Post a Comment