Generalized Regular Expression Parser
- grep is a command line used for searching utility ,it will search globally and Outputs according to the expression.
- grep searches are case sensitive but we can insensitive search by adding the option -i..
- It searches the text anywhere on line both for numeric and alphabetic.
- grep ‘linux’ filename.txt
This search will check the entire document and gives the selective output what we need.
Lets create a text document anywhere ,, i am creating it in root directory as folder greptest and in that i am creating a text document called grep1.txt ,just a sample file in that i will enter some text..
so the path is :
[root@localhost ~]# ls
anaconda-ks.cfg new file testfile3.txt.zip testfile.txt.tar.gz
Desktop new file~ testfilegz.txt typescript
greptest test2.txt testfile.txt
install.log testfile1.txt testfile.txt.tar
install.log.syslog testfile3.txt testfile.txt.tar.bz2
Here we got the directory and lets see the content in it:
[root@localhost ~]# cat greptest/grep1.txt
Now in this document lest search only the file named “sridhar”
- To search a data or word in a specific document
Command: ” grep ‘sridhar’ grep1.txt “
Note: you must enter in to the directory first and then you need to start the search …
[root@localhost ~]# cd greptest/
[root@localhost greptest]# grep ‘sridhar’ grep1.txt
we got the file so this is case sensitive ….
If we want every file named with uppercase:
- case insensitive search using -i option.
2. To search a word with insensitive cases
Command : ” grep -i ‘sridhar’ grep1.txt “
[root@localhost greptest]# grep -i ‘sridhar’ grep1.txt
We have both upper case and also the lowercase …
And the search is same for the numbers also..(ex: “grep ‘5’ grep1.txt ” which will return if there are any )
- we can also search some specific task i.e we can search beginning of the text and list them all..
- To execute this search we will be using ” ^ ” anchor symbol.
3. To search every word beginning of the line.
Command :” grep ‘^linux’ grep1.txt “
- this command will execute where ever the word started with linux will be displayed but this is case sensitive ..
[root@localhost greptest]# grep ‘^linux’ grep1.txt
Here it showed all the word starting with linux..
if we want all the words with linux without case sensitive then the same option -i is used.
Command: ” grep ‘^linux’ grep1.txt “
- And if we want to search end of the line.. i.e searching some specific word which is available at the end of the line
- then we should use ” $ ” symbol which is used to search at the end of the line..
4. To search a word which is ends at every line
Command : “ grep ‘linux$’ grep1.txt “
Note : the ” $ ” the symbol must be specified at the end of the word.
[root@localhost greptest]# grep ‘linux$’ grep1.txt
5. To search numbers.
- If we want search the numbers from 0-10 or 5-8 or 20-59 etc.. i.e we want to list all the numbers available according to our desire then :
Command: ” grep ‘[0-9]’ grep1.txt “
[root@localhost greptest]# grep ‘[0-9]’ grep1.txt
This is the output which contains every number from 0-9…..
- The same issue is also for alphabetic lets consider we need to search from [a-z].
Command : ” grep ‘[a-z]’ grep1.txt ”
[root@localhost greptest]# grep ‘[a-z]’ grep1.txt
This will show every alphabet which has from a-z no matter if it has numbers in it..
In linux there are many packages like around 400 rpm’s are installed where if you want to list these rpm packages type in the command: ” rpm -qa ” this will list every rpm’s which is stored in you system……
Now if we want to search some specific rpm then we will be using a Pipe line( | )
Pipeline is used to grab the information which is entered before the pipe symbol and it will execute(outputs) according to the command typed after the pipe symbol..
6. To check the rpm in specific
lets check the grep package and search whether it is installed or not..
Command : ” rpm -qa | grep grep ”
[root@localhost ~]# rpm -qa | grep grep
This is the rpm of grep like wise lets search for DHCP package and NFS..
Command : ” rpm -qa | grep dhcp ”
[root@localhost ~]# rpm -qa | grep dhcp
Command :” rpm -qa | grep nfs ”
[root@localhost ~]# rpm -qa | grep nfs
For large information listing like xorg rpm i.e:
Command : “ rpm – qa | grep xorg ”
This will show more packages listed .
- so if you want to check how many xorg packages are there i.e how many of them then:
Command : ” rpm -qa | grep xorg | wc -l ”
Note: The pipe command is used to give the out put accordingly i.e here it will check the rpm and searches for xorg packages and i will word count and shows the count..
[root@localhost ~]# rpm -qa | grep xorg | wc -l
This is the output……
7. If want some specific file by elimination some:
- Now if we want some file and we need to eliminate some of the words then we will use the option -v
- This is used to invert the match i.e it will non select the match.
Command : “ grep -v sri grep1.txt ”
This will leave the word sri and its existing words and shows the rest of them.
[root@localhost greptest]# grep -v sri grep1.txt
Now we will invert the match i.e to avoid linux word then:
Command :” grep -v linux grep1.txt ”
[root@localhost greptest]# grep -v linux grep1.txt
Now its clear that it eliminated the linux word(here it is case sensitive thats why its showing uppercase LINUX).
- we can also combine this option i.e if we want to eliminate both the words( linux and sri) then:
Command: ” grep -v sri grep1.txt | grep -v linux ”
[root@localhost greptest]# grep -v sri grep1.txt | grep -v linux
Now its been eliminated……
8. To count the word which exists in the document
- to count the word which is available in the document we use -c option:
Command: “ grep -c sri grep.txt ”
[root@localhost greptest]# grep -c sri grep1.txt