RSS

grep

16 Feb

grep:

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.

Simple usages:

  • 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
linux
GREP
grep
sridhar
SRIDARAN
sridaran
Sridhar
sridhar
sridharsum
sridharlinux
sri

Now in this document lest search only the file named “sridhar”

  1. 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
sridhar
sridhar
sridharsum
sridharlinux

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
sridhar
Sridhar
sridhar
sridharsum
sridharlinux
SRIDHAR
SRIDHARLINUX

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
linux
linuxOS
linuxsolaris

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
linux
sridharlinux

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
1
linuxOS7
LINUX5
2
3
8
grep3

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
linux
linuxOS7
grep3

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
grep-2.5.1-54.2.el5

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
dhcpv6-client-1.0.10-16.el5

Command :”  rpm -qa | grep nfs

[root@localhost ~]# rpm -qa | grep nfs
nfs-utils-lib-1.0.8-7.2.z2
nfs-utils-1.0.9-40.el5

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
82

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
linux
1
linuxOS7
LINUX5
2
3
8
GREP
grep3

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
sridhar
srisum
sridaran
1
LINUX5
2
3
8
GREP
grep3

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
1
LINUX5
2
3
8
GREP
grep3

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
3

[root@localhost ~]# rpm -qa | grep xorg
xorg-x11-xinit-1.0.2-15.el5
xorg-x11-fonts-75dpi-7.1-2.1.el5
xorg-x11-fonts-base-7.1-2.1.el5
xorg-x11-server-Xorg-1.1.1-48.52.el5
xorg-x11-drv-penmount-1.1.0-2.1
xorg-x11-drv-tek4957-1.1.0-1.1
xorg-x11-drv-palmax-1.1.0-1.1
xorg-x11-drv-i810-1.6.5-9.21.el5
xorg-x11-drv-magellan-1.1.0-1.1
xorg-x11-drv-chips-1.1.1-2.1
xorg-x11-drv-trident-1.2.1-3.fc6
xorg-x11-drv-jamstudio-1.1.0-1.1
xorg-x11-drv-mga-1.4.2-10.el5
xorg-x11-drv-spaceorb-1.1.0-1.1
xorg-x11-drv-elo2300-1.1.0-1.1
xorg-x11-drv-apm-1.1.1-2.1
xorg-x11-drv-dummy-0.2.0-2.1
xorg-x11-drv-glint-1.1.1-4.1
xorg-x11-font-utils-7.1-2
xorg-x11-xauth-1.0.1-2.1
xorg-x11-xkb-utils-1.0.2-2.1
xorg-x11-apps-7.1-4.0.1.el5
xorg-x11-fonts-misc-7.1-2.1.el5
xorg-x11-fonts-100dpi-7.1-2.1.el5
xorg-x11-fonts-ISO8859-1-100dpi-7.1-2.1.el5
xorg-x11-fonts-truetype-7.1-2.1.el5
xorg-x11-xfs-1.0.2-4
xorg-x11-drv-void-1.1.0-3.1
xorg-x11-drv-keyboard-1.1.0-3
xorg-x11-drv-neomagic-1.1.1-2.1
xorg-x11-drv-vmmouse-12.4.0-2.1
xorg-x11-drv-elographics-1.1.0-1.1
xorg-x11-drv-dmc-1.1.0-2
xorg-x11-drv-ark-0.6.0-2.1
xorg-x11-drv-vmware-10.13.0-2.1
xorg-x11-drv-ati-6.6.3-3.22.el5
xorg-x11-drv-ast-0.81.0-3
xorg-x11-drv-nsc-2.8.1-2.1
xorg-x11-drv-fbdev-0.3.0-2
xorg-x11-drv-rendition-4.1.0-3.1
xorg-x11-drv-fpit-1.1.0-1.1
xorg-x11-drv-tseng-1.1.0-3.1
xorg-x11-drv-microtouch-1.1.0-1.1
xorg-x11-drv-calcomp-1.1.0-1.1
xorg-x11-drv-tdfx-1.2.1-3.1
xorg-x11-drv-s3-0.4.1-2.1
xorg-x11-drv-ur98-1.1.0-1.1
xorg-x11-drv-acecad-1.1.0-2.1
xorg-x11-drv-cirrus-1.1.0-2.fc6
xorg-x11-drv-savage-2.1.1-5.fc6
xorg-x11-drv-via-0.2.1-9
xorg-x11-drv-sisusb-0.8.1-4.1
xorg-x11-drv-vga-4.1.0-2.1
xorg-x11-drv-dynapro-1.1.0-2
xorg-x11-drv-nv-2.1.12-3.el5
xorg-x11-drv-v4l-0.1.1-4
xorg-x11-drv-summa-1.1.0-1.1
xorg-x11-filesystem-7.1-2.fc6
xorg-x11-server-utils-7.1-4.fc6
xorg-x11-twm-1.0.1-3.1
xorg-x11-utils-7.1-2.fc6
xorg-x11-fonts-ISO8859-1-75dpi-7.1-2.1.el5
xorg-x11-fonts-Type1-7.1-2.1.el5
xorg-x11-server-Xnest-1.1.1-48.52.el5
xorg-x11-drv-evdev-1.0.0.5-3.el5
xorg-x11-drv-vesa-1.3.0-8.1.el5
xorg-x11-drv-mouse-1.1.1-1.1
xorg-x11-drv-hyperpen-1.1.0-2
xorg-x11-drv-magictouch-1.0.0.5-2.1
xorg-x11-drv-joystick-1.1.0-1.1
xorg-x11-drv-i128-1.2.0-4
xorg-x11-drv-aiptek-1.0.1-2
xorg-x11-drv-digitaledge-1.1.0-1.1
xorg-x11-drv-s3virge-1.9.1-2.1
xorg-x11-drv-voodoo-1.1.0-3.1
xorg-x11-drv-citron-2.2.0-1.1
xorg-x11-drv-sis-0.9.1-7.1.el5
xorg-x11-drv-siliconmotion-1.4.1-2.1
xorg-x11-drv-i740-1.1.0-2.1
xorg-x11-drv-cyrix-1.1.0-4
xorg-x11-drv-mutouch-1.1.0-3
xorg-x11-drivers-7.1-4.1.el5
Advertisements
 
Leave a comment

Posted by on February 16, 2011 in GREP, Linux

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: