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

Thursday, February 9, 2017

grep command with examples - Part - 1

grep is one of most widely used filter in Unix and Linux operating systems, its very simple to use for beginners unlike advanced filters such as sed and awk, though it is less powerful it still comes with lot of inbuilt options, lets explore this good old friend of every Unix student and professional

i have one file "Beautiful_places.txt" . look what does it contain

Australia, country
United States, country
Canada, country
North America, continent
Europe, continent
Japan, country
Asia, continent
India, country
England, country




I have another file "games_and_sports.txt" with following content -
Trump, cards
Ball, cricket
Queen, chess




Now if you want lines only with word "country" from the file Beautiful_places.txt  use following



To see count of matches use "-c" options



 To exclude lines containing a pattern you can use grep with "-v" option



 To see all the lines containing both capital and lower case "a"  ( to make your search case insensitive ) from the file Beautiful_places.txt, use "-i" option with grep command



sometime you may need to  list files which contains certain pattern , "-l" option of grep command proves really helpful in such cases. See below example only one of the file was listed as only this file contained the your search pattern , 





 also see carefully i have used wild card for the list of files being scanned ( *txt ) , so that all the file ending with "txt" will be scanned for this search

 you may also need to see the line numbers of lines containing your search pattern
grep provides a solution for that too , its "-n" option  ( easy to remember as well :  n for number ☺)



grep has lot of other features , its is used frequently inside Unix shell scripts as well,grep regex is also one of the important topic, i will cover some of the advanced features of grep in my next post, you can visit grep man pages as well, i hope you all really enjoyed this short but useful introduction of grep command ,



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



Tuesday, February 7, 2017

Hard links and soft links with examples ( The ln command )

Links are really tricky and interesting part on Unix/Linux operating systems, Unix comes with two types links Hard links and soft links , also called symbolic links

I will first talk about hard links,
lets consider following file ( MYHARDLINK.txt)  as an example



 To create a hard link you have to use "ln" command, see following example



 once i used "ln" command , it created another file  MYHARDLINK.txt with exact same size and same content too





Now have a closer look





what else is same ?
yes its the "inode" number ( 136912 )
let have a look what happens when you delete the original file
Will the file created during hard link creation will be deleted ?
Or it will remain unaffected ?




So that's not a secret anymore , deleting one of the "filename" wont affect the actual data , that is why you can see file created during hard link creation is not deleted, i said "filename" because both "MYURL.txt" and "MYHARDLINK.txt" are merely file names and they both are pointing to same data on disk

Now coming to the soft links
you can create soft links  using "ln -s"
i have a file called Europe.txt and i created a sof link to it "Britain.txt"




what do you see ?
"inode" numbers are different size of original files and file created during soft link creation is different, even though if you open any one of them you will see the same text.
and interestingly  you see .....an arrow !!!
one more thing you can notice the permissions of  "Britain.txt" starts with "l"
which is enough to tell you that its a soft link

i created one more soft link to file Europe.txt with name Germany.txt



What happens when you delete one of the soft link, what impact will it will cause to original file ?
lets see that



So it does not affect the original file
Now the question comes , what happens when you delete the original file ? How is it going to impact the
soft link

here is your answer


 once you delete the original file you can still see the soft link but it becomes unusable
while you try to open it, it behaves like it does not exists
but remember it is still consuming one inode in memory , so how do you get rid of it ?
either remove it using rm command or use unlink command  as demonstrated below 


 




One important difference between hard and soft link is that, hard link can not be created across two different file systems while is possible to create soft link ( symbolic link ) in such manner

See what happens when i try to create a hard link and soft link (NewZealand.txt) for File Australia.txt


 

Make sure you give absolute path to file while doing so in order to create a working soft link

I hope you liked my effort to give you basic practical knowledge about unix links, Please do comment and share, your suggestions are always welcomed


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