Home > Articles

  • Print
  • + Share This

Searching Text Files

Linux also comes with a number of programs that you can use to search files. Some of these commands use regular expressions (introduced in Chapter 2, "Entering Commands"), while others, such as the strings command, offer limited search capabilities.

Using the grep Command

The grep command is part of a family of commands: grep, fgrep, and egrep. These commands are closely related, but have different capabilities in the type of expressions or wildcards that can be used on the command line. For example, to look for any occurrence of the word hacker in the file cathedral-bazaar.txt:

$ grep -n hacker cathedral-bazaar.txt 
278:  happy extreme, is that a lot of users are hackers too.   
Because source 
279:  code is available, they can be effective hackers.  This can be 
... 
1269:  That being the case, it's doubly important that open-source  
 hackers 
1416:  the hacker culture mirrors the organization of its software,  
 and vice- 

The grep command returns matches for simple words as patterns on the command line. The -n (number) command-line option prints the line number of the matching line in which the pattern is found. The grep command, by default, recognizes and uses only basic regular expressions. If you want to use extended regular expressions, use egrep.

$ grep '\(gp' /etc/info-dir 
grep: Unmatched ( or \( 

Each grep command reads the standard input and writes to the standard output. However, search patterns can also be placed in a text file and used with the -f (file) option. The -f option tells grep to obtain matches from a file. The -n option specifies that grep should print the line numbers where it finds the matches. In this example you have a file that contains the words you want to search for:

$ cat searchfile 
esr 
Web 
$ grep -n -f searchfile cathedral-bazaar.txt 
180:  Linus Torvalds <http://www.tuxedo.org/~esr/faqs/linus>, for  
 example, 
1033:  birth of the World Wide Web, and that Linux left its infancy  
 during 
1178:  <http://www.tuxedo.org/~esr/writings/magic-cauldron/>. 
1456:  Linux.  The paper is accessible on the World Wide Web at 
1477:  available on the Web at  <http://www.agorics.com/ 
 agorpapers.html>. 
1640:  invent Emacs or the World Wide Web or the Internet itself by  
 chasing 
1785:  $Id: cathedral-bazaar.sgml,v 1.46 1999/08/08 02:21:32  
 esr Exp $ 

The grep command has more than 20 different command-line options and can be used to emulate the fgrep or egrep commands with the -F and -E options. For details about these programs, see the grep manual page, or use the GNU info program to read the info pages.

Using the egrep Command

The egrep command uses extended regular expressions (introduced in Chapter 2) for pattern matching, but also handles regular searches like grep and fgrep:

# egrep -n tee /etc/info-dir 
92:* tee: (sh-utils)tee invocation.   Redirect to multiple files. 

The difference between egrep and the other grep commands becomes apparent when regular expressions are used:

# egrep '\(gp' /etc/info-dir 
* gpm: (gpm).                   Text-mode mouse library. 

The egrep program returns a match, whereas fgrep reports nothing, and grep reports an error. Apostrophes (') are used to protect expressions from being interpreted by the shell.

The egrep command reads the standard input and writes to the standard output. Search patterns can be placed in a text file and used with the -f (file) option.

Using the fgrep Command

The fgrep command uses fixed strings for searching. Patterns and files are specified on the command line like so:

# fgrep -n tee /etc/info-dir 
92:* tee: (sh-utils)tee invocation.   Redirect to multiple files. 

As illustrated by the following, the fgrep command does not recognize regular expressions:

# fgrep '\(gp' /etc/info-dir

Because the fgrep command does not recognize expressions other than simple wildcards, no match is reported. Like the other grep commands, fgrep can use search patterns placed in a text file and used with the -f (file) option:

# cat > search.txt 
gpm 
[EOT] 
# fgrep -f search.txt /etc/info-dir 
* gpm: (gpm).                   Text-mode mouse library. 

Using the strings Command

Use the strings command to extract strings from binary files. This can be useful for peeking inside programs or for finding information inside commands. Combine the output of the strings command with other searching programs by using pipes. For example, the following command line reports on the copyright information of each program under the /usr/bin directory:

Without the -f (report files) option, the strings command does not insert the searched file's name in the output fed to the fgrep command.

Using the egrep and fgrep Commands

The egrep command uses extended regular expressions for pattern matching, but also handles simple searches like grep and fgrep:

$ egrep -n Id cathedral-bazaar.txt 
304:  fluid and very user-driven.  Ideas and prototype modes  
 were often 
1785:  $Id: cathedral-bazaar.sgml,v 1.46 1999/08/08 02:21:32  
 esr Exp $ 

Here you have used egrep, just like grep, to search for lines that contain the characters Id. The difference between egrep and the other grep commands becomes apparent when regular expressions are used:

$ egrep -n Id[a-z]+ cathedral-bazaar.txt 
304:  fluid and very user-driven.  Ideas and prototype modes were  
 often 
$ grep -n Id[a-z]+ cathedral-bazaar.txt 
$ fgrep -n Id[a-z]+ cathedral-bazaar.txt 
$ 

This time you searched for the characters Id immediately followed by one or more lowercase letters. The egrep program returns a match. grep returns nothing because the + you used means match one or more of the preceding patterns (any character between a and z in this case). This construct is recognized only when using extended regular expressions. fgrep reports nothing because it looks only for what you've literally typed (fixed strings), and not regular expressions. Single quotes (') can be used if you need to protect expressions and patterns from being interpreted by the shell.

$ fgrep -n '*' cathedral-bazaar.txt 
788:  the data stream as little as possible - and *never* throw away 

zgrep: searching compressed files

A related program called zgrep can be used to search compressed text files by using regular expressions. This program, a shell script, uses the grep or egrep program, along with the gzip command to decompress and search files on-the-fly.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.