Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

11.6. Finding Text Within Files

grep Looks for text within files. For example:

grep this_word this_file.txt 

Example options:

  • -v --- this option is used to display lines which do not contain the string.

  • -n --- this option displays the line numbers

  • -w --- this option makes grep match the whole word

  • -A x or -B x (where x is a number) --- display "x" lines After or Before the section where the particular word is found.

  • -r or rgrep --- search for text within files recursively.

This command uses regular expressions, for more information please see, Section 20.4.2.

For example, this command would look in the file "rpmlist.txt" for anything starting with "rpm":

grep rpm rpmlist.txt

Or you could use it like this, to search through the output of another file:

rpm -qa | grep ogg

The first command lists all RPM's installed on your system, the second finds any containing the string "ogg" and outputs them.

rgrep A "recursive" version of grep (this is a different program to grep). This will search all the files in the current directory and all it's subdirectories and print the names of the files and the matching line. Follows similar syntax to grep (see above). You could also use grep with the -r option to achieve the same affect.

fgrep This version of grep calls grep with the -F option. This will look for literal strings only, it won't use or expand any kind of regular expression.

For example you could type:

fgrep a$*b? file.txt

And fgrep would look for the string "a$*b?" in the file "file.txt".

Tip Other Versions
 

There are various versions of grep which are designed to do different things try searching for them on the internet or within your distribution.

 
 
  Published under the terms of the GNU General Public License Design by Interspire